天天看點

Linux指令實踐(四)

Linux指令實踐(四)

echo "字元串" > 檔案 或者 echo "字元串" >> 檔案 是把字元串重定向到檔案裡,如果檔案不存在則建立,然後把字元串寫進檔案裡,兩者的差別是'>'是輸出重定向,它會清空原有檔案内容,然後再把字元串内容寫進檔案,而'>>'是追加輸出重定向,它不會清空原有的檔案内容,隻在原有檔案的内容最後把字元串内容寫進檔案。 > 檔案 表示清空檔案,如果檔案存在則清空,如果不存在則建立。

1、Linux上的檔案管理類指令都有哪些,其常用的使用方法及相關示例示範。

  • 檔案或目錄的建立

    touch :将每個檔案的通路時間和修改時間修改為目前時間。若檔案不存在将會建立為空檔案,除非使用-c或-h選項。

      用法:touch  [選項]... 檔案...

      選項:

        -a            隻更改通路時間

        -c,  --no-create    不建立任何檔案 

        -d,  --dete=字元串   使用指定字元串表示時間而非目前時間 

        -f           (忽略)

        -h,  --no-dereference    會影響符号連接配接本身,而非符号連接配接所訓示的目的地

                    (目前系統支援更改符号連接配接的所有者時,此選項才有用)

        -m           隻更改修改時間

        -r,  --reference=檔案  使用指定檔案的時間屬性而非目前時間

        -t  STAMP       使用[[CC]YY]MMDDhhmm[.ss]  格式的時間而非目前時間

        --time=WORD     使用WORD指定的時間:access、atime、use都等價于-a選項的效果

                    而modify、mtime等價于-m選項的效果

          --help       顯示此幫助資訊并退出

          --version       顯示版本資訊并退出

      請注意,-d和-t選項可接受不同的日期/時間格式。

常用示範:

[root@centos6_test tmp]#touch xxx
[root@centos6_test tmp]#stat xxx
  File: "xxx"
  Size: 0         	Blocks: 0          IO Block: 4096   普通空檔案
Device: fd00h/64768d	Inode: 1316024     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-10-10 13:28:38.382002766 +0800
Modify: 2019-10-10 13:28:38.382002766 +0800
Change: 2019-10-10 13:28:38.382002766 +0800
[root@centos6_test tmp]#touch -a xxx
[root@centos6_test tmp]#stat xxx
  File: "xxx"
  Size: 0         	Blocks: 0          IO Block: 4096   普通空檔案
Device: fd00h/64768d	Inode: 1316024     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-10-10 13:28:57.982999292 +0800
Modify: 2019-10-10 13:28:38.382002766 +0800
Change: 2019-10-10 13:28:57.982999292 +0800
[root@centos6_test tmp]#ll
總用量 0
-rw-r--r--. 1 root root 0 10月 10 13:28 xxx
[root@centos6_test tmp]#touch -c aaa
[root@centos6_test tmp]#ll
總用量 0
-rw-r--r--. 1 root root 0 10月 10 13:28 xxx
[root@centos6_test tmp]#date 
2019年 10月 10日 星期四 13:29:55 CST
[root@centos6_test tmp]#touch -d '2019-11-11 11:30:10' abc
[root@centos6_test tmp]#ll
總用量 0
-rw-r--r--. 1 root root 0 11月 11 2019 abc
-rw-r--r--. 1 root root 0 10月 10 13:28 xxx
[root@centos6_test tmp]#pwd
/tmp
[root@centos6_test tmp]#cd /root
[root@centos6_test ~]#ls
scripts
[root@centos6_test ~]#touch test.txt
[root@centos6_test ~]#cd /tmp
[root@centos6_test tmp]#ln -s ../root/test.txt test.txt
[root@centos6_test tmp]#ll
總用量 0
-rw-r--r--. 1 root root  0 11月 11 2019 abc
lrwxrwxrwx. 1 root root 16 10月 10 13:38 test.txt -> ../root/test.txt
-rw-r--r--. 1 root root  0 10月 10 13:28 xxx
[root@centos6_test tmp]#stat /root/test.txt 
  File: "/root/test.txt"
  Size: 0         	Blocks: 0          IO Block: 4096   普通空檔案
Device: fd00h/64768d	Inode: 793356      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-10-10 13:38:23.595005397 +0800
Modify: 2019-10-10 13:38:23.595005397 +0800
Change: 2019-10-10 13:38:23.595005397 +0800
[root@centos6_test tmp]#stat test.txt 
  File: "test.txt" -> "../root/test.txt"
  Size: 16        	Blocks: 0          IO Block: 4096   符号連結
Device: fd00h/64768d	Inode: 1316027     Links: 1
Access: (0777/lrwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-10-10 13:38:52.785002202 +0800
Modify: 2019-10-10 13:38:50.867001705 +0800
Change: 2019-10-10 13:38:50.867001705 +0800
[root@centos6_test tmp]#touch -h test.txt 
[root@centos6_test tmp]#stat test.txt 
  File: "test.txt" -> "../root/test.txt"
  Size: 16        	Blocks: 0          IO Block: 4096   符号連結
Device: fd00h/64768d	Inode: 1316027     Links: 1
Access: (0777/lrwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-10-10 13:40:16.896011892 +0800
Modify: 2019-10-10 13:40:13.286992548 +0800
Change: 2019-10-10 13:40:13.286992548 +0800
[root@centos6_test tmp]#stat /root/test.txt 
  File: "/root/test.txt"
  Size: 0         	Blocks: 0          IO Block: 4096   普通空檔案
Device: fd00h/64768d	Inode: 793356      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-10-10 13:38:23.595005397 +0800
Modify: 2019-10-10 13:38:23.595005397 +0800
Change: 2019-10-10 13:38:23.595005397 +0800
[root@centos6_test tmp]#      
[root@centos6_test tmp]#ll /root/test.txt 
-rw-r--r--. 1 root root 0 10月 10 13:38 /root/test.txt
[root@centos6_test tmp]#date
2019年 10月 10日 星期四 13:53:13 CST
[root@centos6_test tmp]#touch -r /root/test.txt  ccc
[root@centos6_test tmp]#ll ccc 
-rw-r--r--. 1 root root 0 10月 10 13:38 ccc
[root@centos6_test tmp]#stat ccc
  File: "ccc"
  Size: 0         	Blocks: 0          IO Block: 4096   普通空檔案
Device: fd00h/64768d	Inode: 1316034     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-10-10 13:38:23.595005397 +0800
Modify: 2019-10-10 13:38:23.595005397 +0800
Change: 2019-10-10 13:53:31.925000824 +0800
[root@centos6_test tmp]#stat /root/test.txt 
  File: "/root/test.txt"
  Size: 0         	Blocks: 0          IO Block: 4096   普通空檔案
Device: fd00h/64768d	Inode: 793356      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-10-10 13:38:23.595005397 +0800
Modify: 2019-10-10 13:38:23.595005397 +0800
Change: 2019-10-10 13:38:23.595005397 +0800        
[root@centos6_test tmp]#stat xxx
  File: "xxx"
  Size: 0         	Blocks: 0          IO Block: 4096   普通空檔案
Device: fd00h/64768d	Inode: 1316024     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-10-10 13:28:57.982999292 +0800
Modify: 2019-10-10 13:28:38.382002766 +0800
Change: 2019-10-10 13:28:57.982999292 +0800
[root@centos6_test tmp]#touch -m xxx 
[root@centos6_test tmp]#stat xxx 
  File: "xxx"
  Size: 0         	Blocks: 0          IO Block: 4096   普通空檔案
Device: fd00h/64768d	Inode: 1316024     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-10-10 13:28:57.982999292 +0800
Modify: 2019-10-10 13:47:39.699998443 +0800
Change: 2019-10-10 13:47:39.699998443 +0800
[root@centos6_test tmp]#touch -t '201912121034.21' abc
[root@centos6_test tmp]#stat abc
  File: "abc"
  Size: 0         	Blocks: 0          IO Block: 4096   普通空檔案
Device: fd00h/64768d	Inode: 1316033     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-12-12 10:34:21.000000000 +0800
Modify: 2019-12-12 10:34:21.000000000 +0800
Change: 2019-10-10 13:48:39.258004301 +0800       
[root@centos6_test tmp]#date
2019年 10月 10日 星期四 14:02:27 CST
[root@centos6_test tmp]#stat abc
  File: "abc"
  Size: 0         	Blocks: 0          IO Block: 4096   普通空檔案
Device: fd00h/64768d	Inode: 1316033     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-10-10 14:00:52.147000137 +0800
Modify: 2019-12-12 10:34:21.000000000 +0800
Change: 2019-10-10 14:00:52.147000137 +0800
[root@centos6_test tmp]#touch --time=access abc
[root@centos6_test tmp]#stat abc
  File: "abc"
  Size: 0         	Blocks: 0          IO Block: 4096   普通空檔案
Device: fd00h/64768d	Inode: 1316033     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-10-10 14:02:49.266000652 +0800
Modify: 2019-12-12 10:34:21.000000000 +0800
Change: 2019-10-10 14:02:49.266000652 +0800
[root@centos6_test tmp]#touch --time=use abc
[root@centos6_test tmp]#stat abc
  File: "abc"
  Size: 0         	Blocks: 0          IO Block: 4096   普通空檔案
Device: fd00h/64768d	Inode: 1316033     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-10-10 14:03:14.579006906 +0800
Modify: 2019-12-12 10:34:21.000000000 +0800
Change: 2019-10-10 14:03:14.579006906 +0800
[root@centos6_test tmp]#stat xxx
  File: "xxx"
  Size: 0         	Blocks: 0          IO Block: 4096   普通空檔案
Device: fd00h/64768d	Inode: 1316024     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-10-10 14:01:12.818003178 +0800
Modify: 2019-10-10 13:59:38.505997486 +0800
Change: 2019-10-10 14:01:12.818003178 +0800
[root@centos6_test tmp]#touch --time=mtim xxx
[root@centos6_test tmp]#stat xxx
  File: "xxx"
  Size: 0         	Blocks: 0          IO Block: 4096   普通空檔案
Device: fd00h/64768d	Inode: 1316024     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-10-10 14:01:12.818003178 +0800
Modify: 2019-10-10 14:03:57.191991478 +0800
Change: 2019-10-10 14:03:57.191991478 +0800      

    mkdir:若指定目錄不存在則建立目錄。

      用法:mkdir  [選項]...  目錄名...

        -m, --mode=模式    設定權限模式(類似chmod),而不是rwxrwxrwx減umask

        -p, --parents      需要時建立目标目錄的上層目錄,但即使這些目錄已存在也不當錯誤處理

        -v, --verbose      每次建立目錄都顯示其過程資訊

        --help          顯示此幫助資訊并退出

        --version        顯示版本資訊并退出

      請注意,長選項必須使用的參數對于短選項時也是必須使用的。

[root@centos6_test tmp]#mkdir test
[root@centos6_test tmp]#ll
總用量 4
drwxr-xr-x. 2 root root 4096 10月 10 13:22 test
[root@centos6_test tmp]#mkdir -m 700 abc
[root@centos6_test tmp]#ll
總用量 8
drwx------. 2 root root 4096 10月 10 13:22 abc
drwxr-xr-x. 2 root root 4096 10月 10 13:22 test
[root@centos6_test tmp]#mkdir -p xxx/aaa/bbb/ccc
[root@centos6_test tmp]#tree
.
├── abc
├── test
└── xxx
    └── aaa
        └── bbb
            └── ccc

6 directories, 0 files
[root@centos6_test tmp]#mkdir -v ddd
mkdir: 已建立目錄 "ddd"
[root@centos6_test tmp]#mkdir -pv ddd/aaa/ccc/eee
mkdir: 已建立目錄 "ddd/aaa"
mkdir: 已建立目錄 "ddd/aaa/ccc"
mkdir: 已建立目錄 "ddd/aaa/ccc/eee"
[root@centos6_test tmp]#      

    重定向 >    >> 

[root@centos6_test tmp]#ls 
[root@centos6_test tmp]#> xxxj.sh
[root@centos6_test tmp]#ls
xxxj.sh
[root@centos6_test tmp]#echo xxx >abc.txt
[root@centos6_test tmp]#ls
abc.txt  xxxj.sh
[root@centos6_test tmp]#>> aa
[root@centos6_test tmp]#echo bbb >> bb
[root@centos6_test tmp]#ls
aa  abc.txt  bb  xxxj.sh
[root@centos6_test tmp]#cat >> aabbcc <<EOF
> 12424
> EOF
[root@centos6_test tmp]#ls
aa  aabbcc  abc.txt  bb  xxxj.sh
[root@centos6_test tmp]#ls
aa  aabbcc  abc.txt  bb  xxxj.sh
[root@centos6_test tmp]#cat > 12aa <<EOF
> sdlfjasjdfoaj
> EOF
[root@centos6_test tmp]#ls
12aa  aa  aabbcc  abc.txt  bb  xxxj.sh      

說明:echo "字元串" > 檔案  或者 echo "字元串" >> 檔案 是把字元串重定向到檔案裡,如果檔案不存在則建立,然後把字元串寫進檔案裡,兩者的差別是'>'是輸出重定向,它會清空原有檔案内容,然後再把字元串内容寫進檔案,而'>>'是追加輸出重定向,它不會清空原有的檔案内容,隻在原有檔案的内容最後把字元串内容寫進檔案。  > 檔案 表示清空檔案,如果檔案存在則清空,如果不存在則建立。>> 檔案 表示把空字元追加到檔案,如果檔案存在,則不改變檔案内容,如果檔案不存在,則建立。當然vim也是可以建立新檔案的,如果檔案存在則編輯檔案,不存在則建立空檔案,然後編輯檔案,但要注意vim不是說打開一個不存在的檔案就建立了一個空檔案,我們要儲存才會建立檔案,不儲存退出則不會建立新檔案。

  • 檔案或目錄的删除

    rmdir:删除指定的空目錄。

      用法:rmdir  [選項]...  目錄...

        --ignore-fail-on-non-empty       忽略僅由目錄非空産生的所有錯誤

        -p, --parents              删除指定目錄及其上級目錄,例如"rmdir -p a/b/c" 與"rmdir a/b/c a/b a"基本相同

        -v, --verbose              輸出處理的目錄詳情

        --help                  顯示此幫助資訊并退出

        --version                  顯示版本資訊并退出

[root@centos6_test tmp]#tree
.
├── aa
│   └── bb
│       └── cc
│           └── dd
├── abc
├── ccc
├── test.txt -> ../root/test.txt
└── xxx

4 directories, 4 files
[root@centos6_test tmp]#rmdir --ignore-fail-on-non-empty aa
[root@centos6_test tmp]#tree
.
├── aa
│   └── bb
│       └── cc
│           └── dd
├── abc
├── ccc
├── test.txt -> ../root/test.txt
└── xxx

4 directories, 4 files
[root@centos6_test tmp]#rmdir -p aa
rmdir: 删除 "aa" 失敗: 目錄非空
[root@centos6_test tmp]#rmdir -p aa/bb/cc/dd/
[root@centos6_test tmp]#tree
.
├── abc
├── ccc
├── test.txt -> ../root/test.txt
└── xxx

0 directories, 4 files
[root@centos6_test tmp]#      

    rm:删除(unlink)檔案

      用法:rm  [選項]...  檔案...

        -f,--force          強制删除。忽略不存在的檔案,不提示确認

        -i              在删除前需要确認

        -I(大寫i)           在删除超過三個檔案或者遞歸删除前要求确認。此選項比-i提示内容更少,但同樣可以阻止大多數錯誤發生

        --interactive[=WHEN]     根據指定WHEN進行确認提示:never,once(-I),或者always(-i)。如果此選項不加WHEN則總是提示

        --one-file-system      遞歸删除一個層級時,跳過所有不符合指令行參數的檔案

        --no-preserve-roo       不特殊對待"/"

        --preserve-root       不允許删除"/"(預設)

        -r, -R, --recursive      遞歸删除目錄及其内容

        -v,--verbose          詳細顯示進行的步驟

        --help             顯示此幫助資訊并退出

        --version           顯示版本資訊并退出

      預設時,rm不會删除目錄。使用--recursive(-r或-R)選項可删除每個給定的目錄,以及其下所有的内容。要删除第一個字元為"-"的檔案(例如"-foo"),請使用以下方法之一:

        rm -- -foo

        rm ./-foo  

[root@centos6_test tmp]#ll
總用量 4
drwxr-xr-x. 3 root root 4096 10月 10 14:54 aa
-rw-r--r--. 1 root root    0 10月 10 14:54 ab
-rw-r--r--. 1 root root    0 10月 10 14:54 ac
-rw-r--r--. 1 root root    0 10月 10 14:54 ba
-rw-r--r--. 1 root root    0 10月 10 14:54 bb
-rw-r--r--. 1 root root    0 10月 10 14:54 bc
-rw-r--r--. 1 root root    0 10月 10 14:54 ca
-rw-r--r--. 1 root root    0 10月 10 14:54 cb
-rw-r--r--. 1 root root    0 10月 10 13:38 ccc
lrwxrwxrwx. 1 root root   16 10月 10 13:40 test.txt -> ../root/test.txt
-rw-r--r--. 1 root root    0 10月 10 14:03 xxx
[root@centos6_test tmp]#rm -f ab ac ddddd
[root@centos6_test tmp]#ll
總用量 4
drwxr-xr-x. 3 root root 4096 10月 10 14:54 aa
-rw-r--r--. 1 root root    0 10月 10 14:54 ba
-rw-r--r--. 1 root root    0 10月 10 14:54 bb
-rw-r--r--. 1 root root    0 10月 10 14:54 bc
-rw-r--r--. 1 root root    0 10月 10 14:54 ca
-rw-r--r--. 1 root root    0 10月 10 14:54 cb
-rw-r--r--. 1 root root    0 10月 10 13:38 ccc
lrwxrwxrwx. 1 root root   16 10月 10 13:40 test.txt -> ../root/test.txt
-rw-r--r--. 1 root root    0 10月 10 14:03 xxx
[root@centos6_test tmp]#rm -i bb
rm:是否删除普通空檔案 "bb"?y
[root@centos6_test tmp]#ll
總用量 4
drwxr-xr-x. 3 root root 4096 10月 10 14:54 aa
-rw-r--r--. 1 root root    0 10月 10 14:54 ba
-rw-r--r--. 1 root root    0 10月 10 14:54 bc
-rw-r--r--. 1 root root    0 10月 10 14:54 ca
-rw-r--r--. 1 root root    0 10月 10 14:54 cb
-rw-r--r--. 1 root root    0 10月 10 13:38 ccc
lrwxrwxrwx. 1 root root   16 10月 10 13:40 test.txt -> ../root/test.txt
-rw-r--r--. 1 root root    0 10月 10 14:03 xxx
[root@centos6_test tmp]#rm -I ba bc ca 
[root@centos6_test tmp]#ll
總用量 4
drwxr-xr-x. 3 root root 4096 10月 10 14:54 aa
-rw-r--r--. 1 root root    0 10月 10 14:54 cb
-rw-r--r--. 1 root root    0 10月 10 13:38 ccc
lrwxrwxrwx. 1 root root   16 10月 10 13:40 test.txt -> ../root/test.txt
-rw-r--r--. 1 root root    0 10月 10 14:03 xxx
[root@centos6_test tmp]#rm -I cb ccc  xxx  test.txt 
rm:删除所有參數?y
[root@centos6_test tmp]#ll
總用量 4
drwxr-xr-x. 3 root root 4096 10月 10 14:54 aa
[root@centos6_test tmp]#tree 
.
└── aa
    └── bb
        └── cc
            └── dd

4 directories, 0 files
[root@centos6_test tmp]#rm -r aa
rm:是否進入目錄"aa"? n
[root@centos6_test tmp]#rm -rf aa
[root@centos6_test tmp]#tree
.

0 directories, 0 files
[root@centos6_test tmp]#touch {a..d}{a..d}
[root@centos6_test tmp]#ll
總用量 0
-rw-r--r--. 1 root root 0 10月 10 14:57 aa
-rw-r--r--. 1 root root 0 10月 10 14:57 ab
-rw-r--r--. 1 root root 0 10月 10 14:57 ac
-rw-r--r--. 1 root root 0 10月 10 14:57 ad
-rw-r--r--. 1 root root 0 10月 10 14:57 ba
-rw-r--r--. 1 root root 0 10月 10 14:57 bb
-rw-r--r--. 1 root root 0 10月 10 14:57 bc
-rw-r--r--. 1 root root 0 10月 10 14:57 bd
-rw-r--r--. 1 root root 0 10月 10 14:57 ca
-rw-r--r--. 1 root root 0 10月 10 14:57 cb
-rw-r--r--. 1 root root 0 10月 10 14:57 cc
-rw-r--r--. 1 root root 0 10月 10 14:57 cd
-rw-r--r--. 1 root root 0 10月 10 14:57 da
-rw-r--r--. 1 root root 0 10月 10 14:57 db
-rw-r--r--. 1 root root 0 10月 10 14:57 dc
-rw-r--r--. 1 root root 0 10月 10 14:57 dd
[root@centos6_test tmp]#rm --interactive=once *
rm:删除所有參數?y
[root@centos6_test tmp]#ll
總用量 0
[root@centos6_test tmp]#touch {a..d}
[root@centos6_test tmp]#ll
總用量 0
-rw-r--r--. 1 root root 0 10月 10 14:58 a
-rw-r--r--. 1 root root 0 10月 10 14:58 b
-rw-r--r--. 1 root root 0 10月 10 14:58 c
-rw-r--r--. 1 root root 0 10月 10 14:58 d
[root@centos6_test tmp]#rm --interactive=never *
[root@centos6_test tmp]#ll
總用量 0
[root@centos6_test tmp]#touch {a..f}
[root@centos6_test tmp]#ll
總用量 0
-rw-r--r--. 1 root root 0 10月 10 15:00 a
-rw-r--r--. 1 root root 0 10月 10 15:00 b
-rw-r--r--. 1 root root 0 10月 10 15:00 c
-rw-r--r--. 1 root root 0 10月 10 15:00 d
-rw-r--r--. 1 root root 0 10月 10 15:00 e
-rw-r--r--. 1 root root 0 10月 10 15:00 f
[root@centos6_test tmp]#rm -v *
rm:是否删除普通空檔案 "a"?y
已删除"a"
rm:是否删除普通空檔案 "b"?y
已删除"b"
rm:是否删除普通空檔案 "c"?y
已删除"c"
rm:是否删除普通空檔案 "d"?
rm:是否删除普通空檔案 "e"?
rm:是否删除普通空檔案 "f"?
[root@centos6_test tmp]#
[root@centos6_test tmp]#ll
總用量 0
-rw-r--r--. 1 root root 0 10月 10 15:00 d
-rw-r--r--. 1 root root 0 10月 10 15:00 e
-rw-r--r--. 1 root root 0 10月 10 15:00 f
[root@centos6_test tmp]#rm -fv *
已删除"d"
已删除"e"
已删除"f"
[root@centos6_test tmp]#touch -- -abc
[root@centos6_test tmp]#ll
總用量 0
-rw-r--r--. 1 root root 0 10月 10 15:03 -abc
[root@centos6_test tmp]#rm -f -abc 
rm:無效選項 -- a
嘗試使用"rm ./-abc"删除檔案"-abc"。
請嘗試執行"rm --help"來擷取更多資訊。
[root@centos6_test tmp]#rm -- -abc 
rm:是否删除普通空檔案 "-abc"?y
[root@centos6_test tmp]#ll
總用量 0
[root@centos6_test tmp]#touch -- -abc
[root@centos6_test tmp]#ll
總用量 0
-rw-r--r--. 1 root root 0 10月 10 15:03 -abc
[root@centos6_test tmp]#rm -f ./-abc 
[root@centos6_test tmp]#ll
總用量 0
[root@centos6_test tmp]#      
  • 檔案或目錄的修改

    rename:檔案重命名

      用法:rename  要修改的字元  修改成的字元  檔案...

      選項:無

常用示範:       

[root@centos6_test tmp]#touch {a..d}.txt
[root@centos6_test tmp]#ll
總用量 0
-rw-r--r--. 1 root root 0 10月 10 15:09 a.txt
-rw-r--r--. 1 root root 0 10月 10 15:09 b.txt
-rw-r--r--. 1 root root 0 10月 10 15:09 c.txt
-rw-r--r--. 1 root root 0 10月 10 15:09 d.txt
[root@centos6_test tmp]#rename .txt .sh *.txt
[root@centos6_test tmp]#ll
總用量 0
-rw-r--r--. 1 root root 0 10月 10 15:09 a.sh
-rw-r--r--. 1 root root 0 10月 10 15:09 b.sh
-rw-r--r--. 1 root root 0 10月 10 15:09 c.sh
-rw-r--r--. 1 root root 0 10月 10 15:09 d.sh
[root@centos6_test tmp]#rename a xx a.sh
[root@centos6_test tmp]#ll
總用量 0
-rw-r--r--. 1 root root 0 10月 10 15:09 b.sh
-rw-r--r--. 1 root root 0 10月 10 15:09 c.sh
-rw-r--r--. 1 root root 0 10月 10 15:09 d.sh
-rw-r--r--. 1 root root 0 10月 10 15:09 xx.sh
[root@centos6_test tmp]#      
[root@centos6_test tmp]#mkdir testdir
[root@centos6_test tmp]#ll
總用量 8
drwxr-xr-x. 2 root root 4096 10月 10 16:17 aaa
drwxr-xr-x. 2 root root 4096 10月 10 16:18 testdir
[root@centos6_test tmp]#rename test abcd testdir
[root@centos6_test tmp]#ll
總用量 8
drwxr-xr-x. 2 root root 4096 10月 10 16:17 aaa
drwxr-xr-x. 2 root root 4096 10月 10 16:18 abcddir
[root@centos6_test tmp]#      

說明:rename支援批量改檔案名,後面可以支援通配符

  • 檔案或目錄的移動

    mv:将源檔案重命名為目标檔案,或将源檔案移動至指定目錄。

      用法:mv  [選項]...  [-T] 源檔案  目标檔案

       或:mv  [選項]...  源檔案...  目錄

       或:mv  [選項]...  -t  目錄  源檔案...

        --backup[=CONTROL]        為每個已經存在的目标檔案建立備份

        -b                 類似--backup,但不接受參數

        -f,--force              覆寫前不詢問

        -i,--interactive           覆寫前詢問

        -n,--no-clobber           不覆寫已存在的檔案

      如果指定了-i、-f、-n中的多個,僅最後一個生效

        --strip-trailing-slashes 去掉每個源檔案參數尾部的斜線

        -S, --suffix=SUFFIX 替換常用的備份檔案字尾

        -t, --target-directory=DIRECTORY 将所有參數指定的源檔案或目錄移動至 指定目錄

        -T, --no-target-directory 将目标檔案視作普通檔案處理

        -u, --update 隻在源檔案檔案比目标檔案新,或目标檔案不存在時才進行移動

        -v, --verbose 詳細顯示進行的步驟

        --help 顯示此幫助資訊并退出

        --version 顯示版本資訊并退出

      備份檔案的字尾為"~",除非以--suffix 選項或是SIMPLE_BACKUP_SUFFIX環境變量指定。版本控制的方式可通過--backup 選項或VERSION_CONTROL 環境變量來選擇。

       以下是可用的變量值:

              none, off 不進行備份(即使使用了--backup 選項)

              numbered, t 備份檔案加上數字進行排序

              existing, nil 若有數字的備份檔案已經存在則使用數字,否則使用普通方式備份

              simple, never 永遠使用普通方式備份

[root@centos6_test tmp]#ll
總用量 8
-rw-r--r--  1 root root    0 10月 11 10:41 a
drwxr-xr-x. 2 root root 4096 10月 10 16:17 aaa
drwxr-xr-x. 2 root root 4096 10月 10 16:18 abcddir
-rw-r--r--  1 root root    0 10月 11 10:41 b
-rw-r--r--  1 root root    0 10月 11 10:41 c
-rw-r--r--  1 root root    0 10月 11 10:41 d
[root@centos6_test tmp]#mv --backup a d
mv:是否覆寫"d"? y
[root@centos6_test tmp]#ll
總用量 8
drwxr-xr-x. 2 root root 4096 10月 10 16:17 aaa
drwxr-xr-x. 2 root root 4096 10月 10 16:18 abcddir
-rw-r--r--  1 root root    0 10月 11 10:41 b
-rw-r--r--  1 root root    0 10月 11 10:41 c
-rw-r--r--  1 root root    0 10月 11 10:41 d
-rw-r--r--  1 root root    0 10月 11 10:41 d~
[root@centos6_test tmp]#mv -b b c
mv:是否覆寫"c"? y
[root@centos6_test tmp]#ll
總用量 8
drwxr-xr-x. 2 root root 4096 10月 10 16:17 aaa
drwxr-xr-x. 2 root root 4096 10月 10 16:18 abcddir
-rw-r--r--  1 root root    0 10月 11 10:41 c
-rw-r--r--  1 root root    0 10月 11 10:41 c~
-rw-r--r--  1 root root    0 10月 11 10:41 d
-rw-r--r--  1 root root    0 10月 11 10:41 d~
[root@centos6_test tmp]#mv -f c d
[root@centos6_test tmp]#ll
總用量 8
drwxr-xr-x. 2 root root 4096 10月 10 16:17 aaa
drwxr-xr-x. 2 root root 4096 10月 10 16:18 abcddir
-rw-r--r--  1 root root    0 10月 11 10:41 c~
-rw-r--r--  1 root root    0 10月 11 10:41 d
-rw-r--r--  1 root root    0 10月 11 10:41 d~
[root@centos6_test tmp]#mv -i c~ d
d   d~  
[root@centos6_test tmp]#mv -i c~ d~ 
mv:是否覆寫"d~"? y
[root@centos6_test tmp]#ll
總用量 8
drwxr-xr-x. 2 root root 4096 10月 10 16:17 aaa
drwxr-xr-x. 2 root root 4096 10月 10 16:18 abcddir
-rw-r--r--  1 root root    0 10月 11 10:41 d
-rw-r--r--  1 root root    0 10月 11 10:41 d~
[root@centos6_test tmp]#mv -n d d~
[root@centos6_test tmp]#ll
總用量 8
drwxr-xr-x. 2 root root 4096 10月 10 16:17 aaa
drwxr-xr-x. 2 root root 4096 10月 10 16:18 abcddir
-rw-r--r--  1 root root    0 10月 11 10:41 d
-rw-r--r--  1 root root    0 10月 11 10:41 d~
[root@centos6_test tmp]#mv -S .bak d d~
mv:是否覆寫"d~"? y 
[root@centos6_test tmp]#ll
總用量 8
drwxr-xr-x. 2 root root 4096 10月 10 16:17 aaa
drwxr-xr-x. 2 root root 4096 10月 10 16:18 abcddir
-rw-r--r--  1 root root    0 10月 11 10:41 d~
-rw-r--r--  1 root root    0 10月 11 10:41 d~.bak
[root@centos6_test tmp]#mv -t aaa/ d~ d~.bak 
[root@centos6_test tmp]#ll
總用量 8
drwxr-xr-x. 2 root root 4096 10月 11 10:46 aaa
drwxr-xr-x. 2 root root 4096 10月 10 16:18 abcddir
[root@centos6_test tmp]#ll aaa/
總用量 0
-rw-r--r-- 1 root root 0 10月 11 10:41 d~
-rw-r--r-- 1 root root 0 10月 11 10:41 d~.bak
      
[root@centos6_test tmp]#ll
總用量 8
drwxr-xr-x. 2 root root 4096 10月 11 10:48 aaa
-rw-r--r--  1 root root    0 10月 11 10:48 aac
-rw-r--r--  1 root root    0 10月 11 10:48 aad
-rw-r--r--  1 root root    0 10月 11 10:48 aae
-rw-r--r--  1 root root    0 10月 11 10:48 aaf
drwxr-xr-x. 2 root root 4096 10月 10 16:18 abcddir
[root@centos6_test tmp]#mv -T aac aaa/
mv:是否覆寫"aaa/"? y
mv: 無法以非目錄來覆寫目錄"aaa/"
[root@centos6_test tmp]#ll
總用量 8
drwxr-xr-x. 2 root root 4096 10月 11 10:48 aaa
-rw-r--r--  1 root root    0 10月 11 10:48 aac
-rw-r--r--  1 root root    0 10月 11 10:48 aad
-rw-r--r--  1 root root    0 10月 11 10:48 aae
-rw-r--r--  1 root root    0 10月 11 10:48 aaf
drwxr-xr-x. 2 root root 4096 10月 10 16:18 abcddir
[root@centos6_test tmp]#mv -T aac aad 
mv:是否覆寫"aad"? y
[root@centos6_test tmp]#ll
總用量 8
drwxr-xr-x. 2 root root 4096 10月 11 10:48 aaa
-rw-r--r--  1 root root    0 10月 11 10:48 aad
-rw-r--r--  1 root root    0 10月 11 10:48 aae
-rw-r--r--  1 root root    0 10月 11 10:48 aaf
drwxr-xr-x. 2 root root 4096 10月 10 16:18 abcddir
[root@centos6_test tmp]#mv -T aaa/ abcddir/
mv:是否覆寫"abcddir/"? y
[root@centos6_test tmp]#ll
總用量 4
-rw-r--r--  1 root root    0 10月 11 10:48 aad
-rw-r--r--  1 root root    0 10月 11 10:48 aae
-rw-r--r--  1 root root    0 10月 11 10:48 aaf
drwxr-xr-x. 2 root root 4096 10月 11 10:48 abcddir
[root@centos6_test tmp]#ll abcddir/
總用量 0
-rw-r--r-- 1 root root 0 10月 11 10:41 d~
-rw-r--r-- 1 root root 0 10月 11 10:41 d~.bak
[root@centos6_test tmp]#      
[root@centos6_test tmp]#echo aaa >> aa.txt
[root@centos6_test tmp]#echo bbb >> bb.txt
[root@centos6_test tmp]#ll
總用量 8
-rw-r--r-- 1 root root 4 10月 11 11:10 aa.txt
-rw-r--r-- 1 root root 4 10月 11 11:10 bb.txt
[root@centos6_test tmp]#mv -u aa.txt bb.txt 
[root@centos6_test tmp]#ll
總用量 8
-rw-r--r-- 1 root root 4 10月 11 11:10 aa.txt
-rw-r--r-- 1 root root 4 10月 11 11:10 bb.txt
[root@centos6_test tmp]#cat aa.txt 
aaa
[root@centos6_test tmp]#cat bb.txt 
bbb
[root@centos6_test tmp]#touch aa.txt 
[root@centos6_test tmp]#ll aa.txt 
-rw-r--r-- 1 root root 4 10月 11 11:11 aa.txt
[root@centos6_test tmp]#mv -u aa.txt bb.txt 
mv:是否覆寫"bb.txt"? y
[root@centos6_test tmp]#ll
總用量 4
-rw-r--r-- 1 root root 4 10月 11 11:11 bb.txt
[root@centos6_test tmp]#cat bb.txt 
aaa
[root@centos6_test tmp]#      
  • 檔案或目錄的複制

    cp:将源檔案複制至目标檔案,或将多個源檔案複制至目标目錄。

      用法:cp  [選項]... [-T] 源檔案...  目錄

         或:cp  [選項]... 源檔案 目标檔案

       或:cp  [選項]... -t 目錄  源檔案...

        -a, --archive 等于-dR --preserve=all

        --backup[=CONTROL]  為每個已存在的目标檔案建立備份

        -b 類似--backup 但不接受參數

        --copy-contents 在遞歸處理是複制特殊檔案内容

        -d 等于--no-dereference --preserve=links

        -f, --force 如果目标檔案無法打開則将其移除并重試(當 -n 選項存在時則不需再選此項)

        -i, --interactive 覆寫前詢問(使前面的 -n 選項失效)

        -H 跟随源檔案中的指令行符号連結

        -l, --link 連結檔案而不複制(建立硬連接配接)

        -L, --dereference 總是跟随符号連結

        -n, --no-clobber 不要覆寫已存在的檔案(使前面的 -i 選項失效)

        -P, --no-dereference 不跟随源檔案中的符号連結

        -p 等于--preserve=模式,所有權,時間戳

        --preserve[=屬性清單 保持指定的屬性(預設:模式,所有權,時間戳),如果可能保持附加屬性:環境、連結、xattr 等

        -c same as --preserve=context

        --sno-preserve=屬性清單 不保留指定的檔案屬性

        --parents 複制前在目标目錄建立來源檔案路徑中的所有目錄

        -R, -r, --recursive 遞歸複制目錄及其子目錄内的所有内容

        --reflink[=WHEN] 控制克隆/CoW 副本。請檢視下面的内如。

        --remove-destination 嘗試打開目标檔案前先删除已存在的目的地檔案 (相對于 --force 選項)

        --sparse=WHEN 控制建立稀疏檔案的方式

        --strip-trailing-slashes 删除參數中所有源檔案/目錄末端的斜杠

        -s, --symbolic-link 隻建立符号連結而不複制檔案

        -S, --suffix=字尾 自行指定備份檔案的字尾

        -t, --target-directory=目錄 将所有參數指定的源檔案/目錄複制至目标目錄

        -T, --no-target-directory 将目标目錄視作普通檔案

        -u, --update 當源檔案比目标檔案新時或目标檔案不存在時才複制

        -v, --verbose 顯示詳細進行步驟

        -x, --one-file-system stay on this file system

        -Z, --context=CONTEXT set security context of copy to CONTEXT

        --help 顯示此幫助資訊并退出

        --version 顯示版本資訊并退出

[root@centos6_test tmp]#echo aa > aa
[root@centos6_test tmp]#ll
總用量 4-rw-r--r-- 1 root root 3 10月 11 12:24 aa
[root@centos6_test tmp]#stat aa
  File: "aa"
  Size: 3         	Blocks: 8          IO Block: 4096   普通檔案
Device: fd00h/64768d	Inode: 1316023     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-10-11 12:24:06.980002423 +0800
Modify: 2019-10-11 12:24:06.980002423 +0800
Change: 2019-10-11 12:24:06.980002423 +0800
[root@centos6_test tmp]#cp -a aa bb
[root@centos6_test tmp]#ll
總用量 8
-rw-r--r-- 1 root root 3 10月 11 12:24 aa
-rw-r--r-- 1 root root 3 10月 11 12:24 bb
[root@centos6_test tmp]#stat bb
  File: "bb"
  Size: 3         	Blocks: 8          IO Block: 4096   普通檔案
Device: fd00h/64768d	Inode: 1316030     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-10-11 12:24:06.980002423 +0800
Modify: 2019-10-11 12:24:06.980002423 +0800
Change: 2019-10-11 12:24:21.380012785 +0800
[root@centos6_test tmp]#cat bb
aa
[root@centos6_test tmp]#cp --backup aa cc
[root@centos6_test tmp]#ll
總用量 12
-rw-r--r-- 1 root root 3 10月 11 12:24 aa
-rw-r--r-- 1 root root 3 10月 11 12:24 bb
-rw-r--r-- 1 root root 3 10月 11 12:25 cc
[root@centos6_test tmp]#cp --backup aa cc
cp:是否覆寫"cc"? y
[root@centos6_test tmp]#ll
總用量 16
-rw-r--r-- 1 root root 3 10月 11 12:24 aa
-rw-r--r-- 1 root root 3 10月 11 12:24 bb
-rw-r--r-- 1 root root 3 10月 11 12:25 cc
-rw-r--r-- 1 root root 3 10月 11 12:25 cc~
[root@centos6_test tmp]#cp --backup=numbered aa cc
cp:是否覆寫"cc"? y
[root@centos6_test tmp]#ll
總用量 20
-rw-r--r-- 1 root root 3 10月 11 12:24 aa
-rw-r--r-- 1 root root 3 10月 11 12:24 bb
-rw-r--r-- 1 root root 3 10月 11 12:25 cc
-rw-r--r-- 1 root root 3 10月 11 12:25 cc~
-rw-r--r-- 1 root root 3 10月 11 12:25 cc.~1~
[root@centos6_test tmp]#cp -b aa cc
cp:是否覆寫"cc"? y
[root@centos6_test tmp]#ll
總用量 24
-rw-r--r-- 1 root root 3 10月 11 12:24 aa
-rw-r--r-- 1 root root 3 10月 11 12:24 bb
-rw-r--r-- 1 root root 3 10月 11 12:25 cc
-rw-r--r-- 1 root root 3 10月 11 12:25 cc~
-rw-r--r-- 1 root root 3 10月 11 12:25 cc.~1~
-rw-r--r-- 1 root root 3 10月 11 12:25 cc.~2~
[root@centos6_test tmp]#ll
總用量 24
-rw-r--r-- 1 root root 3 10月 11 12:24 aa
-rw-r--r-- 1 root root 3 10月 11 12:24 bb
-rw-r--r-- 1 root root 3 10月 11 12:25 cc
-rw-r--r-- 1 root root 3 10月 11 12:25 cc~
-rw-r--r-- 1 root root 3 10月 11 12:25 cc.~1~
-rw-r--r-- 1 root root 3 10月 11 12:25 cc.~2~
[root@centos6_test tmp]#cp -d /etc/rc3.d/S01sysstat sys
[root@centos6_test tmp]#ll
總用量 24
-rw-r--r-- 1 root root  3 10月 11 12:24 aa
-rw-r--r-- 1 root root  3 10月 11 12:24 bb
-rw-r--r-- 1 root root  3 10月 11 12:25 cc
-rw-r--r-- 1 root root  3 10月 11 12:25 cc~
-rw-r--r-- 1 root root  3 10月 11 12:25 cc.~1~
-rw-r--r-- 1 root root  3 10月 11 12:25 cc.~2~
lrwxrwxrwx 1 root root 17 10月 11 12:30 sys -> ../init.d/sysstat
[root@centos6_test tmp]#cp -i aa bb
cp:是否覆寫"bb"? y
[root@centos6_test tmp]#cp -H /etc/rc3.d/S01sysstat xx
[root@centos6_test tmp]#ll
總用量 28
-rw-r--r-- 1 root root    3 10月 11 12:24 aa
-rw-r--r-- 1 root root    3 10月 11 12:32 bb
-rw-r--r-- 1 root root    3 10月 11 12:25 cc
-rw-r--r-- 1 root root    3 10月 11 12:25 cc~
-rw-r--r-- 1 root root    3 10月 11 12:25 cc.~1~
-rw-r--r-- 1 root root    3 10月 11 12:25 cc.~2~
lrwxrwxrwx 1 root root   17 10月 11 12:30 sys -> ../init.d/sysstat
-rwxr-xr-x 1 root root 1144 10月 11 12:33 xx
[root@centos6_test tmp]#ls -i /etc/fstab 
654087 /etc/fstab
[root@centos6_test tmp]#cp -l /etc/fstab .
[root@centos6_test tmp]#ls -i .
654087 fstab
[root@centos6_test tmp]#cp -L /etc/rc3.d/S01sysstat ddd
[root@centos6_test tmp]#ll
總用量 32
-rw-r--r--  1 root root    3 10月 11 12:24 aa
lrwxrwxrwx. 2 root root   17 9月  19 08:53 abc -> ../init.d/sysstat
-rw-r--r--  1 root root    3 10月 11 12:32 bb
-rw-r--r--  1 root root    3 10月 11 12:25 cc
-rw-r--r--  1 root root    3 10月 11 12:25 cc~
-rw-r--r--  1 root root    3 10月 11 12:25 cc.~1~
-rw-r--r--  1 root root    3 10月 11 12:25 cc.~2~
-rwxr-xr-x  1 root root 1144 10月 11 12:34 ddd
lrwxrwxrwx  1 root root   17 10月 11 12:30 sys -> ../init.d/sysstat
-rwxr-xr-x  1 root root 1144 10月 11 12:33 xx
[root@centos6_test tmp]#cp -n aa bb
[root@centos6_test tmp]#ll
總用量 32
-rw-r--r--  1 root root    3 10月 11 12:24 aa
lrwxrwxrwx. 2 root root   17 9月  19 08:53 abc -> ../init.d/sysstat
-rw-r--r--  1 root root    3 10月 11 12:32 bb
-rw-r--r--  1 root root    3 10月 11 12:25 cc
-rw-r--r--  1 root root    3 10月 11 12:25 cc~
-rw-r--r--  1 root root    3 10月 11 12:25 cc.~1~
-rw-r--r--  1 root root    3 10月 11 12:25 cc.~2~
-rwxr-xr-x  1 root root 1144 10月 11 12:34 ddd
lrwxrwxrwx  1 root root   17 10月 11 12:30 sys -> ../init.d/sysstat
-rwxr-xr-x  1 root root 1144 10月 11 12:33 xx
[root@centos6_test tmp]#cp -P /etc/rc3.d/S01sysstat xxx
[root@centos6_test tmp]#ll
總用量 32
-rw-r--r--  1 root root    3 10月 11 12:24 aa
lrwxrwxrwx. 2 root root   17 9月  19 08:53 abc -> ../init.d/sysstat
-rw-r--r--  1 root root    3 10月 11 12:32 bb
-rw-r--r--  1 root root    3 10月 11 12:25 cc
-rw-r--r--  1 root root    3 10月 11 12:25 cc~
-rw-r--r--  1 root root    3 10月 11 12:25 cc.~1~
-rw-r--r--  1 root root    3 10月 11 12:25 cc.~2~
-rwxr-xr-x  1 root root 1144 10月 11 12:34 ddd
lrwxrwxrwx  1 root root   17 10月 11 12:30 sys -> ../init.d/sysstat
-rwxr-xr-x  1 root root 1144 10月 11 12:33 xx
lrwxrwxrwx  1 root root   17 10月 11 12:35 xxx -> ../init.d/sysstat
[root@centos6_test tmp]#ls /boot/
config-2.6.32-696.el6.x86_64  initramfs-2.6.32-696.el6.x86_64.img    symvers-2.6.32-696.el6.x86_64.gz
efi                           initrd-2.6.32-696.el6.x86_64kdump.img  System.map-2.6.32-696.el6.x86_64
grub                          lost+found                             vmlinuz-2.6.32-696.el6.x86_64
[root@centos6_test tmp]#cp -r /boot/ boot
[root@centos6_test tmp]#ll
總用量 40
-rw-r--r--  1 root root    3 10月 11 12:24 aa
lrwxrwxrwx. 2 root root   17 9月  19 08:53 abc -> ../init.d/sysstat
-rw-r--r--  1 root root    3 10月 11 12:32 bb
dr-xr-xr-x  5 root root 4096 10月 11 12:38 boot
-rw-r--r--  1 root root    3 10月 11 12:25 cc
-rw-r--r--  1 root root    3 10月 11 12:25 cc~
-rw-r--r--  1 root root    3 10月 11 12:25 cc.~1~
-rw-r--r--  1 root root    3 10月 11 12:25 cc.~2~
-rwxr-xr-x  1 root root 1144 10月 11 12:34 ddd
-rwxr-xr-x  1 root root 1144 10月 11 12:36 fff
lrwxrwxrwx  1 root root   17 10月 11 12:30 sys -> ../init.d/sysstat
-rwxr-xr-x  1 root root 1144 10月 11 12:33 xx
lrwxrwxrwx  1 root root   17 10月 11 12:35 xxx -> ../init.d/sysstat
[root@centos6_test tmp]#ls boot
config-2.6.32-696.el6.x86_64  initramfs-2.6.32-696.el6.x86_64.img    symvers-2.6.32-696.el6.x86_64.gz
efi                           initrd-2.6.32-696.el6.x86_64kdump.img  System.map-2.6.32-696.el6.x86_64
grub                          lost+found                             vmlinuz-2.6.32-696.el6.x86_64
[root@centos6_test tmp]#ll /boot
總用量 38874
-rw-r--r--. 1 root root   108164 3月  22 2017 config-2.6.32-696.el6.x86_64
drwxr-xr-x. 3 root root     1024 9月  19 08:55 efi
drwxr-xr-x. 2 root root     1024 9月  19 09:06 grub
-rw-------. 1 root root 26688788 9月  19 08:58 initramfs-2.6.32-696.el6.x86_64.img
-rw-------. 1 root root  5871604 9月  19 09:07 initrd-2.6.32-696.el6.x86_64kdump.img
drwx------. 2 root root    12288 9月  19 08:44 lost+found
-rw-r--r--. 1 root root   215634 3月  22 2017 symvers-2.6.32-696.el6.x86_64.gz
-rw-r--r--. 1 root root  2622364 3月  22 2017 System.map-2.6.32-696.el6.x86_64
-rwxr-xr-x. 1 root root  4274992 3月  22 2017 vmlinuz-2.6.32-696.el6.x86_64
[root@centos6_test tmp]#cp -s ddd ddd.link
[root@centos6_test tmp]#ll
總用量 40
-rw-r--r--  1 root root    3 10月 11 12:24 aa
lrwxrwxrwx. 2 root root   17 9月  19 08:53 abc -> ../init.d/sysstat
-rw-r--r--  1 root root    3 10月 11 12:32 bb
dr-xr-xr-x  5 root root 4096 10月 11 12:38 boot
-rw-r--r--  1 root root    3 10月 11 12:25 cc
-rw-r--r--  1 root root    3 10月 11 12:25 cc~
-rw-r--r--  1 root root    3 10月 11 12:25 cc.~1~
-rw-r--r--  1 root root    3 10月 11 12:25 cc.~2~
-rwxr-xr-x  1 root root 1144 10月 11 12:34 ddd
lrwxrwxrwx  1 root root    3 10月 11 12:39 ddd.link -> ddd
-rwxr-xr-x  1 root root 1144 10月 11 12:36 fff
lrwxrwxrwx  1 root root   17 10月 11 12:30 sys -> ../init.d/sysstat
-rwxr-xr-x  1 root root 1144 10月 11 12:33 xx
lrwxrwxrwx  1 root root   17 10月 11 12:35 xxx -> ../init.d/sysstat
[root@centos6_test tmp]#cp -S .bak aa bb
cp:是否覆寫"bb"? y
[root@centos6_test tmp]#ll
總用量 44
-rw-r--r--  1 root root    3 10月 11 12:24 aa
lrwxrwxrwx. 2 root root   17 9月  19 08:53 abc -> ../init.d/sysstat
-rw-r--r--  1 root root    3 10月 11 12:39 bb
-rw-r--r--  1 root root    3 10月 11 12:32 bb.bak
dr-xr-xr-x  5 root root 4096 10月 11 12:38 boot
-rw-r--r--  1 root root    3 10月 11 12:25 cc
-rw-r--r--  1 root root    3 10月 11 12:25 cc~
-rw-r--r--  1 root root    3 10月 11 12:25 cc.~1~
-rw-r--r--  1 root root    3 10月 11 12:25 cc.~2~
-rwxr-xr-x  1 root root 1144 10月 11 12:34 ddd
lrwxrwxrwx  1 root root    3 10月 11 12:39 ddd.link -> ddd
-rwxr-xr-x  1 root root 1144 10月 11 12:36 fff
lrwxrwxrwx  1 root root   17 10月 11 12:30 sys -> ../init.d/sysstat
-rwxr-xr-x  1 root root 1144 10月 11 12:33 xx
lrwxrwxrwx  1 root root   17 10月 11 12:35 xxx -> ../init.d/sysstat
[root@centos6_test tmp]#mkdir test
[root@centos6_test tmp]#ll
總用量 48
-rw-r--r--  1 root root    3 10月 11 12:24 aa
lrwxrwxrwx. 2 root root   17 9月  19 08:53 abc -> ../init.d/sysstat
-rw-r--r--  1 root root    3 10月 11 12:39 bb
-rw-r--r--  1 root root    3 10月 11 12:32 bb.bak
dr-xr-xr-x  5 root root 4096 10月 11 12:38 boot
-rw-r--r--  1 root root    3 10月 11 12:25 cc
-rw-r--r--  1 root root    3 10月 11 12:25 cc~
-rw-r--r--  1 root root    3 10月 11 12:25 cc.~1~
-rw-r--r--  1 root root    3 10月 11 12:25 cc.~2~
-rwxr-xr-x  1 root root 1144 10月 11 12:34 ddd
lrwxrwxrwx  1 root root    3 10月 11 12:39 ddd.link -> ddd
-rwxr-xr-x  1 root root 1144 10月 11 12:36 fff
lrwxrwxrwx  1 root root   17 10月 11 12:30 sys -> ../init.d/sysstat
drwxr-xr-x  2 root root 4096 10月 11 12:41 test
-rwxr-xr-x  1 root root 1144 10月 11 12:33 xx
lrwxrwxrwx  1 root root   17 10月 11 12:35 xxx -> ../init.d/sysstat
[root@centos6_test tmp]#cp -t test aa bb bb.bak cc cc~ ddd fff xx
[root@centos6_test tmp]#ll test/
總用量 32
-rw-r--r-- 1 root root    3 10月 11 12:41 aa
-rw-r--r-- 1 root root    3 10月 11 12:41 bb
-rw-r--r-- 1 root root    3 10月 11 12:41 bb.bak
-rw-r--r-- 1 root root    3 10月 11 12:41 cc
-rw-r--r-- 1 root root    3 10月 11 12:41 cc~
-rwxr-xr-x 1 root root 1144 10月 11 12:41 ddd
-rwxr-xr-x 1 root root 1144 10月 11 12:41 fff
-rwxr-xr-x 1 root root 1144 10月 11 12:41 xx      

    install:将檔案複制至目标檔案,或将多個源檔案複制至目标目錄。

用法:

用法:install [選項]... [-T] 源檔案 目标檔案
 或:install [選項]... 源檔案... 目錄
 或:install [選項]... -t 目錄 源檔案...
 或:install [選項]... -d 目錄...

      
在前三種形式中,将源檔案複制到目标檔案或将多個源檔案複制到一個已存在的目錄
中同時設定其所有權和權限模式。在第四種形式中,建立給出目标目錄中的所有元件。      

選項:

長選項必須使用的參數對于短選項時也是必需使用的。
      --backup[=CONTROL]	為每個已存在的檔案建立備份
  -b			類似--backup 但不接受參數
  -c			(忽略)
  -C, --compare		比較每組源檔案和目标檔案,在一些情況下不修改目标檔案
  -d, --directory	将所有參數視為目錄名稱;為指定的目錄建立所有元件
  -D			建立目标目錄的所有必要的父目錄,然後将源檔案複制至該目錄
  -g, --group=組	自行設定所屬組,而不是程序目前的所屬組
  -m, --mode=模式	自行設定權限模式(像chmod),而不是rwxr-xr-x
  -o, --owner=所有者	自行設定所有者(隻适用于超級使用者)
  -p, --preserve-timestamps	修改源檔案的通路/修改時間以與目标檔案保持一緻
  -s, --strip			拆解符号表
      --strip-program=程式	指定拆解二進制檔案的程式
  -S, --suffix=字尾		覆寫常用備份檔案字尾
  -t, --target-directory=目錄	将源檔案所有參數複制到指定目錄
  -T, --no-target-directory	将目标檔案視為普通檔案
  -v, --verbose			建立目錄時顯示其名稱
  -P, --preserve-context (SELinux) preserve security context
  -Z, --context=CONTEXT  (SELinux) set security context of files and directories
      --help		顯示此幫助資訊并退出
      --version		顯示版本資訊并退出        

說明:install指令和cp指令類似,都可以将指定檔案或目錄拷貝到指定路徑,但install可以控制目标檔案的屬性。

[root@centos6_test tmp]#install /etc/fstab ./xx.tab
[root@centos6_test tmp]#ll
總用量 4
-rwxr-xr-x 1 root root 779 10月 11 13:30 xx.tab
[root@centos6_test tmp]#cat xx.tab 

#
# /etc/fstab
# Created by anaconda on Thu Sep 19 08:46:05 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/VolGroup-lv_root /                       ext4    defaults        1 1
UUID=3c68b9fa-be71-4498-a3c2-20583ef982ae /boot                   ext4    defaults        1 2
/dev/mapper/VolGroup-lv_swap swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
[root@centos6_test tmp]#install -d test
[root@centos6_test tmp]#ll
總用量 8
drwxr-xr-x 2 root root 4096 10月 11 13:31 test
-rwxr-xr-x 1 root root  779 10月 11 13:30 xx.tab
[root@centos6_test tmp]#install -t test/ /etc/fstab xx.tab /etc/issue.net
[root@centos6_test tmp]#ll test/
總用量 8
-rwxr-xr-x 1 root root 779 10月 11 13:32 fstab
-rwxr-xr-x 1 root root   0 10月 11 13:32 issue.net
-rwxr-xr-x 1 root root 779 10月 11 13:32 xx.tab
[root@centos6_test tmp]#install -g 'qiuhom' -o 'qiuhom' -m 600 xx.tab aaa
[root@centos6_test tmp]#ll
總用量 12
-rw------- 1 qiuhom qiuhom  779 10月 11 13:34 aaa
drwxr-xr-x 2 root   root   4096 10月 11 13:32 test
-rwxr-xr-x 1 root   root    779 10月 11 13:30 xx.tab
[root@centos6_test tmp]#      
  • 切換目錄

    cd:change directory 切換目錄

      用法:cd 目錄名

cd . 進入目前目錄

cd .. 進入上一級目錄

cd - 進入上一次所在目錄

cd ~ 進入目前使用者家目錄

cd ~USERName 進入到指定使用者家目錄

cd 不加任何内容 進到目前使用者家目錄

[root@centos6_test rc3.d]#cd .
[root@centos6_test rc3.d]#cd ..
[root@centos6_test etc]#cd -
/etc/rc3.d[root@centos6_test init.d]#pwd
/etc/init.d
[root@centos6_test init.d]#cd ~qiuhom
[root@centos6_test qiuhom]#pwd
/home/qiuhom
[root@centos6_test qiuhom]#cd ~
[root@centos6_test ~]#pwd
/root
[root@centos6_test ~]#cd /etc/init.d/
[root@centos6_test init.d]#cd
[root@centos6_test ~]#pwd
/root
[root@centos6_test ~]#      
  • 檔案或目錄的檢視

     ls:列出檔案的資訊(預設是目前目錄)。

用法:ls [選項]... [檔案]...      
長選項必須使用的參數對于短選項時也是必需使用的。
  -a, --all			不隐藏任何以. 開始的項目
  -A, --almost-all		列出除. 及.. 以外的任何項目
      --author			與-l 同時使用時列出每個檔案的作者
  -b, --escape			以八進制溢出序清單示不可列印的字元
      --block-size=大小		塊以指定大小的位元組為機關
  -B, --ignore-backups		不列出任何以"~"字元結束的項目
  -c				配合-lt:根據ctime 排序并顯示ctime(檔案
				狀态最後更改的時間)
				配合-l:顯示ctime 但根據名稱排序				其他情況:按ctime 排序
  -C				每欄由上至下列出項目
      --color[=WHEN]		控制是否使用色彩分辨檔案。WHEN 可以是
				"never"(預設)、"always"或"auto"其中之一
  -d, --directory		當遇到目錄時列出目錄本身而非目錄内的檔案
  -D, --dired			産生适合Emacs 的dired 模式使用的結果
  -f				不進行排序,-aU 選項生效,-lst 選項失效
  -F, --classify		加上檔案類型的訓示符号(*/=@| 其中一個)
      --format=關鍵字		交錯-x,逗号分隔-m,水準-x,長-l,
				單欄-1,詳細-l,垂直-C
      --full-time		即-l --time-style=full-iso
  -g				類似-l,但不列出所有者
      --group-directories-first
			在檔案前分組目錄。此選項可與--sort 一起使用,
			但是一旦使用--sort=none (-U)将禁用分組
  -G, --no-group		以一個長清單的形式,不輸出組名
  -h, --human-readable		與-l 一起,以易于閱讀的格式輸出檔案大小
				(例如 1K 234M 2G)
      --si			同上面類似,但是使用1000 為基底而非1024
  -H, --dereference-command-line
				跟随指令行列出的符号連結
      --dereference-command-line-symlink-to-dir
				跟随指令行列出的目錄的符号連結
      --hide=PATTERN         	隐藏符合PATTERN 模式的項目
				(-a 或 -A 将覆寫此選項)
      --indicator-style=方式	指定在每個項目名稱後加上訓示符号方式:
				none (預設),classify (-F),file-type (-p)
  -i, --inode			顯示每個檔案的inode 号
  -I, --ignore=PATTERN		不顯示任何符合指定shell PATTERN 的項目
  -k				即--block-size=1K
  -l				使用較長格式列出資訊
  -L, --dereference		當顯示符号連結的檔案資訊時,顯示符号連結所訓示
				的對象而并非符号連結本身的資訊
  -m				所有項目以逗号分隔,并填滿整行行寬
  -n, --numeric-uid-gid		類似 -l,但列出UID 及GID 号
  -N, --literal			輸出未經處理的項目名稱 (如不特别處理控制字元)
  -o				類似 -l,但不列出有關組的資訊
  -p,  --indicator-style=slash	對目錄加上表示符号"/"
  -q, --hide-control-chars	以"?"字元代替無法列印的字元
      --show-control-chars	直接顯示無法列印的字元 (這是預設方式,除非調用
				的程式名稱是"ls"而且是在終端輸出結果)
  -Q, --quote-name		将條目名稱括上雙引号
      --quoting-style=方式	使用指定的quoting 方式顯示條目的名稱:
				literal、locale、shell、shell-always、c、escape
  -r, --reverse			排序時保留順序
  -R, --recursive		遞歸顯示子目錄
  -s, --size			以塊數形式顯示每個檔案配置設定的尺寸
  -S				根據檔案大小排序
      --sort=WORD		以下是可選用的WORD 和它們代表的相應選項:
				extension -X       status   -c
				none      -U       time     -t
				size      -S       atime    -u
				time      -t       access   -u
				version   -v       use      -u
      --time=WORD		和-l 同時使用時顯示WORD 所代表的時間而非修改時
				間:atime、access、use、ctime 或status;加上
				--sort=time 選項時會以指定時間作為排序關鍵字
      --time-style=STYLE	和-l 同時使用時根據STYLE 代表的格式顯示時間:
				full-iso、iso、locale、posix-iso、+FORMAT。
				FORMAT 即是"date"所用的時間格式;如果FORMAT
				是FORMAT1<換行>FORMAT2,FORMAT1 适用于較舊
				的檔案而FORMAT2 适用于較新的檔案;如果STYLE
				以"posix-"開頭,則STYLE 僅在POSIX 語系之外
				生效。
  -t				根據修改時間排序
  -T, --tabsize=寬度	指定制表符(Tab)的寬度,而非8 個字元
  -t			按修改時間排序
  -T, --tabsize=COLS	指定制表符(Tab)的寬度,而非8個字元
  -u			同-lt 一起使用:按照通路時間排序并顯示
			同-l一起使用:顯示通路時間并按檔案名排序
			其他:按照通路時間排序
  -U			不進行排序;按照目錄順序列出項目
  -v			在文本中進行數字(版本)的自然排序
  -w, --width=COLS           assume screen width instead of current value
  -x                         list entries by lines instead of by columns
  -X                         sort alphabetically by entry extension
  -1                         list one file per line      
[root@centos6_test /]#ls
bin   dev  home  lib64       media  mnt  opt   root  selinux  sys  usr
boot  etc  lib   lost+found  misc   net  proc  sbin  srv      tmp  var
[root@centos6_test /]#ls -l
總用量 102
dr-xr-xr-x.   2 root root  4096 9月  19 08:56 bin
dr-xr-xr-x.   5 root root  1024 9月  19 09:05 boot
drwxr-xr-x   21 root root  3800 10月 11 10:14 dev
drwxr-xr-x. 119 root root 12288 10月 11 13:38 etc
drwxr-xr-x.   3 root root  4096 10月  1 14:39 home
dr-xr-xr-x.  11 root root  4096 9月  19 08:52 lib
dr-xr-xr-x.   9 root root 12288 9月  19 08:52 lib64
drwx------.   2 root root 16384 9月  19 08:44 lost+found
drwxr-xr-x.   2 root root  4096 9月  23 2011 media
drwxr-xr-x    2 root root     0 10月 11 10:14 misc
drwxr-xr-x.   2 root root  4096 9月  23 2011 mnt
drwxr-xr-x    2 root root     0 10月 11 10:14 net
drwxr-xr-x.   3 root root  4096 9月  19 08:55 opt
dr-xr-xr-x  136 root root     0 10月 11 2019 proc
dr-xr-x---.  16 root root  4096 10月 11 13:54 root
dr-xr-xr-x.   2 root root 12288 9月  19 08:56 sbin
drwxr-xr-x.   2 root root  4096 9月  19 08:46 selinux
drwxr-xr-x.   2 root root  4096 9月  23 2011 srv
drwxr-xr-x   13 root root     0 10月 11 2019 sys
drwxrwxrwt.   5 root root  4096 10月 11 13:39 tmp
drwxr-xr-x.  13 root root  4096 9月  19 08:46 usr
drwxr-xr-x.  22 root root  4096 9月  19 08:55 var
[root@centos6_test /]#ls -a
.          .autorelabel  .dbus  home   lost+found  mnt  proc  selinux  tmp
..         bin           dev    lib    media       net  root  srv      usr
.autofsck  boot          etc    lib64  misc        opt  sbin  sys      var
[root@centos6_test /]#ls -A
.autofsck     bin   .dbus  etc   lib    lost+found  misc  net  proc  sbin     srv  tmp  var
.autorelabel  boot  dev    home  lib64  media       mnt   opt  root  selinux  sys  usr
[root@centos6_test /]#ls -G
bin   dev  home  lib64       media  mnt  opt   root  selinux  sys  usr
boot  etc  lib   lost+found  misc   net  proc  sbin  srv      tmp  var
[root@centos6_test /]#ls -lG
總用量 102
dr-xr-xr-x.   2 root  4096 9月  19 08:56 bin
dr-xr-xr-x.   5 root  1024 9月  19 09:05 boot
drwxr-xr-x   21 root  3800 10月 11 10:14 dev
drwxr-xr-x. 119 root 12288 10月 11 13:38 etc
drwxr-xr-x.   3 root  4096 10月  1 14:39 home
dr-xr-xr-x.  11 root  4096 9月  19 08:52 lib
dr-xr-xr-x.   9 root 12288 9月  19 08:52 lib64
drwx------.   2 root 16384 9月  19 08:44 lost+found
drwxr-xr-x.   2 root  4096 9月  23 2011 media
drwxr-xr-x    2 root     0 10月 11 10:14 misc
drwxr-xr-x.   2 root  4096 9月  23 2011 mnt
drwxr-xr-x    2 root     0 10月 11 10:14 net
drwxr-xr-x.   3 root  4096 9月  19 08:55 opt
dr-xr-xr-x  136 root     0 10月 11 2019 proc
dr-xr-x---.  16 root  4096 10月 11 13:54 root
dr-xr-xr-x.   2 root 12288 9月  19 08:56 sbin
drwxr-xr-x.   2 root  4096 9月  19 08:46 selinux
drwxr-xr-x.   2 root  4096 9月  23 2011 srv
drwxr-xr-x   13 root     0 10月 11 2019 sys
drwxrwxrwt.   5 root  4096 10月 11 13:39 tmp
drwxr-xr-x.  13 root  4096 9月  19 08:46 usr
drwxr-xr-x.  22 root  4096 9月  19 08:55 var
[root@centos6_test /]#ls -lh
總用量 102K
dr-xr-xr-x.   2 root root 4.0K 9月  19 08:56 bin
dr-xr-xr-x.   5 root root 1.0K 9月  19 09:05 boot
drwxr-xr-x   21 root root 3.8K 10月 11 10:14 dev
drwxr-xr-x. 119 root root  12K 10月 11 13:38 etc
drwxr-xr-x.   3 root root 4.0K 10月  1 14:39 home
dr-xr-xr-x.  11 root root 4.0K 9月  19 08:52 lib
dr-xr-xr-x.   9 root root  12K 9月  19 08:52 lib64
drwx------.   2 root root  16K 9月  19 08:44 lost+found
drwxr-xr-x.   2 root root 4.0K 9月  23 2011 media
drwxr-xr-x    2 root root    0 10月 11 10:14 misc
drwxr-xr-x.   2 root root 4.0K 9月  23 2011 mnt
drwxr-xr-x    2 root root    0 10月 11 10:14 net
drwxr-xr-x.   3 root root 4.0K 9月  19 08:55 opt
dr-xr-xr-x  136 root root    0 10月 11 2019 proc
dr-xr-x---.  16 root root 4.0K 10月 11 13:54 root
dr-xr-xr-x.   2 root root  12K 9月  19 08:56 sbin
drwxr-xr-x.   2 root root 4.0K 9月  19 08:46 selinux
drwxr-xr-x.   2 root root 4.0K 9月  23 2011 srv
drwxr-xr-x   13 root root    0 10月 11 2019 sys
drwxrwxrwt.   5 root root 4.0K 10月 11 13:39 tmp
drwxr-xr-x.  13 root root 4.0K 9月  19 08:46 usr
drwxr-xr-x.  22 root root 4.0K 9月  19 08:55 var
[root@centos6_test /]#ls -ls
總用量 102
 4 dr-xr-xr-x.   2 root root  4096 9月  19 08:56 bin
 2 dr-xr-xr-x.   5 root root  1024 9月  19 09:05 boot
 0 drwxr-xr-x   21 root root  3800 10月 11 10:14 dev
12 drwxr-xr-x. 119 root root 12288 10月 11 13:38 etc
 4 drwxr-xr-x.   3 root root  4096 10月  1 14:39 home
 4 dr-xr-xr-x.  11 root root  4096 9月  19 08:52 lib
12 dr-xr-xr-x.   9 root root 12288 9月  19 08:52 lib64
16 drwx------.   2 root root 16384 9月  19 08:44 lost+found
 4 drwxr-xr-x.   2 root root  4096 9月  23 2011 media
 0 drwxr-xr-x    2 root root     0 10月 11 10:14 misc
 4 drwxr-xr-x.   2 root root  4096 9月  23 2011 mnt
 0 drwxr-xr-x    2 root root     0 10月 11 10:14 net
 4 drwxr-xr-x.   3 root root  4096 9月  19 08:55 opt
 0 dr-xr-xr-x  136 root root     0 10月 11 2019 proc
 4 dr-xr-x---.  16 root root  4096 10月 11 13:54 root
12 dr-xr-xr-x.   2 root root 12288 9月  19 08:56 sbin
 4 drwxr-xr-x.   2 root root  4096 9月  19 08:46 selinux
 4 drwxr-xr-x.   2 root root  4096 9月  23 2011 srv
 0 drwxr-xr-x   13 root root     0 10月 11 2019 sys
 4 drwxrwxrwt.   5 root root  4096 10月 11 13:39 tmp
 4 drwxr-xr-x.  13 root root  4096 9月  19 08:46 usr
 4 drwxr-xr-x.  22 root root  4096 9月  19 08:55 var
[root@centos6_test /]#ls -lsh
總用量 102K
4.0K dr-xr-xr-x.   2 root root 4.0K 9月  19 08:56 bin
2.0K dr-xr-xr-x.   5 root root 1.0K 9月  19 09:05 boot
   0 drwxr-xr-x   21 root root 3.8K 10月 11 10:14 dev
 12K drwxr-xr-x. 119 root root  12K 10月 11 13:38 etc
4.0K drwxr-xr-x.   3 root root 4.0K 10月  1 14:39 home
4.0K dr-xr-xr-x.  11 root root 4.0K 9月  19 08:52 lib
 12K dr-xr-xr-x.   9 root root  12K 9月  19 08:52 lib64
 16K drwx------.   2 root root  16K 9月  19 08:44 lost+found
4.0K drwxr-xr-x.   2 root root 4.0K 9月  23 2011 media
   0 drwxr-xr-x    2 root root    0 10月 11 10:14 misc
4.0K drwxr-xr-x.   2 root root 4.0K 9月  23 2011 mnt
   0 drwxr-xr-x    2 root root    0 10月 11 10:14 net
4.0K drwxr-xr-x.   3 root root 4.0K 9月  19 08:55 opt
   0 dr-xr-xr-x  136 root root    0 10月 11 2019 proc
4.0K dr-xr-x---.  16 root root 4.0K 10月 11 13:54 root
 12K dr-xr-xr-x.   2 root root  12K 9月  19 08:56 sbin
4.0K drwxr-xr-x.   2 root root 4.0K 9月  19 08:46 selinux
4.0K drwxr-xr-x.   2 root root 4.0K 9月  23 2011 srv
   0 drwxr-xr-x   13 root root    0 10月 11 2019 sys
4.0K drwxrwxrwt.   5 root root 4.0K 10月 11 13:39 tmp
4.0K drwxr-xr-x.  13 root root 4.0K 9月  19 08:46 usr
4.0K drwxr-xr-x.  22 root root 4.0K 9月  19 08:55 var
[root@centos6_test ~]#cd /tmp/
[root@centos6_test tmp]#ls -lR
.:
總用量 12
-rw------- 1 qiuhom qiuhom    4 10月 11 13:57 aaa
drwxr-xr-x 2 root   root   4096 10月 11 13:32 test
-rwxr-xr-x 1 root   root    779 10月 11 13:30 xx.tab

./test:
總用量 8
-rwxr-xr-x 1 root root 779 10月 11 13:32 fstab
-rwxr-xr-x 1 root root   0 10月 11 13:32 issue.net
-rwxr-xr-x 1 root root 779 10月 11 13:32 xx.tab
[root@centos6_test tmp]#ls -lr
總用量 12
-rwxr-xr-x 1 root   root    779 10月 11 13:30 xx.tab
drwxr-xr-x 2 root   root   4096 10月 11 13:32 test
-rw------- 1 qiuhom qiuhom    4 10月 11 13:57 aaa
[root@centos6_test tmp]#ls -lS
總用量 12
drwxr-xr-x 2 root   root   4096 10月 11 13:32 test
-rwxr-xr-x 1 root   root    779 10月 11 13:30 xx.tab
-rw------- 1 qiuhom qiuhom    4 10月 11 13:57 aaa
[root@centos6_test tmp]#ls -i
1316038 aaa  1316024 test  1316023 xx.tab
[root@centos6_test tmp]#       

    cat:将檔案或标準輸入組合輸出到标準輸出。

用法:cat [選項]... [檔案]...      
-A, --show-all           等于-vET
  -b, --number-nonblank    對非空輸出行編号
  -e                       等于-vE
  -E, --show-ends          在每行結束處顯示"$"
  -n, --number             對輸出的所有行編号
  -s, --squeeze-blank      不輸出多行空行
  -t                       與-vT 等價
  -T, --show-tabs          将跳格字元顯示為^I
  -u                       (被忽略)
  -v, --show-nonprinting   使用^ 和M- 引用,除了LFD和 TAB 之外
      --help		顯示此幫助資訊并退出
      --version		顯示版本資訊并退出
      
[root@centos6_test tmp]#cat xx.tab 
     
				
		    #


# /etc/fstab
# Created by anaconda on Thu Sep 19 08:46:05 2019
	
     	     	   	

#
# Accessible filesystems, b
# y reference, are maint
# ained under '/dev/disk'
[root@centos6_test tmp]#cat -A xx.tab 
     $
^I^I^I^I$
^I^I    #$
$
$
# /etc/fstab$
# Created by anaconda on Thu Sep 19 08:46:05 2019$
^I$
     ^I     ^I   ^I$
$
#$
# Accessible filesystems, b$
# y reference, are maint$
# ained under '/dev/disk'$
[root@centos6_test tmp]#cat -e xx.tab 
     $
				$
		    #$
$
$
# /etc/fstab$
# Created by anaconda on Thu Sep 19 08:46:05 2019$
	$
     	     	   	$
$
#$
# Accessible filesystems, b$
# y reference, are maint$
# ained under '/dev/disk'$
[root@centos6_test tmp]#cat -n xx.tab 
     1	     
     2					
     3			    #
     4	
     5	
     6	# /etc/fstab
     7	# Created by anaconda on Thu Sep 19 08:46:05 2019
     8		
     9	     	     	   	
    10	
    11	#
    12	# Accessible filesystems, b
    13	# y reference, are maint
    14	# ained under '/dev/disk'
[root@centos6_test tmp]#cat -b xx.tab 
     1	     
     2					
     3			    #


     4	# /etc/fstab
     5	# Created by anaconda on Thu Sep 19 08:46:05 2019
     6		
     7	     	     	   	

     8	#
     9	# Accessible filesystems, b
    10	# y reference, are maint
    11	# ained under '/dev/disk'
[root@centos6_test tmp]#cat -T xx.tab 
     
^I^I^I^I
^I^I    #


# /etc/fstab
# Created by anaconda on Thu Sep 19 08:46:05 2019
^I
     ^I     ^I   ^I

#
# Accessible filesystems, b
# y reference, are maint
# ained under '/dev/disk'      

cat除了上面的用法還有一個比較常用的用法,就是利用cat的标準輸出,重定向到檔案如下:

[root@centos6_test tmp]#cat >> test << EOF
> aaaaaa
> gggggg
> bbbbbb
> cccccc
> dddddd
> EOF
[root@centos6_test tmp]#cat test 
aaaaaa
gggggg
bbbbbb
cccccc
dddddd
[root@centos6_test tmp]#      

說明:上面這種用法需要注意EOF結束的時候前後不能有任何字元,包括空格。

    tac:将指定每個檔案按行倒置并寫到标準輸出,如果不指定檔案,或檔案為"-",則從标準輸入讀取資料。

用法:tac [選項]... [檔案]...      
長選項必須使用的參數對于短選項時也是必需使用的。
  -b, --before			在行前而非行尾添加分隔标志
  -r, --regex			将分隔标志視作正規表達式來解析
  -s, --separator=字元串	使用指定字元串代替換行作為分隔标志
      --help		顯示此幫助資訊并退出
      --version		顯示版本資訊并退出      
[root@centos6_test tmp]#cat test 
1111111
2222222
3333333
4444444
5555555
[root@centos6_test tmp]#tac test 
5555555
4444444
3333333
2222222
1111111      

    more

usage: more [-dflpcsu] [+linenum | +/pattern] name1 name2 ...      
-num 一次顯示的行數
-d 提示使用者,在畫面下方顯示 [Press space to continue, 'q' to quit.] ,如果使用者按錯鍵,則會顯示 [Press 'h' for instructions.] 而不是 '哔' 聲
-l 取消遇見特殊字元 ^L(送紙字元)時會暫停的功能
-f 計算行數時,以實際上的行數,而非自動換行過後的行數(有些單行字數太長的會被擴充為兩行或兩行以上)
-p 不以卷動的方式顯示每一頁,而是先清除螢幕後再顯示内容
-c 跟 -p 相似,不同的是先顯示内容再清除其他舊資料
-s 當遇到有連續兩行以上的空白行,就代換為一行的空白行
-u 不顯示下引号 (根據環境變數 TERM 指定的 terminal 而有所不同)
+/pattern 在每個文檔顯示前搜尋該字串(pattern),然後從該字串之後開始顯示
+num 從第 num 行開始顯示
fileNames 欲顯示内容的文檔,可為複數個數
      

常用操作:

Enter 向下n行,需要定義。預設為1行
Ctrl+F 向下滾動一屏
空格鍵 向下滾動一屏
Ctrl+B 傳回上一屏
= 輸出目前行的行号
:f 輸出檔案名和目前行的行号
V 調用vi編輯器
!指令 調用Shell,并執行指令
q 退出more      
[root@centos6_test tmp]#cat fstab 





#
# /etc/fstab
# Created by anaconda on Thu Sep 19 08:46:05 2019




#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/VolGroup-lv_root /                       ext4    defaults        1 1
UUID=3c68b9fa-be71-4498-a3c2-20583ef982ae /boot                   ext4    defaults        1 2
/dev/mapper/VolGroup-lv_swap swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
[root@centos6_test tmp]#more -s fstab 

#
# /etc/fstab
# Created by anaconda on Thu Sep 19 08:46:05 2019

#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/VolGroup-lv_root /                       ext4    defaults        1 1
UUID=3c68b9fa-be71-4498-a3c2-20583ef982ae /boot                   ext4    defaults        1 2
/dev/mapper/VolGroup-lv_swap swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
[root@centos6_test tmp]#more +15 fstab 
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/VolGroup-lv_root /                       ext4    defaults        1 1
UUID=3c68b9fa-be71-4498-a3c2-20583ef982ae /boot                   ext4    defaults        1 2
/dev/mapper/VolGroup-lv_swap swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
[root@centos6_test tmp]#      

    less:分頁顯示檔案内容,或分頁顯示标準輸出。

less [參數] 檔案       
-b <緩沖區大小> 設定緩沖區的大小
-e 當檔案顯示結束後,自動離開
-f 強迫打開特殊檔案,例如外圍裝置代号、目錄和二進制檔案
-g 隻标志最後搜尋的關鍵詞
-i 忽略搜尋時的大小寫
-m 顯示類似more指令的百分比
-N 顯示每行的行号
-o <檔案名> 将less 輸出的内容在指定檔案中儲存起來
-Q 不使用警告音
-s 顯示連續空行為一行
-S 行過長時間将超出部分舍棄
-x <數字> 将"tab"鍵顯示為規定的數字
      
/字元串:向下搜尋"字元串"的功能
?字元串:向上搜尋"字元串"的功能
n:重複前一個搜尋(與 / 或 ? 有關)
N:反向重複前一個搜尋(與 / 或 ? 有關)
b 向後翻一頁
d 向後翻半頁
h 顯示幫助界面
Q 退出less 指令
u 向前滾動半頁
y 向前滾動一行
空格鍵 滾動一頁
Enter鍵 滾動一行
[pagedown]: 向下翻動一頁
[pageup]: 向上翻動一頁      
全屏導航

ctrl + F - 向前移動一屏
ctrl + B - 向後移動一屏
ctrl + D - 向前移動半屏
ctrl + U - 向後移動半屏
2.單行導航

j - 向前移動一行
k - 向後移動一行
3.其它導航

G - 移動到最後一行
g - 移動到第一行
q / ZZ - 退出 less 指令
4.其它有用的指令

v - 使用配置的編輯器編輯目前檔案
h - 顯示 less 的幫助文檔
&pattern - 僅顯示比對模式的行,而不是整個檔案      

1、檢視檔案

less /etc/fstab
      

2、指令輸出結果太多可以用less分頁顯示

ps -ef |less
      

3、浏覽多個檔案

less /etc/passwd /etc/fstab /etc/inittab      

    head:将指定的檔案的頭10行顯示到标準輸出。

head [選項]... [檔案]...      
長選項必須使用的參數對于短選項時也是必需使用的。
  -c,  --bytes=[-]K	顯示每個檔案的前K 位元組内容;
			如果附加"-"參數,則除了每個檔案的最後K位元組資料外
			顯示剩餘全部内容
  -n, --lines=[-]K	顯示每個檔案的前K 行内容;
			如果附加"-"參數,則除了每個檔案的最後K 行外顯示
			剩餘全部内容
  -q, --quiet, --silent	不顯示包含給定檔案名的檔案頭
  -v, --verbose		總是顯示包含給定檔案名的檔案頭
      --help		顯示此幫助資訊并退出
      --version		顯示版本資訊并退出
      
[root@centos6_test tmp]#head /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Thu Sep 19 08:46:05 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/VolGroup-lv_root /                       ext4    defaults        1 1
UUID=3c68b9fa-be71-4498-a3c2-20583ef982ae /boot                   ext4    defaults        1 2
[root@centos6_test tmp]#head -n 4 /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Thu Sep 19 08:46:05 2019
[root@centos6_test tmp]#head -n -4 /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Thu Sep 19 08:46:05 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/VolGroup-lv_root /                       ext4    defaults        1 1
UUID=3c68b9fa-be71-4498-a3c2-20583ef982ae /boot                   ext4    defaults        1 2
/dev/mapper/VolGroup-lv_swap swap                    swap    defaults        0 0
[root@centos6_test tmp]#cat /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Thu Sep 19 08:46:05 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/VolGroup-lv_root /                       ext4    defaults        1 1
UUID=3c68b9fa-be71-4498-a3c2-20583ef982ae /boot                   ext4    defaults        1 2
/dev/mapper/VolGroup-lv_swap swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
[root@centos6_test tmp]#      

    tail:将指定檔案的最後10行顯示到标準輸出。

tail [選項]... [檔案]...      
長選項必須使用的參數對于短選項時也是必需使用的。
  -c, --bytes=K		輸出最後K 位元組;另外,使用-c +K 從每個檔案的
			第K 位元組輸出
  -f, --follow[={name|descriptor}]
		即時輸出檔案變化後追加的資料。
			-f, --follow 等于--follow=descriptor 
  -F		即--follow=name --retry
  -n, --lines=K            output the last K lines, instead of the last 10;
                           or use -n +K to output lines starting with the Kth
      --max-unchanged-stats=N
                           with --follow=name, reopen a FILE which has not
                           changed size after N (default 5) iterations
                           to see if it has been unlinked or renamed
                           (this is the usual case of rotated log files).
                           With inotify, this option is rarely useful.
      --pid=PID		同 -f 一起使用,當 PID 所對應的程序死去後終止
  -q, --quiet, --silent	不輸出給出檔案名的頭
      --retry		即使目标檔案不可通路依然試圖打開;在與參數
			--follow=name 同時使用時常常有用。
  -s, --sleep-interval=N   with -f, sleep for approximately N seconds
                             (default 1.0) between iterations.
                           With inotify and --pid=P, check process P at
                           least once every N seconds.
  -v, --verbose            always output headers giving file names
      --help		顯示此幫助資訊并退出
      --version		顯示版本資訊并退出
      
[root@centos6_test tmp]#tail fstab 
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/VolGroup-lv_root /                       ext4    defaults        1 1
UUID=3c68b9fa-be71-4498-a3c2-20583ef982ae /boot                   ext4    defaults        1 2
/dev/mapper/VolGroup-lv_swap swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
[root@centos6_test tmp]#tail -n 3 fstab 
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
      
Linux指令實踐(四)

說明:tail -f 可以監控某個檔案的内容變化,通常我們用來監控某些日志檔案的變化,和它類似的指令有tailf

    stat:檢視檔案或檔案系統的狀态資訊。

stat [選項]... 檔案...      
-L, --dereference     follow links
  -Z, --context         print the SELinux security context 
  -f, --file-system     display file system status instead of file status
  -c --format=格式	使用指定輸出格式代替預設值,每用一次指定格式換一新行
      --printf=格式	類似 --format,但是會解釋反斜杠轉義符,不使用換行作
				輸出結尾。如果您仍希望使用換行,可以在格式中
				加入"\n"
  -t, --terse		使用簡潔格式輸出
      --help		顯示此幫助資訊并退出
      --version		顯示版本資訊并退出      
[root@centos6_test tmp]#stat /etc/fstab 
  File: "/etc/fstab"
  Size: 779       	Blocks: 8          IO Block: 4096   普通檔案
Device: fd00h/64768d	Inode: 654087      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-10-11 12:37:07.236000094 +0800
Modify: 2019-09-19 08:46:05.221999997 +0800
Change: 2019-09-19 08:58:21.402999996 +0800      

2、使用指令行展開功能,建立/tmp/a1,/tmp/a2,/tmp/a1/a,/tmp/a1/b,在/tmp目錄下建立目錄:x_y,x_z,q_y,q_z

[root@centos6_test ~]#mkdir -pv /tmp/a{1/{a,b},2}
mkdir: 已建立目錄 "/tmp/a1"
mkdir: 已建立目錄 "/tmp/a1/a"
mkdir: 已建立目錄 "/tmp/a1/b"
mkdir: 已建立目錄 "/tmp/a2"
[root@centos6_test ~]#mkdir -pv /tmp/{x_,q_}{y,z}
mkdir: 已建立目錄 "/tmp/x_y"
mkdir: 已建立目錄 "/tmp/x_z"
mkdir: 已建立目錄 "/tmp/q_y"
mkdir: 已建立目錄 "/tmp/q_z"
[root@centos6_test ~]#      

3、檔案的中繼資料資訊都有哪些,分别表示什麼含義,如何檢視?如何修改檔案的時間戳資訊。

     檔案的資料分兩種,一種是檔案的中繼資料,一種是檔案資料内容本身,檢視檔案的中繼資料可以用stat指令來檢視。

比如檢視 /etc/issue 這個檔案的中繼資料,可以用stat /etc/issue指令來檢視如下:

[root@centos6_test tmp]#stat /etc/issue
  File: "/etc/issue"
  Size: 0         	Blocks: 0          IO Block: 4096   普通空檔案
Device: fd00h/64768d	Inode: 659335      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-10-10 11:41:22.599000002 +0800
Modify: 2019-10-01 14:30:47.741993885 +0800
Change: 2019-10-01 14:30:47.741993885 +0800
[root@centos6_test tmp]#      

說明:File:表示檔案名

Size:檔案的大小

Blocks:檔案占用了多少資料塊

IO Block:檔案所占的資料塊大小(一個資料塊的大小)

Device:檔案所在硬碟的那個柱面

Inode:檔案的節點編号

Links:檔案的連接配接數

Access:檔案權限及最近通路時間

Uid:檔案的所有者的UID

GId:檔案的所屬組的GID

Modify:檔案最近修改時間

Change:檔案最近變化時間(屬性最近變化時間)

修改檔案的時間戳資訊用touch指令來修改

touch -a 修改檔案的通路時間(atime)或者touch --time=atime 檔案

touch -m 修改檔案的mtime 或者 touch --time=mtime 檔案

也可以用touch -t "時間字元串" 檔案 或者touch -d "時間字元串" 檔案 來修改,值得注意的是時間字元串的格式是[[CC]YY]MMDDhhmm[.ss],其中CC表示世紀,YY表示年分,MM表示月份,DD表示天,hh表示小時,mm表示分鐘,ss表示秒鐘。如果使用-d 來指定時間字元串,其格式可以寫成 "YYYY-MM-DD hh:mm:ss" 就是我們通常看到的時間格式。

[root@centos6_test tmp]#date
2019年 10月 10日 星期四 15:43:49 CST
[root@centos6_test tmp]#touch -a /etc/issue
[root@centos6_test tmp]#stat /etc/issue
  File: "/etc/issue"
  Size: 0         	Blocks: 0          IO Block: 4096   普通空檔案
Device: fd00h/64768d	Inode: 659335      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-10-10 15:43:55.998999825 +0800
Modify: 2019-10-01 14:30:47.741993885 +0800
Change: 2019-10-10 15:43:55.998999825 +0800
[root@centos6_test tmp]#touch -m /etc/issue
[root@centos6_test tmp]#stat /etc/issue
  File: "/etc/issue"
  Size: 0         	Blocks: 0          IO Block: 4096   普通空檔案
Device: fd00h/64768d	Inode: 659335      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2019-10-10 15:43:55.998999825 +0800
Modify: 2019-10-10 15:44:17.930009460 +0800
Change: 2019-10-10 15:44:17.930009460 +0800
[root@centos6_test tmp]#touch -t '202112111354.30' /etc/issue
[root@centos6_test tmp]#stat /etc/issue
  File: "/etc/issue"
  Size: 0         	Blocks: 0          IO Block: 4096   普通空檔案
Device: fd00h/64768d	Inode: 659335      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2021-12-11 13:54:30.000000000 +0800
Modify: 2021-12-11 13:54:30.000000000 +0800
Change: 2019-10-10 15:45:23.832995022 +0800
[root@centos6_test tmp]#[root@centos6_test tmp]#touch -d '2011-10-18 16:43:23' /etc/issue      
[root@centos6_test tmp]#stat /etc/issue
  File: "/etc/issue"
  Size: 0         	Blocks: 0          IO Block: 4096   普通空檔案
Device: fd00h/64768d	Inode: 659335      Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2011-10-18 16:43:23.000000000 +0800
Modify: 2011-10-18 16:43:23.000000000 +0800
Change: 2019-10-10 15:58:22.092994693 +0800
[root@centos6_test tmp]#      

  說明:用touch -t 或者-d指定時間來修改檔案的時間戳資訊,是把檔案的atime和mtime修改成指定時間,而ctime不是修改成指定時間,ctime還是會記錄目前檔案屬性發生變化的系統時間

4、在/tmp目錄建立以tfile開頭,後跟目前日期和時間的檔案,檔案名形如:tfile-2016-05-27-09-32-22。

[root@centos6_test ~]#touch /tmp/tfile-$(date +%F-%H-%M-%S)
[root@centos6_test ~]#ls /tmp
tfile-2019-10-08-12-38-26
[root@centos6_test ~]#      

5、複制/etc目錄下所有以p開頭,以非數字結尾的檔案或目錄到/tmp/mytest1目錄中。

[root@centos6_test ~]#cp -a /etc/p*[^[:digit:]] /tmp/mytest1/
[root@centos6_test ~]#ll /tmp/mytest1/
總用量 104
drwxr-xr-x.  2 root root 4096 9月  19 08:58 pam.d
drwxr-xr-x.  3 root root 4096 9月  19 08:49 pango
-rw-r--r--.  1 root root 1615 10月  1 14:39 passwd
-rw-r--r--.  1 root root 1574 9月  19 08:58 passwd-
-rw-r--r--.  1 root root 1362 8月  23 2010 pbm2ppa.conf
drwxr-xr-x.  2 root root 4096 9月  19 08:53 pcmcia
-rw-r--r--.  1 root root 2872 8月  21 2010 pinforc
drwxr-xr-x. 10 root root 4096 9月  19 08:52 pki
drwxr-xr-x.  2 root root 4096 9月  19 08:50 plymouth
drwxr-xr-x.  5 root root 4096 9月  19 08:46 pm
-rw-r--r--.  1 root root  370 10月 15 2014 pm-utils-hd-apm-restore.conf
-rw-r--r--.  1 root root 6300 8月  23 2010 pnm2ppa.conf
drwxr-xr-x.  2 root root 4096 8月  21 2010 popt.d
drwxr-xr-x.  2 root root 4096 9月  19 08:50 portreserve
drwxr-xr-x.  2 root root 4096 9月  19 08:52 postfix
drwxr-xr-x.  3 root root 4096 9月  19 08:52 ppp
-rw-r--r--.  1 root root  789 7月  19 2013 prelink.conf
drwxr-xr-x.  2 root root 4096 8月  19 2013 prelink.conf.d
-rw-r--r--.  1 root root  233 1月  12 2010 printcap
-rw-r--r--.  1 root root 1841 3月  22 2017 profile
drwxr-xr-x.  2 root root 4096 10月  1 14:06 profile.d
-rw-r--r--.  1 root root 6455 1月  12 2010 protocols
drwxr-xr-x.  2 root root 4096 9月  19 08:52 pulse
drwxr-xr-x.  2 root root 4096 9月  19 08:49 purple
[root@centos6_test ~]#      

6、建立使用者tom,指定UID為5001,指定家目錄為/tmp/tom,指定shell為/bin/zsh,指定基本組為tom,附加組為jack。

[root@centos6_test ~]#useradd -u5001 -d /tmp/tom -s /bin/zsh -g tom -G jack tom
[root@centos6_test ~]#getent passwd tom
tom:x:5001:501::/tmp/tom:/bin/zsh
[root@centos6_test ~]#id tom
uid=5001(tom) gid=501(tom) 組=501(tom),502(jack)
[root@centos6_test ~]#
      

7、常用的使用者以及檔案管理指令有哪些,并示範指令以及用法。

  • 使用者的增加

    useradd:添加使用者

      用法:useradd [選項] 使用者名

       -b, --base-dir BASE_DIR 新賬戶的主目錄的基目錄

       -c, --comment COMMENT 新賬戶的 GECOS 字段

       -d, --home-dir HOME_DIR 新賬戶的主目錄

       -D, --defaults 顯示或更改預設的 useradd 配置

       -e, --expiredate EXPIRE_DATE 新賬戶的過期日期 

       -f, --inactive INACTIVE 新賬戶的密碼不活動期

       -g, --gid GROUP 新賬戶主組的名稱或 ID

       -G, --groups GROUPS 新賬戶的附加組清單

       -h, --help 顯示此幫助資訊并推出

       -k, --skel SKEL_DIR 使用此目錄作為骨架目錄

       -K, --key KEY=VALUE 不使用 /etc/login.defs 中的預設值

       -l, --no-log-init 不要将此使用者添加到最近登入和登入失敗資料庫

       -m, --create-home 建立使用者的主目錄

       -M, --no-create-home 不建立使用者的主目錄

       -N, --no-user-group 不建立同名的組

       -o, --non-unique 允許使用重複的 UID 建立使用者

       -p, --password PASSWORD 加密後的新賬戶密碼

       -r, --system 建立一個系統賬戶

       -R, --root CHROOT_DIR chroot 到的目錄

       -s, --shell SHELL 新賬戶的登入 shell

       -u, --uid UID 新賬戶的使用者 ID

       -U, --user-group 建立與使用者同名的組

       -Z, --selinux-user SEUSER 為 SELinux 使用者映射使用指定 SEUSER

[root@centos6_test ~]#useradd -b /tmp/test/ -c 'test' -d /tmp/test test
useradd:警告:此主目錄已經存在。
不從 skel 目錄裡向其中複制任何檔案。
[root@centos6_test ~]#getent passwd test
test:x:5002:5002:test:/tmp/test:/bin/bash
[root@centos6_test ~]#useradd -b /tmp/xxx -c 'xxxx' -d /tmp/xxx xxx
[root@centos6_test ~]#getent passwd xxx
xxx:x:5003:5003:xxxx:/tmp/xxx:/bin/bash
[root@centos6_test ~]#useradd -g 500 -u 901 -G 5002 -r -M -s /sbin/nologin ooo
[root@centos6_test ~]#getent passwd ooo
ooo:x:901:500::/home/ooo:/sbin/nologin
[root@centos6_test ~]#ll /home/
總用量 4
drwx------. 4 qiuhom qiuhom 4096 10月 12 00:15 qiuhom
[root@centos6_test ~]#id ooo
uid=901(ooo) gid=500(qiuhom) 組=500(qiuhom),5002(test)
[root@centos6_test ~]#      
  • 使用者的删除

    userdel:删除使用者

      用法:userdel [選項] 使用者名

         -f, --force force some actions that would fail otherwise

                     e.g. removal of user still logged in 

                     or files, even if not owned by the user

         -h, --help 顯示此幫助資訊并推出

         -r, --remove 删除主目錄和郵件池

         -R, --root CHROOT_DIR chroot 到的目錄

         -Z, --selinux-user 為使用者删除所有的 SELinux 使用者映射

[root@centos6_test ~]#getent passwd xxx
xxx:x:902:902::/home/xxx:/bin/bash
[root@centos6_test ~]#ll /home
總用量 8
drwx------. 4 qiuhom qiuhom 4096 10月 12 00:15 qiuhom
drwx------  4 xxx    xxx    4096 10月 12 00:55 xxx
[root@centos6_test ~]#userdel -r xxx
userdel:/var/spool/mail/xxx 并不屬于 xxx,是以不會删除
[root@centos6_test ~]#ll /home/
總用量 4
drwx------. 4 qiuhom qiuhom 4096 10月 12 00:15 qiuhom
[root@centos6_test ~]#id xxx
id: xxx:無此使用者
[root@centos6_test ~]#      

    chage:修改賬号和密碼都有效期限

      用法:chage [選項] 使用者名

        -d, --lastday 最近日期 将最近一次密碼設定時間設為“最近日期”

        -E, --expiredate 過期日期 将帳戶過期時間設為“過期日期”

        -h, --help 顯示此幫助資訊并推出

        -I, --inactive INACITVE 過期 INACTIVE 天數後,設定密碼為失效狀态(大寫i)

        -l, --list 顯示帳戶年齡資訊(小寫L)

        -m, --mindays 最小天數 将兩次改變密碼之間相距的最小天數設為“最小天數”

        -M, --maxdays 最大天數 将兩次改變密碼之間相距的最大天數設為“最大天數”

        -R, --root CHROOT_DIR chroot 到的目錄

        -W, --warndays 警告天數 将過期警告天數設為“警告天數”

[root@centos6_test ~]#chage -l qiuhom
最近一次密碼修改時間					:10月 11, 2019
密碼過期時間					:從不
密碼失效時間					:從不
帳戶過期時間						:從不
兩次改變密碼之間相距的最小天數		:0
兩次改變密碼之間相距的最大天數		:99999
在密碼過期之前警告的天數	:7
[root@centos6_test ~]#chage -m 3 qiuhom
[root@centos6_test ~]#chage -l qiuhom
最近一次密碼修改時間					:10月 11, 2019
密碼過期時間					:從不
密碼失效時間					:從不
帳戶過期時間						:從不
兩次改變密碼之間相距的最小天數		:3
兩次改變密碼之間相距的最大天數		:99999
在密碼過期之前警告的天數	:7
[root@centos6_test ~]#chage -M 42 qiuhom
[root@centos6_test ~]#chage -l qiuhom
最近一次密碼修改時間					:10月 11, 2019
密碼過期時間					:11月 22, 2019
密碼失效時間					:從不
帳戶過期時間						:從不
兩次改變密碼之間相距的最小天數		:3
兩次改變密碼之間相距的最大天數		:42
在密碼過期之前警告的天數	:7
[root@centos6_test ~]#getent shadow qiuhom
qiuhom:$6$xVEe.VPn$j0KZB5OJLuoI49C5kEbWpsW8Lr3y3uFBXt6JYfOj9iysl9rVN0ohG9aezKIpuhd6mv5fMij3qBksjytjbm35B/:18180:0:99999:7:::
root@centos6_test ~]#chage -I 5 qiuhom
[root@centos6_test ~]#getent shadow qiuhom
qiuhom:$6$xVEe.VPn$j0KZB5OJLuoI49C5kEbWpsW8Lr3y3uFBXt6JYfOj9iysl9rVN0ohG9aezKIpuhd6mv5fMij3qBksjytjbm35B/:18180:3:42:2:5::
[root@centos6_test ~]#chage -W 3 qiuhom
[root@centos6_test ~]#chage -l qiuhom
最近一次密碼修改時間					:10月 11, 2019
密碼過期時間					:11月 22, 2019
密碼失效時間					:11月 27, 2019
帳戶過期時間						:從不
兩次改變密碼之間相距的最小天數		:3
兩次改變密碼之間相距的最大天數		:42
在密碼過期之前警告的天數	:3
[root@centos6_test ~]#chage -d 2019-10-09 qiuhom
[root@centos6_test ~]#chage -l qiuhom
最近一次密碼修改時間					:10月 09, 2019
密碼過期時間					:11月 20, 2019
密碼失效時間					:11月 25, 2019
帳戶過期時間						:從不
兩次改變密碼之間相距的最小天數		:3
兩次改變密碼之間相距的最大天數		:42
在密碼過期之前警告的天數	:3
[root@centos6_test ~]#chage -E 2022-12-12 qiuhom
[root@centos6_test ~]#chage -l qiuhom
最近一次密碼修改時間					:10月 09, 2019
密碼過期時間					:11月 20, 2019
密碼失效時間					:11月 25, 2019
帳戶過期時間						:12月 12, 2022
兩次改變密碼之間相距的最小天數		:3
兩次改變密碼之間相距的最大天數		:42
在密碼過期之前警告的天數	:3      
  • 使用者的檢視

    id:顯示目前使用者或指定使用者的使用者與組的資訊。

id [選項]... [使用者名]      
-a			忽略,僅為與其他版本相相容而設計
  -Z, --context		僅顯示目前使用者的安全環境
  -g, --group		僅顯示有效的使用者組ID
  -G, --groups		顯示所有組的ID
  -n, --name		顯示組名稱而非數字,不與-ugG 一起使用
  -r, --real		顯示真實ID 而非有效ID,與-ugG 一起使用
  -u, --user		僅顯示有效使用者ID
      --help		顯示此幫助資訊并退出
      --version		顯示版本資訊并退出      

 常用示範:

[root@centos6_test ~]#id
uid=0(root) gid=0(root) 組=0(root)
[root@centos6_test ~]#id -u
0
[root@centos6_test ~]#id -g
0
[root@centos6_test ~]#id -G
0
[root@centos6_test ~]#id -un
root
[root@centos6_test ~]#id -gn
root
[root@centos6_test ~]#id -Gn
root
[root@centos6_test ~]#id -Gn tom
tom jack      

    getent:查詢資料庫中的内容

getent [選項...] 資料庫 [鍵 ...]      

選項: 

-s, --service=CONFIG       要使用的服務配置
  -?, --help                 給出該系統求助清單
      --usage                給出簡要的用法資訊
  -V, --version              列印程式版本号      
[root@centos6_test ~]#getent passwd qiuhom
qiuhom:x:500:500::/home/qiuhom:/bin/bash
[root@centos6_test ~]#getent group qiuhom
qiuhom:x:500:
[root@centos6_test ~]#getent shadow qiuhom
qiuhom:$6$epxVKdJN$OWp0tnT2zvFP26rMKuqEZizxUo3HaDqDTO.YHgCpoaIp7jqA4WtblmtH3.4UjICZFKIvJLQzxIB8t9lXoESdV1:18170:0:99999:7:::      

說明:在Linux系統裡,資料庫預設就是以文本的形式展示給使用者。如上示範,/etc/passwd、/etc/shadow 等都是一個個資料庫。

  • 使用者的修改

    usermod:修改使用者資訊

usermod [選項] 使用者      
-c, --comment 注釋            GECOS 字段的新值
  -d, --home HOME_DIR           使用者的新主目錄
  -e, --expiredate EXPIRE_DATE  設定帳戶過期的日期為 EXPIRE_DATE
  -f, --inactive INACTIVE       過期 INACTIVE 天數後,設定密碼為失效狀态
  -g, --gid GROUP               強制使用 GROUP 為新主組
  -G, --groups GROUPS           新的附加組清單 GROUPS
  -a, --append GROUP            将使用者追加至上邊 -G 中提到的附加組中,
                                并不從其它組中删除此使用者
  -h, --help                    顯示此幫助資訊并推出
  -l, --login LOGIN             新的登入名稱
  -L, --lock                    鎖定使用者帳号
  -m, --move-home               将家目錄内容移至新位置 (僅于 -d 一起使用)
  -o, --non-unique              允許使用重複的(非唯一的) UID
  -p, --password PASSWORD       将加密過的密碼 (PASSWORD) 設為新密碼
  -R, --root CHROOT_DIR         chroot 到的目錄
  -s, --shell SHELL             該使用者帳号的新登入 shell
  -u, --uid UID                 使用者帳号的新 UID
  -U, --unlock                  解鎖使用者帳号
  -Z, --selinux-user  SEUSER       使用者賬戶的新 SELinux 使用者映射      
[root@centos6_test ~]#tail -4 /etc/passwd
tom:x:5001:501::/tmp/tom:/bin/zsh
test:x:5002:5002:test:/tmp/test:/bin/bash
xxx:x:5003:5003:xxxx:/tmp/xxx:/bin/bash
ooo:x:901:500::/home/ooo:/sbin/nologin
[root@centos6_test ~]#usermod -c "test usermod" -d /tmp/test/ -s /bin/csh test
[root@centos6_test ~]#getent passwd test
test:x:5002:5002:test usermod:/tmp/test/:/bin/csh
[root@centos6_test ~]#getent shadow qiuhom
qiuhom:$6$xVEe.VPn$j0KZB5OJLuoI49C5kEbWpsW8Lr3y3uFBXt6JYfOj9iysl9rVN0ohG9aezKIpuhd6mv5fMij3qBksjytjbm35B/:18180:0:99999:7:::
[root@centos6_test ~]#usermod -L qiuhom
[root@centos6_test ~]#getent shadow qiuhom
qiuhom:!$6$xVEe.VPn$j0KZB5OJLuoI49C5kEbWpsW8Lr3y3uFBXt6JYfOj9iysl9rVN0ohG9aezKIpuhd6mv5fMij3qBksjytjbm35B/:18180:0:99999:7:::
[root@centos6_test ~]#usermod -U qiuhom
[root@centos6_test ~]#getent shadow qiuhom
qiuhom:$6$xVEe.VPn$j0KZB5OJLuoI49C5kEbWpsW8Lr3y3uFBXt6JYfOj9iysl9rVN0ohG9aezKIpuhd6mv5fMij3qBksjytjbm35B/:18180:0:99999:7:::
[root@centos6_test ~]#usermod -l 'abc' test
[root@centos6_test ~]#getent passwd testt
[root@centos6_test ~]#getent passwd test
[root@centos6_test ~]#tail -4 /etc/passwd
tom:x:5001:501::/tmp/tom:/bin/zsh
xxx:x:5003:5003:xxxx:/tmp/xxx:/bin/bash
ooo:x:901:500::/home/ooo:/sbin/nologin
abc:x:5002:5002:test usermod:/tmp/test/:/bin/csh
[root@centos6_test ~]#      

    passwd:設定使用者的認證資訊,包括使用者密碼、密碼過期時間等。 

passwd [選項...] <帳号名稱>      
-k, --keep-tokens       保持身份驗證令牌不過期
  -d, --delete            删除已命名帳号的密碼(隻有根root才能進行此操作)
  -l, --lock              lock the password for the named account (root only)
  -u, --unlock            unlock the password for the named account (root only)
  -e, --expire            expire the password for the named account (root only)
  -f, --force             強制執行操作
  -x, --maximum=DAYS      密碼的最長有效時限(隻有root才能進行此操作)
  -n, --minimum=DAYS      密碼的最短有效時限(隻有root才能進行此操作)
  -w, --warning=DAYS      在密碼過期前多少天開始提醒使用者(隻有root才能進行此操作)
  -i, --inactive=DAYS     當密碼過期後經過多少天該帳号會被禁用(隻有root才能進行此操作)
  -S, --status            報告已命名帳号的密碼狀态(隻有root才能進行此操作)
  --stdin                 從标準輸入讀取令牌(隻有root才能進行此操作)      
[root@centos6_test ~]#passwd
更改使用者 root 的密碼 。
新的 密碼:
無效的密碼: 過短
無效的密碼: 過于簡單
重新輸入新的 密碼:
passwd: 所有的身份驗證令牌已經成功更新。
[root@centos6_test ~]#passwd qiuhom
更改使用者 qiuhom 的密碼 。
新的 密碼:
無效的密碼: 過短
無效的密碼: 過于簡單
重新輸入新的 密碼:
passwd: 所有的身份驗證令牌已經成功更新。
[root@centos6_test ~]#echo 'admin'|passwd --stdin qiuhom
更改使用者 qiuhom 的密碼 。
passwd: 所有的身份驗證令牌已經成功更新。
[root@centos6_test ~]#passwd -S tom
tom LK 2019-10-10 0 99999 7 -1 (密碼已被鎖定。)
[root@centos6_test ~]#passwd -S qiuhom
qiuhom PS 2019-10-11 0 99999 7 -1 (密碼已設定,使用 SHA512 加密。)
[root@centos6_test ~]#passwd -l qiuhom
鎖定使用者 qiuhom 的密碼 。
passwd: 操作成功
[root@centos6_test ~]#passwd -u qiuhom
解鎖使用者 qiuhom 的密碼 。
passwd: 操作成功
[root@centos6_test ~]#      
  • 使用者切換

    su:switch user 

su [選項]... [-] [使用者 [參數]... ]      
-f 或 --fast 不必讀啟動檔(如 csh.cshrc 等),僅用于 csh 或 tcsh
-m -p 或 --preserve-environment 執行 su 時不改變環境變數
-c command 或 --command=command 變更為帳号為 USER 的使用者并執行指令(command)後再變回原來使用者
-s shell 或 --shell=shell 指定要執行的 shell (bash csh tcsh 等),預設值為 /etc/passwd 内的該使用者(USER) shell
--help 顯示說明檔案
--version 顯示版本資訊
- -l 或 --login 這個參數加了之後,就好像是重新 login 為該使用者一樣,大部份環境變數(HOME SHELL USER等等)都是以該使用者(USER)為主,并且工作目錄也會改變,如果沒有指定 USER ,内定是 root
USER 欲變更的使用者帳号
ARG 傳入新的 shell 參數      
[root@centos6_test ~]#whoami
root
[root@centos6_test ~]#pwd
/root
[root@centos6_test ~]#su qiuhom
[qiuhom@centos6_test root]$whoami
qiuhom
[qiuhom@centos6_test root]$pwd
/root
[qiuhom@centos6_test root]$exit
exit
[root@centos6_test ~]#su - qiuhom
[qiuhom@centos6_test ~]$whoami
qiuhom
[qiuhom@centos6_test ~]$pwd
/home/qiuhom
[qiuhom@centos6_test ~]$exit
logout
[root@centos6_test ~]#su -s /bin/csh - qiuhom
[qiuhom@centos6_test ~]$ echo $SHELL
/bin/csh
[qiuhom@centos6_test ~]$ exit
logout
[root@centos6_test ~]#su -c 'ls' qiuhom
ls: 無法打開目錄.: 權限不夠
[root@centos6_test ~]#su -c 'ls' - qiuhom
[root@centos6_test ~]#cp /tmp/
.esd-0/    fstab      .ICE-unix/ 
[root@centos6_test ~]#cp /tmp/* /home/qiuhom/
[root@centos6_test ~]#su -c 'ls -al' - qiuhom
total 40
drwx------. 4 qiuhom qiuhom 4096 Oct 12 00:15 .
drwxr-xr-x. 3 root   root   4096 Oct  1 14:39 ..
-rw-------  1 qiuhom qiuhom   37 Oct 12 00:13 .bash_history
-rw-r--r--. 1 qiuhom qiuhom   18 Mar 23  2017 .bash_logout
-rw-r--r--. 1 qiuhom qiuhom  176 Mar 23  2017 .bash_profile
-rw-r--r--. 1 qiuhom qiuhom  124 Mar 23  2017 .bashrc
-rw-r--r--  1 root   root    779 Oct 12 00:15 fstab
drwxr-xr-x. 2 qiuhom qiuhom 4096 Nov 12  2010 .gnome2
-rw-------  1 qiuhom qiuhom   43 Oct 12 00:14 .history
drwxr-xr-x. 4 qiuhom qiuhom 4096 Sep 19 08:46 .mozilla
[root@centos6_test ~]#whoami
root
[root@centos6_test ~]#pwd
/root
[root@centos6_test ~]#       
  • 使用者賬戶資訊和密碼資訊配置檔案

     /etc/passwd:存儲使用者都使用者名、使用者ID、使用者密碼标志、使用者uid、使用者gid、使用者說明資訊、使用者家目錄資訊、使用者shell類型;

    /etc/shadow:存儲使用者名、使用者密碼、使用者最後一次修改密碼都時間,這個時間是距離1970年1月1日經過都天數。接下來是最小修改密碼時間間隔,表示從最近修改日期算起,至少要經過多少天才能更改密碼,如果是0,表示随時可以修改。下一個字段是密碼都有效期限,表示從最近一次修改密碼算經過多長時間,密碼過期,必須修改,否則賬戶會過期。接下來是密碼需要變更前都警告天數,表示提前多少天告知使用者需要更改密碼了。下一個字段是密碼過期後寬限天數,表示密碼過期了,還可以有幾天可以登陸系統。賬号失效時間,表示到了這個時間賬号将無法使用,不管密碼是否過期。下一個字段保留。

    /etc/group:存儲使用者組名、組密碼标志、組id、組中的使用者

    /etc/gshadow:存儲使用者組名、組密碼、使用者管理者的名稱、支援的賬号名稱

    /etc/default/useradd:定義使用者預設建立使用者時的預設配置,

      其中GROUP=100 表示如果useradd沒有指定組,并且/etc/login.defs中的USERGROUPS_ENAB為no或者useradd使用了-N選項時,此時該參數生效。建立使用者時使用此組ID。

          HOME=/home  主目錄放在什麼目錄下

          INACTIVE=-1  帳号是否過期

          EXPIRE=  帳号終止日期

          SHELL=/bin/bash  預設使用哪個shell

          SKEL=/etc/skel  模闆目錄,骨架目錄

          CREATE_MAIL_SPOOL=yes  是否建立郵箱檔案

    /etc/login.defs:設定使用者帳号限制的檔案,在這裡我們可配置密碼的最大過期天數,密碼的最大長度限制等内容。該檔案裡的配置對root使用者 無效。如果/etc/shadow檔案裡有相同的選項,則以/etc/shadow裡的設定為準,也就是說/etc/shadow的配置優先級高于 /etc/login.defs  

作者:Linux-1874

出處:https://www.cnblogs.com/qiuhom-1874/

本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接配接,否則保留追究法律責任的權利.

繼續閱讀