天天看點

find指令總結

查找檔案

find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...][expression]

參數

說明

簡解

-type

按檔案類型查找

f:表示檔案   d:表示目錄  c:字元類型  b:塊裝置  s(socket):socket檔案  l:連結檔案

-name

按檔案名稱查找

最好用雙引号括起來。!取反

-maxdepth

查找目錄深度

-mtime

按修改時間查

-ctime

按照改變時間查

屬性改變,内容改變

-atime

按照通路時間查

-size

檔案大小

執行個體1-1  把/data目錄的oldboy,txt删除(使用find)

find指令總結

執行個體1-2  分别查找出/data最近7天的日志檔案,第7天的日志,7天前的日志

指令:find /data –type f –mtime -7 (最近7天)

代碼

[root@ben-test data]# find /data -type f -mtime -7

/data/access_www_2016-09-09.log

/data/access_www_2016-09-10.log

/data/access_www_2016-09-13.log

/data/access_www_2016-09-14.log

/data/access_www_2016-09-12.log

/data/access_www_2016-09-11.log

指令:find /data–type f –mtime 7   (第7天)

[root@ben-test data]# find /data -type f -mtime 7

/data/access_www_2016-09-08.log

指令:find /data–type f –mtime +7   (7天前)

[root@ben-test data]# find /data -type f -mtime +7

/data/access_www_2016-09-03.log

/data/access_www_2016-09-07.log

/data/access_www_2016-09-01.log

/data/access_www_2016-09-05.log

/data/access_www_2016-09-06.log

/data/access_www_2016-09-04.log

/data/access_www_2016-09-02.log

執行個體1-3  删除一個目錄下的所有檔案,但保留一個指定檔案

指令:touch file{1..10}

find /data/ -typef ! –name “file10”| xargs rm -f

[root@ben-test ~]# touch /data/file{1..10}

[root@ben-test ~]# find /data -type f ! -name"file10"   

/data/file6

/data/file3

/data/file2

/data/file7

/data/file1

/data/file5

/data/file8

/data/file4

/data/file9

[root@ben-test ~]# find /data -type f ! -name"file10" |xargs rm -f

[root@ben-test ~]# find /data -type f ! -name"file10"

執行個體1-4  查找出/data目錄下大于1M的txt檔案,并删除

指令:find /data –typef –name “*.txt” –size +1M |xargs rm –f

代碼:

[root@oldboy ~]# find /data -type f -size +1M|xargs ls -lh  

-rw-r--r-- 1 root root 1.3M Oct 24 16:51/data/a.txt

[root@oldboy ~]# find /data -type f -size +1M

/data/a.txt

[root@oldboy ~]# find /data -type f -size +1M|xargs rm –f