D6
3.1 查看文件和目錄
1. ls命令
$ ls #列出當前目錄下所有文件和目錄,但不顯示詳細信息 a b c.txt d.txt e.txt $ ls -1 #每行顯示一條記錄 a b c.txt d.txt e.txt $ ls -l #以長列表格式顯示文件和目錄,包括文件類型、大小、修改日期和時間、權限等信息 total 8 drwxrwxr-x. 3 user1 user1 4096 May 11 15:01 a drwxrwxr-x. 3 user1 user1 4096 May 11 15:01 b -rw-rw-r--. 1 user1 user1 0 May 11 15:50 c.txt -rw-rw-r--. 1 user1 user1 0 May 11 15:52 d.txt -rw-rw-r--. 1 user1 user1 0 May 11 15:50 e.txt #第一個字符:文件類型,--普通文件,d-目錄,s-套接字文件,l-鏈接文件 #字段1:文件權限,9個字符,前三個-所有者,中間三個-用戶組,后面三個-其他用戶的讀、寫、執行權限 #字段2:鏈接數 #字段3:所有者,此處為user1 #字段4:用戶組,user1 #字段5:文件大小,默認字節 #字段6:文件最近一次修改時間 #字段7:文件名 $ls -lh #將文件大小顯示為適合閱讀的格式 total 8.0K drwxrwxr-x. 3 user1 user1 4.0K May 11 15:01 a drwxrwxr-x. 3 user1 user1 4.0K May 11 15:01 b -rw-rw-r--. 1 user1 user1 0 May 11 15:50 c.txt -rw-rw-r--. 1 user1 user1 0 May 11 15:52 d.txt -rw-rw-r--. 1 user1 user1 0 May 11 15:50 e.txt $ ls -F #使用不同的特殊字符歸類不同的文件類型 a/ b/ c.txt d.txt e.txt #/——表示目錄 #無特殊字符——表示普通文件 #@——表示鏈接文件 #*——表示可執行文件 $ ls -ld a #以長列表格式列出某個目錄的信息 drwxrwxr-x. 3 user1 user1 4096 May 11 15:01 a $ ls -R #遞歸地列出子目錄的內容 .: a b c.txt d.txt e.txt ./a: test ./a/test: a.txt file1.txt file2.txt ./b: test ./b/test: b.txt file10.txt file3.txt file4.txt $ ls -ltr #以長列表格式按文件或目錄的修改時間倒序地列出文件和目錄 total 8 drwxrwxr-x. 3 user1 user1 4096 May 11 15:01 b drwxrwxr-x. 3 user1 user1 4096 May 11 15:01 a -rw-rw-r--. 1 user1 user1 0 May 11 15:50 c.txt -rw-rw-r--. 1 user1 user1 0 May 11 15:50 e.txt -rw-rw-r--. 1 user1 user1 0 May 11 15:52 d.txt $ ls -ls #以長列表格式按文件大小順序列出目錄和文件 total 8 4 drwxrwxr-x. 3 user1 user1 4096 May 11 15:01 a 4 drwxrwxr-x. 3 user1 user1 4096 May 11 15:01 b 0 -rw-rw-r--. 1 user1 user1 0 May 11 15:50 c.txt 0 -rw-rw-r--. 1 user1 user1 0 May 11 15:52 d.txt 0 -rw-rw-r--. 1 user1 user1 0 May 11 15:50 e.txt $ ls -a #列出包括隱藏文件或目錄在內的所有文件和目錄,包括.(當前目錄)和..(父目錄) . .. a b c.txt d.txt e.txt $ ls -A #列出包括隱藏文件或目錄在內的所有文件和目錄,不包括.(當前目錄)和..(父目錄) a b c.txt d.txt e.txt $ ls -i #顯示文件或目錄的inode編號,可能在系統維護操作時需要(如find命令中用inode編號移除文件名中有特殊字符的文件) 265182 a 917505 b 265183 c.txt 265184 d.txt 265185 e.txt $ ls -n #類似於-l,但用uid和gid代替顯示所有者和用戶組 total 8 drwxrwxr-x. 3 1001 1001 4096 May 11 15:01 a drwxrwxr-x. 3 1001 1001 4096 May 11 15:01 b -rw-rw-r--. 1 1001 1001 0 May 11 15:50 c.txt -rw-rw-r--. 1 1001 1001 0 May 11 15:52 d.txt -rw-rw-r--. 1 1001 1001 0 May 11 15:50 e.txt
2. cat命令
可以查看文件的內容、連接文件、創建一個或多個文件和重定向輸出到終端或文件。
格式:cat [OPTION] [FILE]...
$ cat c.txt #查看文件內存 a b c d 1 2 3 4 2 4 6 8 4 8 12 16 $ cat d.txt #查看文件內容 This is d.txt. $ cat c.txt d.txt #查看多個文件的內容 a b c d 1 2 3 4 2 4 6 8 4 8 12 16 This is d.txt. $ cat -n c.txt #顯示文件內容的行號 1 a b c d 2 1 2 3 4 3 2 4 6 8 4 4 8 12 16 $ cat -b c.txt #顯示文件內容的行號,與-n不同,只標識非空白行的行號 1 a b c d 2 1 2 3 4 3 2 4 6 8 4 4 8 12 16 $ cat -e c.txt #在每一行的結尾顯示$字符 a b c d$ 1 2 3 4$ 2 4 6 8$ 4 8 12 16$ $ cat > f.txt #接收標准輸入的內容並在標准輸出中顯示,此處重定向標准輸出到新的文件 abcdef #此處輸入abcdef並按Ctrl+D組合鍵退出,內容會寫到文件中 $ cat f.txt #查看生成的文件內容 abcdef $ cat d.txt >> f.txt #利用重定向,將文件內容追加到文件中,用>則覆蓋 $ cat f.txt abcdef This is d.txt.
tac命令是將cat命令倒過來,用法:
$ tac c.txt #tac命令以行倒序的形式顯示文件內容 4 8 12 16 2 4 6 8 1 2 3 4 a b c d
D7
3. more和less命令
1)more命令是一個用於一次翻閱一整屏文件的過濾器。
$ more /etc/inittab #用more命令查看一個文件,會自動清空屏幕並顯示文件的開始部分 # inittab is no longer used when using systemd. # # ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM. # # Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target # # systemd uses 'targets' instead of runlevels. By default, there are two main targets: # # multi-user.target: analogous to runlevel 3 # graphical.target: analogous to runlevel 5 # # To view current default target, run: # systemctl get-default # # To set a default target, run: # systemctl set-default TARGET.target #此時按空格鍵,more會將文件下移一個你當前終端窗口的高度,顯示下一頁的內容。 $ more -5 /etc/inittab #使用-num(整數)選項,可以指定一次顯示的行數 # inittab is no longer used when using systemd. # # ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM. # # Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target ---More--(37%) $ cat /etc/inittab | more #通過管道將cat命令顯示的內容輸出到more,一次輸出但慢慢查看
2) less命令
less命令在查看前不需要加載整個文件,因此更快速;與more命令相比,支持向前和向后翻頁。
$ less /etc/fstab #用less命令查看一個文件
a)用less命令打開一個文件后,可以使用搜索功能:
向前搜索:
/——輸入/后面是要搜索的關鍵字,然后鍵入回車,顯示內容的第一行將自動跳轉到關鍵字第一次出現的位置,並高亮顯示所有搜索到的關鍵字;
n——輸入n,顯示內容的第一行將向前跳轉到下一個匹配;
N——輸入N,顯示內容的第一行將向回跳轉到前一個匹配;
向后搜索:
?——與/相反,向回搜索關鍵字;
n——向回搜索下一個匹配;
N——向前搜索下一個匹配。
b)瀏覽較大的文件時,可以使用如下屏幕導航命令:
Ctrl+F——向前翻一個窗口的內容
Ctrl+B——向回翻一個窗口的內容
Ctrl+D——向前翻半個窗口的內容
Ctrl+U——向回翻半個窗口的內容
G——跳轉到文件末尾
g——跳轉到文件開頭
q or ZZ——退出less
c)可以使用less打開多個文件
$ less file1 #打開file1 #此時輸入:":e",顯示 Examine: #此時輸入:"file2",可打開file2,末尾顯示: file2 (file2 of 2) (End)
當使用less打開了多個文件時,可使用如下關鍵字切換文件:
:n——跳轉到下一個文件;
:p——跳轉到前一個文件。
d)less可以在文件的特定位置做一個標記,並跳轉到標記位置:
m——后跟任意小寫字母,使用這個字母標記當前位置;
'(單引號)——后跟任意小寫字母,返回到這個小寫字母標記的位置。
#輸入m,可看到: mark: #輸入"'",可看到: goto mark:
e)在less中輸入F顯示新寫入的內容(注:我嘗試該命令有下面的顯示,但等待很久都沒有更新內容)
#輸入F,顯示: Waiting for data...(interrupt to abort)
4. head命令
head命令用於打印指定輸入的開頭部分內容。默認打印每個指定輸入的前10行。
$ head -n 2 c.txt #使用[-n 正整數N],顯示前N行 a b c d 1 2 3 4 $ head -2 c.txt #直接使用[-正整數N],顯示前N行 a b c d 1 2 3 4 $ head -n -1 c.txt #使用[-n -正整數N],顯示除倒數N行外的所有行 a b c d 1 2 3 4 2 4 6 8 $ head -c 10 c.txt #使用[-c 正整數N],顯示前N個字節的數據 a b c d 1
5. tail命令
tail命令與head命令相反,打印指定輸入的結尾部分的內容。默認打印最后10行。
$ tail -n 2 c.txt #打印末尾2行 2 4 6 8 4 8 12 16 $ tail -2 c.txt #打印末尾2行 2 4 6 8 4 8 12 16 $ tail -f c.txt #打印文件中新寫入的行,對監控日志非常有用 a b c d 1 2 3 4 2 4 6 8 4 8 12 16 #此處會一直等待新內容 $ tail -f c.txt --pid=31741 #在特定的進程結束時終結tail命令 #這里的31741進程對應執行的run.sh腳本,會寫入文件10次,run.sh結束后,tail命令也會結束 $cat run.sh for i in {0..10};do sleep 10 echo $(date) >> c.txt done $ tail -f g.txt --retry #執行tail時g.txt不存在,執行run.sh創建出來並持續寫入 $ cat run.sh for i in {0..10};do sleep 10 echo $(date) >> g.txt done #tail命令輸出的內容 tail: warning: --retry only effective for the initial open tail: cannot open 'g.txt' for reading: No such file or directory tail: 'g.txt' has appeared; following end of new file Tue May 12 09:50:25 CST 2020 Tue May 12 09:50:35 CST 2020 ...... $ tail -f g.txt #執行時g.txt不存在且不加--retry選項 tail: cannot open 'g.txt' for reading: No such file or directory tail: no files remaining
6. file命令
file命令用於接收一個文件作為參數並執行某些測試,以確定正確的文件類型。
#基本方法 $ file /etc/inittab /etc/inittab: ASCII text $ file /etc/init.d/network /etc/init.d/network: Bourne-Again shell script, ASCII text executable $ file /usr/bin/file /usr/bin/file: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=1304f666b8d56dc664e73696a426431aa46ec676, stripped $ file /etc /etc: directory #使用-i選項,可以MIME類型的格式顯示文件類型信息 $ file -i /etc/inittab /etc/inittab: text/plain; charset=us-ascii $ file -i /etc/init.d/network /etc/init.d/network: text/x-shellscript; charset=us-ascii $ file -i /usr/bin/file /usr/bin/file: application/x-executable; charset=binary $ file -i /etc /etc: inode/directory; charset=binary #使用-N選項,輸出的隊列可以在文件名之后無空白填充的形式顯示,對比如下 $ file * a: directory b: directory c.txt: ASCII text d.txt: ASCII text e.txt: empty f.txt: ASCII text run.sh: ASCII text $ file -N * a: directory b: directory c.txt: ASCII text d.txt: ASCII text e.txt: empty f.txt: ASCII text run.sh: ASCII text
7. wc命令
wc命令用於查看文件的行數、單詞數和字符數等信息。
格式如:wc filename
輸出格式如:X Y Z filename,X——行數,Y——單詞數,Z——字節數,filename——文件名
$ wc c.txt 37 214 991 c.txt $ wc -l c.txt #使用-l,只顯示行數 37 c.txt $ wc -w c.txt #使用-w,只顯示單詞數 214 c.txt $ wc -c c.txt #使用-c,只顯示字節數 991 c.txt $ wc -L c.txt #使用-L,顯示文件的最長的行的長度 28 c.txt
8. find命令
find命令用於根據指定的參數搜索和定位文件和目錄的列表。
# find /etc -name inittab #指定目錄,通過-name指定文件名查找 /etc/inittab # cd /etc/ # find . -name inittab #在當前目錄下茶軸 ./inittab $ find . -iname c.txt #在當前目錄下,不區分大小寫的c.txt ./C.txt ./c.txt $ find . -type d -name test #在當前目錄下,查找名為test的目錄 ./a/test ./b/test $ find . -type f -name "*.txt" #在當前目錄下,查找txt文件 $ find . -type f -perm 0775 #在當前目錄下,查找文件權限是755的所有文件 ./run.sh $ find . -type f ! -perm 0775 #在當前目錄下,查找文件權限不是755的所有文件 $ find . -type f -perm 775|xargs chmod 755 #在當前目錄下,查找文件權限是755的所有文件,並將其權限改為755 # find /etc -type f ! -perm /a+w #查找/etc目錄下所有只讀文件 # find ~ -type f -perm /a+x #查找當前用戶主目錄下所有可執行文件 $ find ./b -type f -name "b*" -exec rm {} \; #查找b目錄下所有b開頭的文件並刪除 $ find . -type f -empty #查找當前目錄下所有空文件 $ find . -type d -empty #查找當前目錄下所有空目錄 $ find /tmp/ -type f -name ".*" #查找tmp目錄下所有隱藏文件 # find /tmp/ -user root #查找tmp目錄下所有者是root的文件和目錄 # find /tmp/ -group root #查找tmp目錄下用戶組是root的文件和目錄 $ find ~ -type f -mtime 3 #查找當前用戶主目錄下3天前修改的文件 $ find ~ -type f -mtime 30 #查找當前用戶主目錄下30天前修改的文件 $ find ~ -type f -mtime -3 #查找當前用戶主目錄下3天內修改的文件 $ find ~ -type f -mtime +1 -mtime -6 #查找當前用戶主目錄下1天前6天內修改的文件 $ find ~ -type f -cmin -60 #查找當前用戶主目錄下1小時內變更過的文件 $ find ~ -type f -amin -60 #查找當前用戶主目錄下1小時內訪問過的文件 $ find . -type f -size +10c #查找當前目錄下大於10個字節的文件 $ find . -type f -size +10c -size -20c #查找當前目錄下大於10個字節且小於20個字節的文件
注:可以看到find與管道命令配合使用進行操作。
本節結束