1、linux中包含大量的文件,對于文件查找,linux提供了find命令。
find是一個非常有效的工具,它可以遍歷目標目錄甚至整個文件系統來查找某些文件或目錄:
find [path...] [expression]
其中expression包括三種:options、tests和actions。多個表達式之間被操作符分隔,當操作符被省略時,表示使用了默認操作符-and。
當表達式中不包含任何actions時,默認使用-print,也就是打印出搜索到的所有文件,用換行分隔。
其實可以將三種表達式均視為選項,表示對搜索的某種限制(如-maxdepth表示搜索路徑的最大深度)、或對找到的目標文件的某種測試(如-readable判斷是否可讀)、或對結果采取的某種動作(如-print)。
選項-name pattern搜索文件名:
[root@centos7 temp]# find /root/* -name "file?" /root/file1/root/temp/file1/root/temp/file2/root/temp/file3/root/temp/file4/root/temp/file5/root/temp/file6/root/temp/file7/root/temp/file8/root/temp/file9[root@centos7 temp]#
此例中搜索目錄/root下所有文件,找出匹配file?的文件名,同時由于沒有指定action,所以使用默認的-print將結果打印出來。find命令中,搜索路徑和某些文件名的表示可以使用shell通配符(見上一篇),但為了避免混淆,處于選項后的通配符需要被引號引起來。
選項-maxdepth n指定搜索路徑的最大深度:
[root@centos7 ~]# find /root -maxdepth 1 -name "file?" #注意表達式之間的隱含操作符 -and/root/file1[root@centos7 ~]#
本例中指定最大深度為1,表示只搜索/root目錄,而不進入任何它的子目錄去搜索。
和此選項相對應,-mindepth表示指定搜索路徑的最小深度。
選項-user name按照文件屬主來查找文件:
[root@centos7 ~]# find /root/temp -name "file?" -user learner/root/temp/file1/root/temp/file2[root@centos7 ~]#
或者類似選項-uid n表示按文件屬主的uid,-gid n表示按文件所屬組的gid,-group name表示按文件所屬組。
選項-mtime n 文件上次內容被修改距離現在n*24小時:
[root@centos7 temp]# ls -lt file1?-rw-r--r-- 1 root root 64 10月 27 15:06 file11-rw-r--r-- 1 root root 132 10月 27 13:28 file10-rw-r--r-- 1 root root 22 10月 26 21:31 file12-rw-r--r-- 1 root root 137 10月 12 16:42 file13[root@centos7 temp]# find . -name "file1?" -mtime +5 #五天前./file13[root@centos7 temp]# [root@centos7 temp]# find . -name "file1?" -mtime -5 #五天內./file10./file11[root@centos7 temp]#[root@centos7 temp]# find . -name "file1?" -mtime 5 #剛好五天./file12[root@centos7 temp]#
本例中使用了命令ls的選項-t對文件的時間進行排序,最近被修改的文件在前。選項-mtime n中n可以表示成:
+n 表示大于n
-n 表示小于n
n 表示等于n
還有其他時間(如atime,ctime)的比較,用法相同。
選項-newer file表示搜索到的文件比指定的file要‘新'(上次內容被修改離現在時間更短):
[root@centos7 temp]# find . -name "file1?" -newer file12./file10./file11[root@centos7 temp]#
選項-path pattern文件名匹配pattern(通配符):
[root@centos7 temp]# find . -name "file1?" -path "./file1[13]"./file11./file13[root@centos7 temp]#
注意pattern匹配時不會對/和.進行特殊處理。
通常-path會配合選項-prune使用,表示對某目錄的排除:
[root@centos7 temp]# find . -name "file*"./file10./file12./file11./tmp/file./file13[root@centos7 temp]#[root@centos7 temp]# find . -path "./tmp" -prune -o -name "file*" -print./file10./file12./file11./file13[root@centos7 temp]#
這里的-o表示或者,它和之前所說的-and都是操作符。表示表達式之間的邏輯關系。本例中可以理解為:如果目錄匹配./tmp則執行-prune跳過該目錄,否則匹配-name指定的文件并執行-print。
除這兩個操作符外,操作符!或-not表示邏輯非,操作符(...)和數學運算中的括號類似,表示提高優先級:
[root@centos7 temp]# find . ! -path "./tmp*" -name "file*" ./file10./file12./file11./file13[root@centos7 temp]##排除多個目錄:[root@centos7 temp]# find . /( -path "./tmp" -o -path "./abcd" /) -prune -o -name "file*" -print./file10./file12./file11./file13[root@centos7 temp]#
注意這里的(...)操作符需要被轉義(為避免被shell解釋為其他含義),在符號前加上反斜線'/'。(關于shell中的轉義或引用我們會在講bash編程時詳述)
選項-type x表示搜索類型為x的文件,其中x的可能值包括b、c、d、p、f、l、s。它們和命令ls顯示的文件類型一致(見基礎命令介紹一),f代表普通文件。
[root@centos7 temp]# ln -s file13 file14[root@centos7 temp]# ls -l file14lrwxrwxrwx 1 root root 6 11月 1 12:29 file14 -> file13[root@centos7 temp]# find . -type l./file14[root@centos7 temp]#
選項-perm mode表示搜索特定權限的文件:
[root@centos7 temp]# chmod 777 file14[root@centos7 temp]# ls -l file1[3-4]-rwxrwxrwx 1 root root 137 10月 12 16:42 file13lrwxrwxrwx 1 root root 6 11月 1 12:29 file14 -> file13[root@centos7 temp]# [root@centos7 temp]# find . -perm 777./file13./file14[root@centos7 temp]#
或表示成:
[root@centos7 temp]# find . -perm -g=rwx #表示文件所屬組的權限是可讀、可寫、可執行。./file13./file14[root@centos7 temp]#
選項-size n表示搜索文件大小
[root@centos7 temp]# find . -path "./*" -size +100c./file10./file13[root@centos7 temp]#
此例中+100c表示當前目錄下大于100 bytes的文件,n和前面表示時間的方式類似(+n,-n,n),n后面的字符還包括:
b 單位為512 bytes的塊(n后面沒有后綴時的默認單位)
k 1024 bytes
M 1048576 bytes
G 1073741824 bytes
選項-print0類似-print輸出文件名,但不用任何字符分隔它們。當文件名中包含特殊字符時使用??梢耘浜蠋нx項-0的命令xargs一起使用(后述)。
選項-exec command ;表示要執行的命令
-exec后可以跟任意shell命令來對搜索到的文件做進一步的處理,在command和分號之間都被視為command的參數,其中用{}代表被搜索到的文件。分號需要被轉義。
如對搜索到的文件執行命令ls -l:
[root@centos7 temp]# find . -name "file*" -exec ls -l {} /;-rw-r--r-- 1 root root 132 10月 27 13:28 ./file10-rw-r--r-- 1 root root 22 10月 26 21:31 ./file12-rw-r--r-- 1 root root 64 10月 27 15:06 ./file11-rw-r--r-- 1 root root 67 10月 31 17:50 ./tmp/file-rw-r--r-- 1 root root 0 11月 1 12:05 ./abcd/file15-rwxrwxrwx 1 root root 137 10月 12 16:42 ./file13lrwxrwxrwx 1 root root 6 11月 1 12:29 ./file14 -> file13
-exec選項后的命令是在啟動find所在的目錄內執行的,并且對于每個搜索到的文件,該命令都執行一次,而不是把所有文件列在命令后面只執行一次。
舉例說明下其中的區別:
#命令echo只執行一次[root@centos7 temp]# echo ./file11 ./file12 ./file13./file11 ./file12 ./file13#命令echo執行了三次[root@centos7 temp]# find . -name "file1[1-3]" -exec echo {} /;./file12./file11./file13[root@centos7 temp]#
當使用格式-exec command {} +時表示每個文件都被追加到命令后面,這樣,命令就只被執行一次了:
[root@centos7 temp]# find . -name "file1[1-3]" -exec echo {} +./file12 ./file11 ./file13[root@centos7 temp]#
但有時會出現問題:
[root@centos7 temp]# find . -name "file1[1-3]" -exec mv {} abcd/ +find: 遺漏“-exec”的參數[root@centos7 temp]#
因為這里文件被追加于目錄abcd/的后面,導致報錯。
同時,使用格式-exec command {} +還可能會造成被追加的文件數過多,超出了操作系統對命令行長度的限制。
使用-exec可能會有安全漏洞,通常使用管道和另一個命令xargs來代替-exec執行命令。
2、xargs 從標準輸入中獲得命令的參數并執行
xargs從標準輸入中獲得由空格分隔的項目,并執行命令(默認為/bin/echo)
選項-0將忽略項目的分隔符,配合find的選項-print0,處理帶特殊符號的文件。
[root@centos7 temp]# find . -name "file*" -print0 | xargs -0 ls -l-rw-r--r-- 1 root root 132 10月 27 13:28 ./file10-rw-r--r-- 1 root root 64 10月 27 15:06 ./file11-rw-r--r-- 1 root root 22 10月 26 21:31 ./file12-rwxrwxrwx 1 root root 137 10月 12 16:42 ./file13-rw-r--r-- 1 root root 0 11月 1 14:45 ./file 14 #注意此文件名中包含空格
當不用時:
[root@centos7 temp]# find . -name "file*" | xargs lsls: 無法訪問./file: 沒有那個文件或目錄ls: 無法訪問14: 沒有那個文件或目錄./file10 ./file11 ./file12 ./file13
選項-I string為輸入項目指定替代字符串:
[root@centos7 temp]# ls abcd/[root@centos7 temp]# find . -name "file*" | xargs -I{} mv {} abcd/[root@centos7 temp]# ls abcd/file10 file11 file12 file13[root@centos7 temp]#
這里的意思是說使用-I后面的字符串去代替輸入項目,這樣就可以把它們作為整體放到命令的任意位置來執行了。也避免了-exec command {} +的錯誤。
選項-d指定輸入項目的分隔符:
[root@centos7 temp]# head -n1 /etc/passwdroot:x:0:0:root:/root:/bin/bash[root@centos7 temp]# head -n1 /etc/passwd|xargs -d ":" echo -nroot x 0 0 root /root /bin/bash[root@centos7 temp]#
選項-P指定最大進程數,默認進程數為1,多個進程并發執行。
3、date 打印或設置系統時間
date [OPTION]... [+FORMAT]
當沒有任何參數時表示顯示當前時間:
[root@centos7 temp]# date2016年 11月 01日 星期二 15:30:46 CST[root@centos7 temp]#
選項-d string按描述字符串顯示時間(例子中字符串表示距離1970-01-01零點的秒數):
[root@centos7 temp]# date --date='@2147483647'2038年 01月 19日 星期二 11:14:07 CST
或者:
[root@centos7 temp]# date -d@21474836472038年 01月 19日 星期二 11:14:07 CST
-d后面的字符串還可以是:
[root@centos7 temp]# date -d "-1 day"2016年 10月 31日 星期一 16:11:27 CST
表示昨天
又如明年表示為:
[root@centos7 temp]# date -d "1 year"2017年 11月 01日 星期三 16:12:27 CST
選項-s設置系統時間:
[root@centos7 temp]# date -s "2016-11-01 15:49"2016年 11月 01日 星期二 15:49:00 CST[root@centos7 temp]# date2016年 11月 01日 星期二 15:49:03 CST
由于linux系統啟動時將讀取CMOS來獲得時間,系統會每隔一段時間將系統時間寫入CMOS,為避免更改時間后系統的立即重啟造成時間沒有被寫入CMOS,通常設置完時間后會使用命令clock -w將系統時間寫入到CMOS中。
date命令中由FORMAT來控制輸出格式,加號+在格式之前表示格式開始:
[root@centos7 temp]# date "+%Y-%m-%d %H:%M:%S"2016-11-01 16:00:45[root@centos7 temp]#
本例中格式被雙引號引起來以避免被shell誤解,其中:
%Y 表示年
%m 表示月
%d 表示天
%H 表示小時
%M 表示分鐘
%S 表示秒
還可以指定很多其他格式如只輸出當前時間:
[root@centos7 temp]# date "+%T"16:03:50[root@centos7 temp]#
如輸出距離1970-01-01零點到現在時間的秒數:
[root@centos7 temp]# date +%s1477987540[root@centos7 temp]#
如輸出今天星期幾:
[root@centos7 temp]# date +%A星期二[root@centos7 temp]#
其他格式請自行man
4、gzip 壓縮或解壓文件
gzip [OPTION]... [FILE]...
當命令后直接跟文件時,表示壓縮該文件:
[root@centos7 temp]# ls -l file1*-rw-r--r-- 1 root root 132 10月 27 13:28 file10-rw-r--r-- 1 root root 64 10月 27 15:06 file11-rw-r--r-- 1 root root 22 10月 26 21:31 file12-rw-r--r-- 1 root root 137 10月 12 16:42 file13[root@centos7 temp]# [root@centos7 temp]# gzip file10 file11 file12 file13 [root@centos7 temp]# ls -l file1* -rw-r--r-- 1 root root 75 10月 27 13:28 file10.gz-rw-r--r-- 1 root root 49 10月 27 15:06 file11.gz-rw-r--r-- 1 root root 44 10月 26 21:31 file12.gz-rw-r--r-- 1 root root 109 10月 12 16:42 file13.gz
壓縮后的文件以.gz結尾,gzip是不保留源文件的
選項-d表示解壓縮
[root@centos7 temp]# gzip -d *.gz[root@centos7 temp]# ls -l file1*-rw-r--r-- 1 root root 132 10月 27 13:28 file10-rw-r--r-- 1 root root 64 10月 27 15:06 file11-rw-r--r-- 1 root root 22 10月 26 21:31 file12-rw-r--r-- 1 root root 137 10月 12 16:42 file13
選項-r可以遞歸地進入目錄并壓縮里面的文件
選項-n指定壓縮級別,n為從1-9的數字。1為最快壓縮,但壓縮比最小;9的壓縮速度最慢,但壓縮比最大。默認時n為6。
[root@centos7 temp]# gzip -r9 ./tmp
當gzip后沒有文件或文件為-時,將從標準輸入讀取并壓縮:
[root@centos7 temp]# echo "hello world" | gzip >hello.gz[root@centos7 temp]# ls -l *.gz-rw-r--r-- 1 root root 32 11月 1 16:40 hello.gz
注意例子中gzip的輸出被重定向到文件hello.gz中,如果對此文件進行解壓,將會生成文件hello。如果被重定向的文件后綴不是.gz,文件名在被改成.gz后綴之前將不能被解壓。
5、zcat 將壓縮的文件內容輸出到標準輸出
[root@centos7 temp]# zcat hello.gz hello world[root@centos7 temp]#
zcat讀取被gzip壓縮的文件,只需文件格式正確,不需要文件名具有.gz的后綴。
6、bzip2 壓縮解壓文件
bzip2 [OPTION]... [FILE]...
命令bzip2和gzip類似都是壓縮命令,只是使用的壓縮算法不一樣,通常bzip2的壓縮比較高。本命令默認同樣不保留源文件,默認文件名后綴為.bz2:
[root@centos7 temp]# bzip2 file11[root@centos7 temp]# ls -l file11.bz2 -rw-r--r-- 1 root root 61 10月 27 15:06 file11.bz2
選項-k可使源文件保留:
[root@centos7 temp]# bzip2 -k file10 [root@centos7 temp]# ls -l file10*-rw-r--r-- 1 root root 132 10月 27 13:28 file10-rw-r--r-- 1 root root 96 10月 27 13:28 file10.bz2
選項-d表示解壓(若存在源文件則報錯):
[root@centos7 temp]# bzip2 -d file10.bz2 bzip2: Output file file10 already exists.[root@centos7 temp]# bzip2 -d file11.bz2[root@centos7 temp]# ls -l file11-rw-r--r-- 1 root root 64 10月 27 15:06 file11
選項-f表示強制覆蓋源文件:
[root@centos7 temp]# bzip2 -d -f file10.bz2[root@centos7 temp]# ls -l file10*-rw-r--r-- 1 root root 132 10月 27 13:28 file10
選項-n和gzip用法一致,表示壓縮比。
7、tar 打包壓縮文件
tar [OPTION...] [FILE]...
命令gzip和bzip2均不支持壓縮目錄(雖然gzip可以用選項-r到目錄內去壓縮,但仍無法壓縮目錄),用tar命令可以將目錄歸檔,然后利用壓縮命令進行壓縮:
[root@centos7 temp]# tar -cf tmp.tar tmp/[root@centos7 temp]# ls -l總用量 18256drwxr-xr-x 2 root root 6 11月 1 16:23 abcd-rwxr-xr-x 1 root root 12 10月 28 17:24 test.shdrwxr-xr-x 2 root root 425984 11月 1 17:08 tmp-rw-r--r-- 1 root root 18001920 11月 1 17:17 tmp.tar
例子中選項-c表示創建打包文件,-f tmp.tar表示指定打包文件名為tmp.tar,后面跟被打包目錄名tmp/。
選項-t列出歸檔內容
選項-v詳細地列出處理的文件
[root@centos7 temp]# ls -l abcd.tar -rw-r--r-- 1 root root 10240 11月 2 08:58 abcd.tar[root@centos7 temp]# tar -tvf abcd.tar drwxr-xr-x root/root 0 2016-11-02 08:57 abcd/-rw-r--r-- root/root 6 2016-11-02 08:57 abcd/file10-rw-r--r-- root/root 6 2016-11-02 08:57 abcd/file11-rw-r--r-- root/root 6 2016-11-02 08:57 abcd/file12-rw-r--r-- root/root 6 2016-11-02 08:57 abcd/file13
選項-u更新歸檔文件(update)。
[root@centos7 temp]# touch abcd/file15[root@centos7 temp]# tar uvf abcd.tar abcdabcd/file15[root@centos7 temp]# tar tvf abcd.tardrwxr-xr-x root/root 0 2016-11-02 08:57 abcd/-rw-r--r-- root/root 6 2016-11-02 08:57 abcd/file10-rw-r--r-- root/root 6 2016-11-02 08:57 abcd/file11-rw-r--r-- root/root 6 2016-11-02 08:57 abcd/file12-rw-r--r-- root/root 6 2016-11-02 08:57 abcd/file13-rw-r--r-- root/root 0 2016-11-02 09:07 abcd/file15
選項-x對歸檔文件進行提取操作。(解包)
[root@centos7 temp]# rm -rf abcd/[root@centos7 temp]# tar -xvf abcd.tar abcd/abcd/file10abcd/file11abcd/file12abcd/file13abcd/file15[root@centos7 temp]# ls abcd #這里是按兩次tab鍵的補全結果abcd/ abcd.tar [root@centos7 temp]#
選項-O解壓文件至標準輸出
[root@centos7 temp]# tar -xf abcd.tar -O hellohellohellohello[root@centos7 temp]# #注意這里輸出了每個歸檔文件的內容[root@centos7 temp]# tar -xf abcd.tar -O | xargs echohello hello hello hello[root@centos7 temp]#
選項-p保留文件權限(用于解包時)。
選項-j、-J、-z 用于壓縮。
其中-j使用命令bzip2,-J使用命令xz,-z使用命令gzip分別將歸檔文件進行壓縮解壓處理(命令tar后的選項可以省略-):
[root@centos7 temp]# tar zcf tmp.tar.gz tmp[root@centos7 temp]# tar jcf tmp.tar.bz2 tmp[root@centos7 temp]# tar Jcf tmp.tar.xz tmp[root@centos7 temp]# du -sh tmp*70M tmp28K tmp.tar.bz2180K tmp.tar.gz40K tmp.tar.xz[root@centos7 temp]#
本例中分別使用三種壓縮格式進行壓縮,可以看到使用命令bzip2的壓縮比最高,命令gzip的壓縮比最低。在執行壓縮文件時,壓縮時間也是我們考量的一個重要因素。默認時,使用gzip最快,xz最慢。
對于這三種格式的壓縮文件進行解壓,只需將選項中-c換成-x即可。
選項-X FILE 排除匹配文件FILE中所列模式的文件:
[root@centos7 abcd]# cat filefile10file13[root@centos7 abcd]# tar -X file -cf file.tar file*[root@centos7 abcd]# tar -tvf file.tar -rw-r--r-- root/root 14 2016-11-02 10:10 file-rw-r--r-- root/root 6 2016-11-02 10:02 file11-rw-r--r-- root/root 6 2016-11-02 10:02 file12-rw-r--r-- root/root 0 2016-11-02 09:07 file15
注意文件FILE中支持通配符匹配:
[root@centos7 abcd]# cat filefile1[2-3][root@centos7 abcd]# tar -X file -cf file.tar file*[root@centos7 abcd]# tar -tvf file.tar -rw-r--r-- root/root 11 2016-11-02 10:20 file-rw-r--r-- root/root 6 2016-11-02 10:02 file10-rw-r--r-- root/root 6 2016-11-02 10:02 file11-rw-r--r-- root/root 0 2016-11-02 09:07 file15
選項-C DIR改變至目錄DIR(用于解包時):
[root@centos7 temp]# tar zxf tmp.tar.gz -C abcd[root@centos7 temp]# ls -l abcd/總用量 688-rw-r--r-- 1 root root 11 11月 2 10:20 file-rw-r--r-- 1 root root 6 11月 2 10:02 file10-rw-r--r-- 1 root root 6 11月 2 10:02 file11-rw-r--r-- 1 root root 6 11月 2 10:02 file12-rw-r--r-- 1 root root 6 11月 2 10:02 file13-rw-r--r-- 1 root root 0 11月 2 09:07 file15drwxr-xr-x 2 root root 425984 11月 1 17:08 tmp
只解壓指定文件:
[root@centos7 temp]# tar zxvf tmp.tar.gz -C abcd/ file1[23]file12file13[root@centos7 temp]# ls -l abcd總用量 12-rw-r--r-- 1 root root 6 11月 16 15:26 file12-rw-r--r-- 1 root root 6 11月 16 15:26 file13
注意這里解壓時,指定文件不能在選項-C之前
如不想解壓壓縮包,但想查看壓縮包中某個文件的內容時,可以使用如下技巧:
[root@centos7 temp]# tar zxf tmp.tar.gz file -OBLOG ADDRESS IS "http://www.49028c.com/article/101263.htm"[root@centos7 temp]#
本文講述了linux中關于文件搜索和歸檔壓縮等相關的命令及部分選項用法,都是在系統管理過程中經常要使用的,需熟練使用。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答