linux基礎命令介紹三:文件搜索及其它

一、linux中包含大量的文件,對於文件查找,linux提供了find命令。

find是一個很是有效的工具,它能夠遍歷目標目錄甚至整個文件系統來查找某些文件或目錄:linux

find [path...] [expression]

其中expression包括三種:optionstestsactions。多個表達式之間被操做符分隔,當操做符被省略時,表示使用了默認操做符-and
當表達式中不包含任何actions時,默認使用-print,也就是打印出搜索到的全部文件,用換行分隔。
其實能夠將三種表達式均視爲選項,表示對搜索的某種限制(如-maxdepth表示搜索路徑的最大深度)、或對找到的目標文件的某種測試(如-readable判斷是否可讀)、或對結果採起的某種動做(如-print)。算法

選項-name pattern搜索文件名shell

[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通配符(見上一篇),但爲了不混淆,處於選項後的通配符須要被引號引發來。express

選項-maxdepth n指定搜索路徑的最大深度編程

[root@centos7 ~]# find /root -maxdepth 1 -name "file?"  #注意表達式之間的隱含操做符 -and
/root/file1
[root@centos7 ~]#

本例中指定最大深度爲1,表示只搜索/root目錄,而不進入任何它的子目錄去搜索。
和此選項相對應,-mindepth表示指定搜索路徑的最小深度。segmentfault

選項-user name按照文件屬主來查找文件centos

[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小時bash

[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的可能值包括bcdpfls。它們和命令ls顯示的文件類型一致(見基礎命令介紹一),f表明普通文件。

[root@centos7 temp]# ln -s file13 file14
[root@centos7 temp]# ls -l file14
lrwxrwxrwx 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 file13
lrwxrwxrwx 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輸出文件名,但不用任何字符分隔它們。當文件名中包含特殊字符時使用。能夠配合帶選項-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 ./file13
lrwxrwxrwx 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執行命令。

二、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 ls
ls: 沒法訪問./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/passwd
root:x:0:0:root:/root:/bin/bash
[root@centos7 temp]# head -n1 /etc/passwd|xargs -d ":" echo -n
root x 0 0 root /root /bin/bash
[root@centos7 temp]#

選項-P指定最大進程數,默認進程數爲1,多個進程併發執行。

三、date 打印或設置系統時間

date [OPTION]... [+FORMAT]

當沒有任何參數時表示顯示當前時間:

[root@centos7 temp]# date
2016年 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@2147483647
2038年 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]# date
2016年 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 +%s
1477987540
[root@centos7 temp]#

如輸出今天星期幾:

[root@centos7 temp]# date +%A
星期二
[root@centos7 temp]#

其餘格式請自行man

四、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後綴以前將不能被解壓。

五、zcat 將壓縮的文件內容輸出到標準輸出

[root@centos7 temp]# zcat hello.gz 
hello world
[root@centos7 temp]#

zcat讀取被gzip壓縮的文件,只需文件格式正確,不須要文件名具備.gz的後綴。

六、bzip2 壓縮解壓文件

bzip2 [OPTION]... [FILE]...

命令bzip2gzip相似都是壓縮命令,只是使用的壓縮算法不同,一般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

選項-ngzip用法一致,表示壓縮比。

七、tar 打包壓縮文件

tar [OPTION...] [FILE]...

命令gzipbzip2均不支持壓縮目錄(雖然gzip能夠用選項-r到目錄內去壓縮,但仍沒法壓縮目錄),用tar命令能夠將目錄歸檔,而後利用壓縮命令進行壓縮:

[root@centos7 temp]# tar -cf tmp.tar tmp/
[root@centos7 temp]# ls -l
總用量 18256
drwxr-xr-x 2 root root        6 11月  1 16:23 abcd
-rwxr-xr-x 1 root root       12 10月 28 17:24 test.sh
drwxr-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 abcd
abcd/file15
[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
-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/file10
abcd/file11
abcd/file12
abcd/file13
abcd/file15
[root@centos7 temp]# ls abcd   #這裏是按兩次tab鍵的補全結果
abcd/     abcd.tar  
[root@centos7 temp]#

選項-O解壓文件至標準輸出

[root@centos7 temp]# tar -xf abcd.tar -O 
hello
hello
hello
hello
[root@centos7 temp]#  #注意這裏輸出了每一個歸檔文件的內容
[root@centos7 temp]# tar -xf abcd.tar -O | xargs echo
hello 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     tmp
28K     tmp.tar.bz2
180K    tmp.tar.gz
40K     tmp.tar.xz
[root@centos7 temp]#

本例中分別使用三種壓縮格式進行壓縮,能夠看到使用命令bzip2的壓縮比最高,命令gzip的壓縮比最低。在執行壓縮文件時,壓縮時間也是咱們考量的一個重要因素。默認時,使用gzip最快,xz最慢。
對於這三種格式的壓縮文件進行解壓,只需將選項中-c換成-x便可。

選項-X FILE 排除匹配文件FILE中所列模式的文件:

[root@centos7 abcd]# cat file
file10
file13
[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 file
file1[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 file15
drwxr-xr-x 2 root root 425984 11月  1 17:08 tmp

只解壓指定文件:

[root@centos7 temp]# tar zxvf tmp.tar.gz -C abcd/ file1[23]
file12
file13
[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 -O
BLOG ADDRESS IS "https://segmentfault.com/blog/learnning"
[root@centos7 temp]#

本文講述了linux中關於文件搜索和歸檔壓縮等相關的命令及部分選項用法,都是在系統管理過程當中常常要使用的。需熟練使用。

相關文章
相關標籤/搜索