文件管理類命令常用使用方法及其相關示例演示
cat
使用語法:cat [-AbeEnstTuv][--help][--version] FILENAME
命令功能:查看文件內容或將多文件連接后傳輸到基本輸出(也可以通過重定向將多文件合并為一個文件)
常用選項及示例
-n或–number 由1開始對所有輸出的內容以行為單位進行編號
1.[root - gwx-st ~]#>cat test1 -n
2.1 1111
3.2
4.3 22222
5.4 333333-b 或–number-nonblank 功能與-n相似,但是會忽略文件內的空白行
1.[root - gwx-st ~]#>cat test2 -b
2.1 1111
3.2 3333將兩個文件合并為一個文件
1.[root - gwx-st ~]#>cat test1 test2 -n > test3
2.[root - gwx-st ~]#>cat test3
3.1 1111
4.2
5.3 22222
6.4 333333
7.5 1111
8.6
9.7 3333
chattr
使用語法:chattr [-RV][-v<版本編號>][+/-/=<屬性>][文件或目錄...]
命令功能:改變文件隱藏屬性
隱藏屬性模式
1.a:讓文件或目錄僅供附加用途。
2.b:不更新文件或目錄的最后存取時間。
3.c:將文件或目錄壓縮后存放。
4.d:將文件或目錄排除誤操作之外。
5.i:不得任意更動文件或目錄。
6.s:保密性刪除文件或目錄。
7.S:即時更新文件或目錄。
8.u:預防意外刪除。
常用選項及示例
-R 遞歸處理,將指定目錄下的所有文件及子目錄一并處理。
-V 顯示指令執行過程。
+<屬性> 開啟文件或目錄的該項屬性。
1.[root - gwx-st ~]#>chattr -RV +i test
2.chattr 1.42.9 (28-Dec-2013)
3.Flags of test set as ----i-----------
4.Flags of test/test1 set as ----i-----------
5.Flags of test/test2 set as ----i-----------
6.[root - gwx-st ~]#>lsattr test
7.----i----------- test/test1
8.----i----------- test/test2-<屬性> 關閉文件或目錄的該項屬性。
1.[root - gwx-st ~]#>chattr -RV -i test
2.chattr 1.42.9 (28-Dec-2013)
3.Flags of test set as ----------------
4.Flags of test/test1 set as ----------------
5.Flags of test/test2 set as ----------------
6.[root - gwx-st ~]#>lsattr test
7.---------------- test/test1
8.---------------- test/test2=<屬性> 指定文件或目錄的該項屬性。
1.[root - gwx-st ~]#>chattr -RV =idS test
2.chattr 1.42.9 (28-Dec-2013)
3.Flags of test set as --S-i-d---------
4.Flags of test/test1 set as --S-i-d---------
5.Flags of test/test2 set as --S-i-d---------
6.[root - gwx-st ~]#>lsattr test
7.--S-i-d--------- test/test1
8.--S-i-d--------- test/test2
9.
chmod
使用語法:chmod [-cfRv][--help][--version][<權限范圍>+/-/=<權限設置...>][文件或目錄...]
或chmod[-cfRv][--help][--version][數字代號][文件或目錄...]
或 chmod [-cfRv][--help][--reference=<參考文件或目錄>][--version][文件或目錄...]
命令功能:變更文件或目錄的權限。
補充說明:
1. 文件或目錄權限的控制分別以讀取,寫入,執行3種一般權限來區分,可以使用 chmod指令去變更文件與目錄的權限,設置方式采用文字或數字代號皆可。符號連接的權限無法變更,如果您對符號連接修改權限,其改變會作用在被連接的原始文件。
2. 權限范圍的表示法如下:
3. u:User,即文件或目錄的擁有者。
4. g:Group,即文件或目錄的所屬群組。
5. o:Other,除了文件或目錄擁有者或所屬群組之外,其他用戶皆屬于這個范圍。
6. a:All,即全部的用戶,包含擁有者,所屬群組以及其他用戶。
7. 有關權限代號的部分,列表于下:
8. r:讀取權限,數字代號為"4"。
9. w:寫入權限,數字代號為"2"。
10. x:執行或切換權限,數字代號為"1"。
11. -:不具任何權限,數字代號為"0"。
常用選項及示例
-v或–verbose 顯示指令執行過程。
-c或–changes 效果類似”-v”參數,但僅回報更改的部分。
-f或–quiet或–silent 靜默模式,不顯示錯誤信息。
-R或–recursive 遞歸處理,將指定目錄下的所有文件及子目錄一并處理。<權限范圍>+<權限設置> 開啟權限范圍的文件或目錄的該項權限設置。
1.[root - gwx-st ~]#>chmod -Rv +w test
2.mode of 'test' retained as 0755 (rwxr-xr-x)
3.mode of 'test/test1' retained as 0644 (rw-r--r--)
4.mode of 'test/test2' retained as 0644 (rw-r--r--)<權限范圍>-<權限設置> 關閉權限范圍的文件或目錄的該項權限設置。
1.[root - gwx-st ~]#>chmod -Rv a-w test
2.mode of 'test' changed from 0755 (rwxr-xr-x) to 0555 (r-xr-xr-x)
3.mode of 'test/test1' changed from 0644 (rw-r--r--) to 0444 (r--r--r--)
4.mode of 'test/test2' changed from 0644 (rw-r--r--) to 0444 (r--r--r--)<權限范圍>=<權限設置> 指定權限范圍的文件或目錄的該項權限設置。
1.[root - gwx-st ~]#>chmod -Rv a=w test
2.mode of 'test' changed from 0555 (r-xr-xr-x) to 0222 (-w--w--w-)
3.mode of 'test/test1' changed from 0444 (r--r--r--) to 0222 (-w--w--w-)
4.mode of 'test/test2' changed from 0444 (r--r--r--) to 0222 (-w--w--w-)–reference=<參考文件或目錄> 把指定文件或目錄的權限全部設 成和參考文件或目錄的權限相同
1.[root - gwx-st ~]#>ll -d test
2.d-w--w--w-. 2 root root 30 Nov 4 14:37 test/
3.[root - gwx-st ~]#>ll sh -d
4.drwxr-xr-x. 2 root root 4.0K Nov 2 09:03 sh/
5.[root - gwx-st ~]#>chmod --reference sh test
6.[root - gwx-st ~]#>ll -d sh test
7.drwxr-xr-x. 2 root root 4.0K Nov 2 09:03 sh/
8.drwxr-xr-x. 2 root root 30 Nov 4 14:37 test/
chown
使用語法:chown [-cfhRv][--dereference][--help][--version][擁有者.<所屬群組>][文件或目錄..]
或chown[-chfRv][--dereference][--help][--version][.所屬群組][文件或目錄... ...]
或chown[-cfhRv][--dereference][--help][--reference=<參考文件或目錄>][--version][文件或目錄...]
命令功能:變更文件或目錄的擁有者或所屬群組
常用選項及示例
-f或–quite或–silent 不顯示錯誤信息。
-R或–recursive 遞歸處理,將指定目錄下的所有文件及子目錄一并處理。
-v或–version 顯示指令執行過程。
-c或–changes 效果類似”-v”參數,但僅回報更改的部分。
1.[root - gwx-st ~]#>ll -d test
2.d-w--w--w-. 2 root root 30 Nov 4 14:37 test/
3.[root - gwx-st ~]#>chown -Rc gwx.gwx test
4.changed ownership of 'test/test1' from root:root to gwx:gwx
5.changed ownership of 'test/test2' from root:root to gwx:gwx
6.changed ownership of 'test' from root:root to gwx:gwx
7.[root - gwx-st ~]#>ll -d test
8.d-w--w--w-. 2 gwx gwx 30 Nov 4 14:37 test/-h或–no-dereference 之對符號連接的文件作修改,而不更動其他任何相關文件。
1.[root - gwx-st ~]#>ll test1
2.lrwxrwxrwx. 1 root root 4 Nov 4 16:02 test1 -> test/
3.[root - gwx-st ~]#>ll -d test
4.drwxr-xr-x. 2 root root 30 Nov 4 16:04 test/
5.[root - gwx-st ~]#>chown -hc gwx.gwx test1
6.changed ownership of 'test1' from root:root to gwx:gwx
7.[root - gwx-st ~]#>ll test1
8.lrwxrwxrwx. 1 gwx gwx 4 Nov 4 16:02 test1 -> test/
9.[root - gwx-st ~]#>ll -d test
10.drwxr-xr-x. 2 root root 30 Nov 4 16:04 test/–reference=<參考文件或目錄> 把指定文件或目錄的擁有者與所屬群組全部設成和參考文件或目錄的擁有者與所屬群組相同。
1.[root - gwx-st ~]#>ll -d test
2.drwxr-xr-x. 2 root root 30 Nov 4 16:04 test/
3.[root - gwx-st ~]#>ll /home/gwx/sh/back-etc.sh
4.-rwxr-xr--. 1 gwx gwx 746 Oct 26 16:04 /home/gwx/sh/back-etc.sh*
5.[root - gwx-st ~]#>chown --reference=/home/gwx/sh/back-etc.sh test
6.[root - gwx-st ~]#>ll -d test
7.drwxr-xr-x. 2 gwx gwx 30 Nov 4 16:04 test/
cp
使用語法:cp [-abdfilpPrRsuvx][源文件或目錄][目標文件或目錄][目的目錄]
命令功能:復制文件或目錄
常用選項及示例
-a 或–archive 此參數的效果和同時指定”-dpR”參數相同。
-b 或–backup 刪除,覆蓋目標文件之前的備份,備份文件會在字尾加上一個備份字符串。
-v 或–verbose 顯示指令執行過程。
-i 或–interactive 覆蓋既有文件之前先詢問用戶。
-f 或–force 強行復制文件或目錄,不論目標文件或目錄是否已存在。
-r 遞歸處理,將指定目錄下的文件與子目錄一并處理。
-R 或–recursive 遞歸處理,將指定目錄下的所有文件與子目錄一并處理。
-p 或–preserve 保留源文件或目錄的屬性。
-P 或–parents 保留源文件或目錄的路徑。
1.[root - gwx-st ~]#>ll -d test*
2.drwxr-xr-x. 2 gwx gwx 30 Nov 4 16:04 test/
3.[root - gwx-st ~]#>cp -aiv test test1
4.'test' -> 'test1'
5.'test/test1' -> 'test1/test1'
6.'test/test2' -> 'test1/test2'
7.[root - gwx-st ~]#>ll -d test*
8.drwxr-xr-x. 2 gwx gwx 30 Nov 4 16:04 test/
9.drwxr-xr-x. 2 gwx gwx 30 Nov 4 16:04 test1/-s 或–symbolic-link 對源文件建立符號連接,而非復制文件。
1.[root - gwx-st ~]#>ll sh.tar.xz
2.-rw-r--r--. 1 root root 2.7K Oct 23 16:59 sh.tar.xz
3.[root - gwx-st ~]#>cp -sv sh.tar.xz sh.tar.xz.ln
4.'sh.tar.xz' -> 'sh.tar.xz.ln'
5.[root - gwx-st ~]#>ll sh.tar.xz.ln
6.lrwxrwxrwx. 1 root root 9 Nov 4 16:40 sh.tar.xz.ln -> sh.tar.xz
7.-d 或–no-dereference 當復制符號連接時,把目標文件或目錄也建立為符號連接,并指向與源文件或目錄連接的原始文件或目錄。
1.[root - gwx-st ~]#>ll sh.tar.xz.ln
2.lrwxrwxrwx. 1 root root 9 Nov 4 16:40 sh.tar.xz.ln -> sh.tar.xz
3.[root - gwx-st ~]#>cp -d sh.tar.xz.ln sh1.tar.xz
4.[root - gwx-st ~]#>ll sh1.tar.xz
5.lrwxrwxrwx. 1 root root 9 Nov 4 16:41 sh1.tar.xz -> sh.tar.xz
6.-l 或–link 對源文件建立硬連接,而非復制文件。
1.[root - gwx-st ~]#>cp -l sh.tar.xz sh2.tar.xz
2.[root - gwx-st ~]#>ll -i sh*.tar.xz
3.101983257 lrwxrwxrwx. 1 root root 9 Nov 4 16:41 sh1.tar.xz -> sh.tar.xz
4.119955255 -rw-r--r--. 2 root root 2.7K Oct 23 16:59 sh2.tar.xz
5.119955255 -rw-r--r--. 2 root root 2.7K Oct 23 16:59 sh.tar.xz
6.
ln
使用語法:ln [-bdfinsv][源文件或目錄][目標文件或目錄]
或 ln [-bdfinsv][源文件或目錄...][目的目錄]
命令功能:連接文件或目錄
常用選項及示例
-f或–force 強行建立文件或目錄的連接,不論文件或目錄是否存在。
-i或–interactive 覆蓋既有文件之前先詢問用戶。
-n或–no-dereference 把符號連接的目的目錄視為一般文件。
-s或–symbolic 對源文件建立符號連接,而非硬連接。
1.[root - gwx-st ~]#>ll nginx-1.11.5.tar.gz
2.-rw-r--r--. 1 root root 935K Oct 29 14:02 nginx-1.11.5.tar.gz
3.[root - gwx-st ~]#>ln -s nginx-1.11.5.tar.gz nginx
4.[root - gwx-st ~]#>ln nginx-1.11.5.tar.gz nginx1
5.[root - gwx-st ~]#>ll -i nginx*
6.101983269 lrwxrwxrwx. 1 root root 19 Nov 4 17:06 nginx -> nginx-1.11.5.tar.gz
7.131383020 -rw-r--r--. 2 root root 935K Oct 29 14:02 nginx1
8.131383020 -rw-r--r--. 2 root root 935K Oct 29 14:02 nginx-1.11.5.tar.gz.11.5.tar.gz nginx1
locate
使用語法:locate [-d <數據庫文件>][范本樣式...]
命令功能:基于本地文件數據庫查找文件
常用選項及示例
-d<數據庫文件>或–database=<數據庫文件> 設置locate指令使用的數據庫。locate指令預設的數據庫位于/var/lib/slocate目錄里,文件名為slocate.db,您可使用這個參數另行指定。使用updatedb更新本地文件數據庫。
1.[root - gwx-st ~]#>locate nginx-1.11.5.tar.gz
2./root/nginx-1.11.5.tar.gz
lsattr
使用語法:lsattr [-adlRvV][文件或目錄...]
命令功能:顯示文件隱藏屬性
常用選項及示例
-a 顯示所有文件和目錄,包括以”.”為名稱開頭字符的額外內建,現行目錄”.”與上層目錄”..”。
1.[root - gwx-st ~/test]#>lsattr -a
2.---------------- ./.
3.---------------- ./..
4.---------------- ./test1
5.---------------- ./test2
6.---------------- ./test.ln-d 顯示,目錄名稱,而非其內容。
1.[root - gwx-st ~/test]#>lsattr -d
2.---------------- .-R 遞歸處理,將指定目錄下的所有文件及子目錄一并處理。
1.[root - gwx-st ~]#>lsattr -R test
2.---------------- test/test1
3.---------------- test/test2
4.---------------- test/test.ln
mv
使用語法:mv [-bfiuv][源文件或目錄][目標文件或目錄]
命令功能:移動或更名現有的文件或目錄。不同不經下執行為移動,同一路徑下執行為更名。
常用選項及示例
-b或–backup 若需覆蓋文件,則覆蓋前先行備份。
-f或–force 若目標文件或目錄與現有的文件或目錄重復,則直接覆蓋現有的文 件或目錄。
-i或–interactive 覆蓋前先行詢問用戶。
-v或–verbose 執行時顯示詳細的信息。
1.移動
2.[root - gwx-st ~/test]#>ll
3.total 12K
4.-rw-r--r--. 2 root root 16 Nov 4 16:03 test1
5.-rw-r--r--. 1 root root 17 Nov 4 16:04 test2
6.-rw-r--r--. 2 root root 16 Nov 4 16:03 test.ln
7.[root - gwx-st ~/test]#>mv ../passwd .
8.[root - gwx-st ~/test]#>ll
9.total 16K
10.-rw-r--r--. 1 root root 3.0K Oct 31 08:39 passwd
11.-rw-r--r--. 2 root root 16 Nov 4 16:03 test1
12.-rw-r--r--. 1 root root 17 Nov 4 16:04 test2
13.-rw-r--r--. 2 root root 16 Nov 4 16:03 test.ln
1.更名
2.[root - gwx-st ~/test]#>ll
3.total 16K
4.-rw-r--r--. 1 root root 3.0K Oct 31 08:39 passwd
5.-rw-r--r--. 2 root root 16 Nov 4 16:03 test1
6.-rw-r--r--. 1 root root 17 Nov 4 16:04 test2
7.-rw-r--r--. 2 root root 16 Nov 4 16:03 test.ln
8.[root - gwx-st ~/test]#>mv test.ln test.mv
9.[root - gwx-st ~/test]#>ll
10.total 16K
11.-rw-r--r--. 1 root root 3.0K Oct 31 08:39 passwd
12.-rw-r--r--. 2 root root 16 Nov 4 16:03 test1
13.-rw-r--r--. 1 root root 17 Nov 4 16:04 test2
14.-rw-r--r--. 2 root root 16 Nov 4 16:03 test.mv
15.[root - gwx-st ~/test]#>
16.
rm
使用語法:rm [-dfirv][文件或目錄...]
命令功能:刪除文件或目錄
常用選項及示例
-d或–directory 直接把欲刪除的目錄的硬連接數據刪成0,刪除該目錄。
-f或–force 強制刪除文件或目錄。
-i或–interactive 刪除既有文件或目錄之前先詢問用戶。
-r或-R或–recursive 遞歸處理,將指定目錄下的所有文件及子目錄一并處理。
-v或–verbose 顯示指令執行過程。
1.[root - gwx-st ~]#>ll -d test*
2.drwxr-xr-x. 2 root root 57 Nov 4 17:21 test/
3.drwxr-xr-x. 2 gwx gwx 30 Nov 4 16:04 test1/
4.[root - gwx-st ~]#>rm -dirv test1
5.rm: descend into directory 'test1'? y
6.rm: remove regular file 'test1/test1'? y
7.removed 'test1/test1'
8.rm: remove regular file 'test1/test2'? y
9.removed 'test1/test2'
10.rm: remove directory 'test1'? y
11.removed directory: 'test1'
12.[root - gwx-st ~]#>ll -d test*
13.drwxr-xr-x. 2 root root 57 Nov 4 17:21 test/
tee
使用語法:tee [-ai][文件...]
命令功能:讀取標準輸入的數據,并將其內容輸出成文件
常用選項及示例
-a或–append 附加到既有文件的后面,而非覆蓋它。
-i-i或–ignore-interrupts 忽略中斷信號。
1.[root - gwx-st ~/test]#>cat test1
2.www
3.qqq
4.eee
5.[root - gwx-st ~/test]#>tee -ai test1
6.test
7.test
8.dd
9.dd
10.ss
11.ss
12.[root - gwx-st ~/test]#>cat test1
13.www
14.qqq
15.eee
16.test
17.dd
18.ss
touch
使用語法:touch [-acfm][-d <日期時間>][-r <參考文件或目錄>][-t <日期時間>][文件或目錄...]
或 touch [-acfm][日期時間][文件或目錄...]
命令功能:改變文件或目錄時間
默認情況下,如果指定的文件不存在的時候,touch命令會創建對應的文件
常用選項及示例
-t<日期時間> 使用指定的日期時間,而非現在的時間。
-a或–time=atime或–time=access或–time=use 只更改存取時間。
1.[root - gwx-st ~/test]#>ll -u test1
2.-rw-r--r--. 2 root root 24 Mar 11 2016 test1
3.[root - gwx-st ~/test]#>touch -at 201611041700.25 test1
4.[root - gwx-st ~/test]#>ll -u test1
5.-rw-r--r--. 2 root root 24 Nov 4 17:00 test1-c或–no-create 不建立任何文件。
1.[root - gwx-st ~/test]#>ll
2.total 16K
3.-rw-r--r--. 1 root root 3.0K Oct 31 08:39 passwd
4.-rw-r--r--. 2 root root 24 Nov 4 17:42 test1
5.-rw-r--r--. 1 root root 9 Nov 4 17:35 test2
6.-rw-r--r--. 2 root root 24 Nov 4 17:42 test.mv
7.[root - gwx-st ~/test]#>touch -c gghgag
8.[root - gwx-st ~/test]#>ll
9.total 16K
10.-rw-r--r--. 1 root root 3.0K Oct 31 08:39 passwd
11.-rw-r--r--. 2 root root 24 Nov 4 17:42 test1
12.-rw-r--r--. 1 root root 9 Nov 4 17:35 test2
13.-rw-r--r--. 2 root root 24 Nov 4 17:42 test.mv-m或–time=mtime或–time=modify 只更改變動時間。
1.[root - gwx-st ~/test]#>ll test1
2.-rw-r--r--. 2 root root 24 Nov 4 17:42 test1
3.[root - gwx-st ~/test]#>touch -mt 201611010000 test1
4.[root - gwx-st ~/test]#>ll test1
5.-rw-r--r--. 2 root root 24 Nov 1 00:00 test1-r<參考文件或目錄> 把指定文件或目錄的日期時間,統統設成和參考文件或目錄的日期時間相同。
1.[root - gwx-st ~/test]#>ll test1 test2
2.-rw-r--r--. 2 root root 24 Nov 1 00:00 test1
3.-rw-r--r--. 1 root root 9 Nov 4 17:35 test2
4.[root - gwx-st ~/test]#>touch -r test1 test2
5.[root - gwx-st ~/test]#>ll test1 test2
6.-rw-r--r--. 2 root root 24 Nov 1 00:00 test1
7.-rw-r--r--. 1 root root 9 Nov 1 00:00 test2
umask
使用語法:umask [-S][權限掩碼]
命令功能:指定在建立文件時預設的權限掩碼
注意:
umask設定的值在對象為文件的時候為被6減為設定的默認值,比如umask 077 ,則其權限為600;另外如果是只想對當前的一個操作指定與當前umask不等的設定時,可以使用以下方式實現:即umask只在子shell生效的效果
1. [root - gwx-st ~]#>ll umask
2. ls: cannot access umask: No such file or directory
3. [root - gwx-st ~]#>(umask 333;touch umask)
4. [root - gwx-st ~]#>ll umask
5. -r--r--r--. 1 root root 0 Nov 5 08:22 umask
常用選項及示例
-S 以文字的方式來表示權限掩碼
1.[root - gwx-st ~]#>umask
2.0022
3.[root - gwx-st ~]#>umask -S
4.u=rwx,g=rx,o=rx
whereis
使用語法:whereis [-bfmsu][文件...]
命令功能:會在特定目錄中查找符合條件的文件
常用選項及示例
-b 只查找二進制文件。
1.[root - gwx-st ~]#>whereis -b ls
2.ls: /usr/bin/ls
3.
which
使用語法:which [文件...]
命令功能:會在環境變量$PATH設置的目錄里查找符合條件的文件
常用選項及示例
1.[root - gwx-st ~]#>which ls
2.ls is aliased to `ls -hF --color'
3.ls is /usr/bin/ls
原創文章,作者:N24-wenxuan,如若轉載,請注明出處:http://www.www58058.com/57556
贊,總結的內容也比較多,通過markdown來展示命令,排版比較清晰~繼續加油~