find命令總結

1.1 功能說明

查找文件

1.2 語法格式

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

參數

說明

簡解

-type

按文件類型查找

f:表示文件   d:表示目錄  c:字符類型  b:塊設備  ssocket):socket文件  l:鏈接文件

-name

按文件名稱查找

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

-maxdepth

查找目錄深度


-mtime

按修改時間查


-ctime

按照改變時間查

屬性改變,內容改變

-atime

按照訪問時間查


-size

文件大小


1.3 實踐操作

實例1-1  /data目錄的oldboy,txt刪除(使用find

wKiom1gwzhSAAvfIAABH8jQWAJA967.png

實例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目錄下大於1Mtxt文件,並刪除

命令: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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章