N26 第二周

1、Linux上的文件管理類命令都有哪些,其常用的使用方法及其相關示例演示。

 [1]mkdir : make directories

           mkdir [OPTION]… DIRECTORY…

     example:

[root@localhost test]# ls

[root@localhost test]# mkdir a

[root@localhost test]# ls

a

        -p:自動按需創建父目錄(-p, –parentsno error if existing, make parent       directories as needed

[root@localhost test]# mkdir -p a/b

         -v, –verbose  顯示創建過程

[root@localhost test]# mkdir   -pv  a/b/c

mkdir: 已創建目錄 "a/b"

mkdir: 已創建目錄 "a/b/c"

         -m, –mode=M   set file mode (as in chmod), not a=rwx – umask

[root@localhost test]# mkdir  b

[root@localhost test]# mkdir -m 666 c

[root@localhost test]# ll

總用量 0

drwxr-xr-x. 4 root root 22 2月   5 13:35 a

drwxr-xr-x. 2 root root  6 2月   5 13:38 b

drw-rw-rw-. 2 root root  6 2月   5 13:38 c

 [2]rmdir : remove empty directories

rmdir [OPTION]… DIRECTORY…

example:

[root@localhost test]# tree

.

├── a

   ├── b

      └── c

   └── d

└── c

5 directories, 0 files

[root@localhost test]# rmdir c

[root@localhost test]# ls

a

      -p:刪除目錄后,如果其父目錄為空,則一并刪除之

  -v:顯示過程

            [root@localhost test]# tree a

        a

        └── b

   1 directory, 0 files

   [root@localhost test]# rmdir -pv a/b

   rmdir: 正在刪除目錄 "a/b"

   rmdir: 正在刪除目錄 "a"

 

 [3]tree命令

tree – list contents of directories in a tree-like format

        tree [options] [directory]

       -L level:指定要顯示的層級

   example:

[root@localhost test]# tree

.

└── a

    └── b

        └── c

3 directories, 0 files

[root@localhost test]# tree -L 2

.

└── a

    └── b

2 directories, 0 files

 [4]cat命令和tac命令

 cat [OPTION]… [FILE]…   #concatenate  files and print on the standard output

    -n : 顯示輸出行數

   [root@localhost test]# cat -n b

     1 a

     2 a

     3 a

[root@localhost test]# tac –help b

用法:tac [選項]… [文件]…

Write each FILE to standard output, last line first.

With no FILE, or when FILE is -, read standard input.

Mandatory arguments to long options are mandatory for short options too.

  -b, –before   在行前而非行尾添加分隔標志

  -r, –regex   將分隔標志視作正則表達式來解析

  -s, –separator=字符串 使用指定字符串代替換行作為分隔標志

 [5]more命令和less命令

 more命令:

more FILE

特點:翻屏至文件尾部后自動退出

     less命令和more命令相反

 [6]head命令

查看文件的前n

   head [options] FILE

 -n #=-#

[root@localhost test]# head b

a

b

c

[root@localhost test]# head -n 2 b

a

b

[root@localhost test]# head -2 b

a

b

 [7]tail命令:

查看文件的后n

      tail  [options] FILE

      -n #=-#

            [root@localhost test]# tail b

    a

    b

    c

    [root@localhost test]# tail -n 2 b

    b

    c

    [root@localhost test]# tail -2 b

    b

    c

-f:查看文件尾部內容結束后不退出,跟隨顯示新增的行

    圖片1.png

 [8]stat命令

stat -display file or file system status

   stat FILE…

     -f:–file-system   display file system status instead of file status

   example:

[root@localhost test]# stat a

  文件:"a"

  大小:14         塊:0          IO 塊:4096   目錄

設備:fd03h/64771d Inode8404856     硬鏈接:3

權限:(0755/drwxr-xr-x)  Uid(    0/    root)   Gid(    0/    root)

環境:unconfined_u:object_r:user_tmp_t:s0

最近訪問:2017-02-05 14:21:28.944173969 +0800

最近更改:2017-02-05 14:21:28.944173969 +0800

最近改動:2017-02-05 14:21:28.944173969 +0800

創建時間:

[root@localhost test]# stat -f a

  文件:"a"

    IDfd0300000000 文件名長度:255     類型:xfs

塊大?。?span style="font-family:Calibri">4096       基本塊大小:4096

    塊:總計:1277440    空閑:1244764    可用:1244764

Inodes: 總計:5120000    空閑:5115491

 [9]touch 命令

   touch – change file timestamps

    touch [OPTION]… FILE

     example

          -c –no create  不創建文件

[root@localhost test]# touch -c c

[root@localhost test]# ls

a  b

          -t  改成指定的時間  synopsis:YYMMDDHHMM.SS

  -a  change only the access time

[root@localhost test]# stat a

  文件:"a"

  大小:0          塊:0          IO 塊:4096   普通空文件

設備:fd03h/64771d Inode11830       硬鏈接:1

權限:(0644/-rw-r–r–)  Uid(    0/    root)   Gid(    0/    root)

環境:unconfined_u:object_r:user_tmp_t:s0

最近訪問:2017-02-05 19:20:33.997396041 +0800

最近更改:2017-02-05 19:20:33.997396041 +0800

最近改動:2017-02-05 19:20:33.997396041 +0800

創建時間:

[root@localhost test]# touch -a -t 1612261759.56 a

[root@localhost test]# stat a

  文件:"a"

  大?。?span style="font-family:Calibri">0          塊:0          IO 塊:4096   普通空文件

設備:fd03h/64771d Inode11830       硬鏈接:1

權限:(0644/-rw-r–r–)  Uid(    0/    root)   Gid(    0/    root)

環境:unconfined_u:object_r:user_tmp_t:s0

最近訪問:2016-12-26 17:59:56.000000000 +0800

最近更改:2017-02-05 19:20:33.997396041 +0800

最近改動:2017-02-05 19:20:48.373396219 +0800

創建時間:

    -m   change only the modification time

          -r  –references   use this file's times instead of current time

[root@localhost test]# stat a

  文件:"a"

  大?。?span style="font-family:Calibri">14         塊:0          IO 塊:4096   目錄

設備:fd03h/64771d Inode8404856     硬鏈接:3

權限:(0755/drwxr-xr-x)  Uid(    0/    root)   Gid(    0/    root)

環境:unconfined_u:object_r:user_tmp_t:s0

最近訪問:2016-01-12 17:58:36.000000000 +0800

最近更改:2017-02-05 14:21:28.944173969 +0800

最近改動:2017-02-05 15:22:34.117219326 +0800

創建時間:

[root@localhost test]# touch -r a c

[root@localhost test]# stat c

  文件:"c"

  大?。?span style="font-family:Calibri">0          塊:0          IO 塊:4096   普通空文件

設備:fd03h/64771d Inode11830       硬鏈接:1

權限:(0644/-rw-r–r–)  Uid(    0/    root)   Gid(    0/    root)

環境:unconfined_u:object_r:user_tmp_t:s0

最近訪問:2016-01-12 17:58:36.000000000 +0800

最近更改:2017-02-05 14:21:28.944173969 +0800

最近改動:2017-02-05 15:36:29.399229662 +0800

創建時間:

 [10]cp命令:copy

源文件:目標文件

    單源復制:cp [OPTION]… [-T] SOURCE DEST

如果DEST不存在:則事先創建此文件,并復制源文件的數據流至DEST

[root@localhost test]# ls

[root@localhost test]# touch 1

[root@localhost test]# mkdir a

[root@localhost test]# cp 1 a/2

[root@localhost test]# ls a

2

如果DEST存在:

  如果DEST是非空目錄文件,則覆蓋目標文件

[root@localhost test]# cat  1

111

[root@localhost test]# cat a/1

[root@localhost test]# cp 1 a/

cp:是否覆蓋"a/1"? y

[root@localhost test]# cat a/1

111

  如果DEST是目錄文件,則先在DEST目錄下創建一個與源文件同名的文件,并      

          復制數據流

[root@localhost test]# ls

1  a

[root@localhost test]# ls a

2

[root@localhost test]# cp 1 a/

[root@localhost test]# ls a

1  2

    多源復制:cp [OPTION]…SOURCE…DIRECTORY   

  cp [OPTION]… -t DIRECTORY SOURCE…

如果DEST不存在,錯誤

[root@localhost test]# ls

1  2

[root@localhost test]# cp {1,2} a

cp: 目標"a" 不是目錄

如果DEST存在:

  如果DEST是非目錄文件,錯誤

[root@localhost test]# ll

總用量 0

-rw-r–r–. 1 root root 0 2月   5 16:46 1

-rw-r–r–. 1 root root 0 2月   5 16:46 2

-rw-r–r–. 1 root root 0 2月   5 16:48 3

[root@localhost test]# cp {1,2} 3

cp: 目標"3" 不是目錄

  如果DEST是目錄文件,分別復制每個文件至目標目錄中,并保持原名

[root@localhost test]# ll

總用量 0

-rw-r–r–. 1 root root 0 2月   5 16:46 1

-rw-r–r–. 1 root root 0 2月   5 16:46 2

-rw-r–r–. 1 root root 0 2月   5 16:48 3

drwxr-xr-x. 2 root root 6 2月   5 16:49 a

[root@localhost test]# cp {1,2,3} a

[root@localhost test]# ls a

1  2  3

  常用選項:

-i-interactive交互式復制,既覆蓋之前提醒用戶確認

[root@localhost test]# ls

1  2  3  a

[root@localhost test]# ls a

1  2  3

[root@localhost test]# cp -i 1 a/

cp:是否覆蓋"a/1"? y

-f–force強制覆蓋目標文件

-r=-R:遞歸復制目錄

[root@localhost test]# ls

1  2  3  a

[root@localhost test]# mkdir b

[root@localhost test]# cp a b

cp: 略過目錄"a"

[root@localhost test]# cp -r  a b

[root@localhost test]# ls b

a

-d:復制符號鏈接文件本身,而非指向的源文件

[root@localhost test]# touch 1

[root@localhost test]# ln -s 1 2

[root@localhost test]# cp -d 2 3

[root@localhost test]# ll

總用量 0

-rw-r–r–. 1 root root 0 2月   5 17:16 1

lrwxrwxrwx. 1 root root 1 2月   5 17:16 2 -> 1

lrwxrwxrwx. 1 root root 1 2月   5 17:16 3 -> 1

-a-dR –preserve=all,archive,用于實現歸檔

[root@localhost test]# cp -a 3 4

[root@localhost test]# ll

總用量 0

-rw-r–r–. 1 root root 0 2月   5 17:16 1

lrwxrwxrwx. 1 root root 1 2月   5 17:16 2 -> 1

lrwxrwxrwx. 1 root root 1 2月   5 17:16 3 -> 1

lrwxrwxrwx. 1 root root 1 2月   5 17:16 4 -> 1

[root@localhost test]#

  -preserv=

mode:權限

[root@localhost test]# ll

總用量 0

-rw-rw-rw-. 1 root root 0 2月   5 17:16 1

[root@localhost test]# cp 1 5

[root@localhost test]# cp –preserve=mode 1 6

[root@localhost test]# ll

總用量 0

-rw-rw-rw-. 1 root root 0 2月   5 17:16 1

-rw-r–r–. 1 root root 0 2月   5 17:25 5

-rw-rw-rw-. 1 root root 0 2月   5 17:26 6

ownership:屬主和屬組

[root@localhost test]# ll

總用量 0

-rw-rw-r–. 1 l_cong l_cong 0 2月   5 17:29 1

[root@localhost test]# cp  1 2

[root@localhost test]# cp –preserve=ownership 1 3

[root@localhost test]# ll

總用量 0

-rw-rw-r–. 1 l_cong l_cong 0 2月   5 17:29 1

-rw-r–r–. 1 root   root   0 2月   5 17:30 2

-rw-rw-r–. 1 l_cong l_cong 0 2月   5 17:30 3

timestamp:時間戳

[root@localhost test]# cp –preserve=timestamp 1 3

[root@localhost test]# ll

總用量 0

-rw-rw-r–. 1 l_cong l_cong 0 2月   5 17:29 1

-rw-r–r–. 1 root   root   0 2月   5 17:31 2

-rw-r–r–. 1 root   root   0 2月   5 17:29 3

context:安全標簽

xattr:擴展屬性

links:符號鏈接

all:上述所有屬性

 [12]mv命令:move

mv [OPTION]…[-T] SOURCE DEST

mv [OPTION]… SOURCE…DIRECTORY

mv [OPTION]… DIRECTORY SOURCE…

常用選項:

-i:交互式

-fforce

example

[root@localhost test]# cat 1

qqqqqq

[root@localhost test]# cat 6

[root@localhost test]# mv -f 1 6

[root@localhost test]# ls

3  6

[root@localhost test]# cat 6

qqqqqq

[root@localhost test]#

 [13]rm命令:remove

rm [OPTION]…FILE…

 常用選項:

-iinteractive

-fforce

-rrecursive

example

[root@localhost test]# rm 3

rm:是否刪除普通空文件 "3"?

[root@localhost test]# rm -r 3

rm:是否刪除普通空文件 "3"

[root@localhost test]# mkdir a

[root@localhost test]# rm a

rm: 無法刪除"a": 是一個目錄

[root@localhost test]# rm -r a

rm:是否刪除目錄 "a"?

[root@localhost test]# rm -f a

rm: 無法刪除"a": 是一個目錄

[root@localhost test]# rm -f 1

[root@localhost test]# rm -rf a

 

2、bash的工作特性之命令執行狀態返回值和命令行展開所涉及的內容及其示例演示。

 [1]命令執行的狀態結果:

bash通過狀態返回值來輸出此結果:

成功:0

失敗:1-255

命令執行完成之后,其狀態返回值保存于bash的特殊變量$?

命令正常執行時,有的還回有命令返回值

根據命令及其功能不同,結果各不相同

example:

[root@localhost test]# ls

3  6

[root@localhost test]# echo $?

0

[root@localhost test]# lll

bash: lll: 未找到命令

[root@localhost test]# echo $?

127

 [2]引用命令的執行結果:$(COMMAND)`COMMAND`

example:

[root@localhost test]# touch 1

[root@localhost test]# cat 1

[root@localhost test]# echo $(ls /)  >>  1

[root@localhost test]# cat 1

bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var

[root@localhost test]# echo `ls /root`  >>  1  

[root@localhost test]# cat 1

bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var

initial-setup-ks.cfg 公共 模板 視頻 圖片 文檔 下載 音樂 桌面

 [3]強引用:''和弱引用:""

 [4]快捷鍵

Ctrl+a:跳轉至命令行行首

Ctrl+e:跳轉至命令行行尾

Ctrl+l:清屏,相當于clear

 

3、請使用命令行展開功能來完成以下練習:

   (1)、創建/tmp目錄下的:a_c, a_d, b_c, b_d

[root@localhost /]# cd /var/tmp/test/

[root@localhost test]# mkdir -v /tmp/{a,b}_{c,d}

mkdir: 已創建目錄 "/tmp/a_c"

mkdir: 已創建目錄 "/tmp/a_d"

mkdir: 已創建目錄 "/tmp/b_c"

mkdir: 已創建目錄 "/tmp/b_d"

   (2)、創建/tmp/mylinux目錄下的:

mylinux/

    ├── bin

    ├── boot

    │   └── grub

    ├── dev

    ├── etc

    │   ├── rc.d

    │      └── init.d

    │   └── sysconfig

    │       └── network-scripts

    ├── lib

    │   └── modules

    ├── lib64

    ├── proc

    ├── sbin

    ├── sys

    ├── tmp

    ├── usr

    │   └── local

    │       ├── bin

    │       └── sbin

    └── var

        ├── lock

        ├── log

        └── run

[root@localhost test]# mkdir -pv

/tmp/mylinux/{bin,boot/grub,dev,etc/{rc.d/init.d,sysconfig/network-scripts},lib/modules,lib64,proc,sbin,sys,tmp,usr/local/{bin,sbin},var/{lock,log,run}}

mkdir: 已創建目錄 "/tmp/mylinux"

mkdir: 已創建目錄 "/tmp/mylinux/bin"

mkdir: 已創建目錄 "/tmp/mylinux/boot"

mkdir: 已創建目錄 "/tmp/mylinux/boot/grub"

mkdir: 已創建目錄 "/tmp/mylinux/dev"

mkdir: 已創建目錄 "/tmp/mylinux/etc"

mkdir: 已創建目錄 "/tmp/mylinux/etc/rc.d"

mkdir: 已創建目錄 "/tmp/mylinux/etc/rc.d/init.d"

mkdir: 已創建目錄 "/tmp/mylinux/etc/sysconfig"

mkdir: 已創建目錄 "/tmp/mylinux/etc/sysconfig/network-scripts"

mkdir: 已創建目錄 "/tmp/mylinux/lib"

mkdir: 已創建目錄 "/tmp/mylinux/lib/modules"

mkdir: 已創建目錄 "/tmp/mylinux/lib64"

mkdir: 已創建目錄 "/tmp/mylinux/proc"

mkdir: 已創建目錄 "/tmp/mylinux/sbin"

mkdir: 已創建目錄 "/tmp/mylinux/sys"

mkdir: 已創建目錄 "/tmp/mylinux/tmp"

mkdir: 已創建目錄 "/tmp/mylinux/usr"

mkdir: 已創建目錄 "/tmp/mylinux/usr/local"

mkdir: 已創建目錄 "/tmp/mylinux/usr/local/bin"

mkdir: 已創建目錄 "/tmp/mylinux/usr/local/sbin"

mkdir: 已創建目錄 "/tmp/mylinux/var"

mkdir: 已創建目錄 "/tmp/mylinux/var/lock"

mkdir: 已創建目錄 "/tmp/mylinux/var/log"

mkdir: 已創建目錄 "/tmp/mylinux/var/run"

 

4、文件的元數據信息有哪些,分別表示什么含義,如何查看?如何修改文件的時間戳信息

 元數據信息有:文件名稱,文件大小,文件類型,訪問權限,時間戳等等。用stat命令查看,touch命令更改時間戳。

Example:

[root@localhost test]# stat a

  文件:"a"

  大?。?span style="font-family:Calibri">0          塊:0          IO 塊:4096   普通空文件

設備:fd03h/64771d Inode11830       硬鏈接:1

權限:(0644/-rw-r–r–)  Uid(    0/    root)   Gid(    0/    root)

環境:unconfined_u:object_r:user_tmp_t:s0

最近訪問:2017-02-05 19:11:49.380389549 +0800

最近更改:2017-02-05 19:11:49.380389549 +0800

最近改動:2017-02-05 19:11:49.380389549 +0800

創建時間:

  更改訪問的時間為指定的時間:

[root@localhost test]# stat a

  文件:"a"

  大小:0          塊:0          IO 塊:4096   普通空文件

設備:fd03h/64771d Inode11830       硬鏈接:1

權限:(0644/-rw-r–r–)  Uid(    0/    root)   Gid(    0/    root)

環境:unconfined_u:object_r:user_tmp_t:s0

最近訪問:2017-02-05 19:20:33.997396041 +0800

最近更改:2017-02-05 19:20:33.997396041 +0800

最近改動:2017-02-05 19:20:33.997396041 +0800

創建時間:

[root@localhost test]# touch -a -t 1612261759.56 a

[root@localhost test]# stat a

  文件:"a"

  大?。?span style="font-family:Calibri">0          塊:0          IO 塊:4096   普通空文件

設備:fd03h/64771d Inode11830       硬鏈接:1

權限:(0644/-rw-r–r–)  Uid(    0/    root)   Gid(    0/    root)

環境:unconfined_u:object_r:user_tmp_t:s0

最近訪問:2016-12-26 17:59:56.000000000 +0800

最近更改:2017-02-05 19:20:33.997396041 +0800

最近改動:2017-02-05 19:20:48.373396219 +0800

創建時間:

。

5、如何定義一個命令的別名,如何在命令中引用另一個命令的執行結果?

   用alias命令定義別名:

     alias  name=value

注意:別名只對當前shell有效,如果想永久生效,需要定義在配置文件中。

引用命令的執行結果:$(COMMAND)`COMMAND`

 

6、顯示/var目錄下所有以1開頭,以一個小寫字母結尾,且中間至少出現一位數字(可以有其它字符)的文件或目錄。

[root@localhost test]# ls -d  /var/1*[0-9]*[[:lower:]]

/var/1a2sa

 

7、顯示/etc目錄下,以任意一個數字開頭,且以非數字結尾的文件或目錄?

[root@localhost test]# ls  -d /etc/[0-9]*[^0-9]

/etc/1aaa

 

8、顯示/etc目錄下,以非字母開頭,后面跟了一個字母以及其它任意長度任意字符的文件或目錄。

[root@localhost test]# ls -d  /etc/[^[:alpha:]][a-z]*

/etc/1aaa

 

9、/tmp目錄下創建以tfile開頭,后跟當前日期和時間的文件,文件名形如:tfile-2016-05-27-09-32-22。

[root@localhost test]# touch /var/tfile-$(date +%F-%H-%M-%S)

 

10、復制/etc目錄下所有以p開頭,以非數字結尾的文件目錄復制到/tmp/mytest1目錄中。

[root@localhost test]# cp -r /etc/p*[^0-9] /tmp/mytest1/

 

11、復制/etc目錄下所有以.d結尾的文件或目錄至/tmp/mytest2目錄中。

[root@localhost test]# cp -r /etc/*.d /tmp/mytest2

 

12、復制/etc/目錄下所有以1mn開頭,以.conf結尾的文件至/tmp/mytest3中。

[root@localhost test]#cp -r /etc/[1mn]*.conf /tmp/mytest3

 

 

原創文章,作者:N26-xiaocong,如若轉載,請注明出處:http://www.www58058.com/67582

(0)
N26-xiaocongN26-xiaocong
上一篇 2017-02-05
下一篇 2017-02-05

相關推薦

  • 文件管理基礎知識及命令詳解

    文件系統     文件和目錄被組織成一個單根倒置樹結構     文件系統從根目錄下開始,用“ /”表示     根文件系統(rootfs): root filesystem    …

    Linux干貨 2016-08-05
  • for,while,until簡介

    Shell(以Bash為例)中的循環語句一般有for、while、until這幾種,偶爾還有寫錯語法的時候,這里結合實例來自己總結一下。也為今后使用提供一個快捷的資料獲取渠道。 一、for循環語句 實例1.1 最基本的for循環: (傳統的形式,for var in …) 代碼如下: #!/bin/bashfor x in one two three fou…

    Linux干貨 2016-08-22
  • 09yum的使用以及簡單配置

    YUM: yellowdog update modifier ,rpm的前端程序,用來解決軟件包相關依賴性,可以在多個庫之間定位軟件包。 yum repository:yum repo,存儲了眾多RPM包,以及包相關的元數據文件,放置于特定目錄repodata下。 yum 訪問的文件服務器主要有三種,ftp,http,file。 yum客戶端配置文件: 【/…

    Linux干貨 2016-11-04
  • rsyslog, mysql, loganalyzer聯合實現

    一. rsyslog簡介 rsyslog是一個自由軟件, 是GPL的lincesed增強的syslogd. 它提供了Mysql和完全可配置的輸出格式的支持. 1. 日志信息格式:      <優先級>時間戳 主機名 模塊名/級別/信息摘要:內容      <priority>…

    Linux干貨 2016-10-24
  • RAID簡述和Linux軟RAID配置

    RAID簡述和Linux軟RAID配置 PS:僅為課程練習,原理和配置可能有誤,僅供參考,謝謝; 1.RAID功能簡述     RAID,全稱(Redundant Arrays of Inexpensive Disk或Redundant Arrays of Independent Disk),現在一般多用于后面那個稱呼,獨立磁盤冗余陣列。RAID的基礎概念大…

    Linux干貨 2016-07-17
  • AWK

    awk:報告生成器,格式化文本輸出         處理方式:文件里有多行,awk處理時,先將第一行放到內存里面,然后將該行分割成列(字段),第一個字段叫$1,第二個字段$2,以此類推,$0代表整行。        &n…

    Linux干貨 2016-09-26

評論列表(1條)

  • 馬哥教育
    馬哥教育 2017-03-02 19:48

    建議操作系統中要使用中文。

欧美性久久久久