天天看點

linux檔案查找利器 非find莫屬

<a href="http://467754239.blog.51cto.com/4878013/1620000" target="_blank">昨天看到群裡在很多群友讨論find指令,那麼今天我就和大家聊聊工作中的find指令的使用:</a>

一、find文法

Usage:find PATHNAME [-option] [-exec|-ok command] {} \;

PATHNAME:查找的路徑名

[option]:可選擇要比對的參數

[-exec|-ok command]:将查找的檔案執行command操作

{}:查找到的檔案名

\;:反斜杠代表轉義,分号代表結束符

二、find參數

-name

1

<code>按檔案名字查找</code>

-perm

<code>按檔案權限查找</code>

-user

<code>按檔案屬主查找</code>

-group

<code>按檔案屬組查找</code>

-nouser

<code>查找無有效屬主的檔案,即檔案的屬組在</code><code>/etc/groups</code><code>中不存在</code>

-nogroup

<code>#查找無有效屬組的檔案,即檔案的屬主在/etc/groups中不存在</code>

-mtime(mmin)  -n +n

<code>按檔案更改時間來查找檔案,-n指n天(分鐘)以内,+n指n天(分鐘)以前</code>

-atime(amin)  -n +n

<code>按檔案通路時間來查找檔案,-n指n天(分鐘)以内,+n指n天(分鐘)以前</code>

-ctime(cmin)  -n +n

<code>按檔案建立時間來查找檔案,-n指n天(分鐘)以内,+n指n天(分鐘)以前</code>

-never f1 !f2

<code>查找更改時間比f1新但比f2舊的檔案</code>

-type [b| d| c| p| l| f]

<code>查找塊裝置,目錄,字元裝置,管道、符号連結、普通檔案</code>

-size n[c]

<code>查長度為n塊[或n位元組]的檔案</code>

-depth

<code>查找于某一類型檔案系統中的檔案,這些檔案系統類型通常可在</code><code>/etc/fstab</code><code>中找到</code>

-mount

<code>查檔案時不跨越檔案系統</code><code>mount</code><code>點</code>

-follow

<code>如果遇到符号連結檔案,就跟蹤連結所指的檔案</code>

-prune

<code>忽略某個目錄,如果同時使用了-depth選項,那麼-prune選項将被</code><code>find</code><code>指令忽略。</code>

三、find案列

1、查找指定目錄下大于300M的檔案,并将其移動到指定的路徑中

2

<code># find ~ -size +300M</code>

<code># find ~ -size +300M -exec mv {} ~/sql/ \;</code>

2、查找指定目錄下大于300M的檔案,并将其删除的兩種方式

<code># find ./ -size +300M -delete</code>

<code># find ./ -size +300M -exec rm -rf {} \;</code>

3、查找指定目錄下大于1G而小于1.5G的檔案

<code># find ./ -size +1G -size -1.5G</code>

4、查找指定目錄下非.sh結尾的檔案的兩種方法

<code># find ./ -not -name '*.sh'</code>

<code># find ./ ! -name '*.sh'</code>

5、删除7天前對資料庫的備份檔案

<code># find /bakcup/ -type f -name '*.sql' -mtime +7 -exec rm -rf {} \;</code>

6、查找目前目錄下以.sh結尾的檔案,但是排除子目錄為script目錄下的檔案

<code># find . -path './script' -prune -o -name '*.sh'</code>

7、查找目前目錄下以.py結尾的檔案,并将其修改為.pyc

<code># find ./ -name '*.py' -exec mv {} '{}c' \;</code>

8、查找目前目錄下所有檔案,并把檔案中的hello字元串替換成Hello

3

<code># echo 'hello world!' &gt; test01.txt</code>

<code># echo 'hello teacher!' &gt; test02.txt</code>

<code># find ./ -type f -exec sed -i 's@hello@Hello@g' {} \;   </code>

四、find搭配日志重點闡述

1、時間搜尋

atime(access time):通路時間;指的是檔案最後讀取的事件,可以使用touch指令更改目前時間

ctime(change time):變更時間;指的是檔案本身最後被變更的事件,變更動作是chmod、chgrp、mv等

mtime(modify time):修改時間,指的是檔案内容最後被修改的事件,修改動作echo重定向、vim等

2、時間文法

find PathName {-atime/-ctime/-mtime/-amin/-cmin/-mmin} [-/+]num

第一個參數:查找檔案的路徑名

第二個參數:前面的字母a、c、m分别表示通路、變更、修改;time為日期,min為分鐘

第三個參數:不該符号表示符合這個值得,'-'表示以内的,'+'表示以前的

3、時間小練習

查找目前目錄中,當天被通路過的檔案:時間機關為day

<code># find ./ -atime 0</code>

查找目前目錄中,距離此刻5分鐘以前10分鐘以内被修改過的檔案:時間機關為min

<code># find ./ -mmin -10 -min +5</code>

另外補充一個額外的指令:xargs

此指令的使用很類似于find的exec參數,比如:

<code># find ./ -type f | xargs chmod 755</code>

<code># find ./ -type f | xargs echo '' &gt; /root/filename.log</code>

<code># find ./ -type f | xargs rm -rf</code>

網上的一篇文章:

     本文轉自zys467754239 51CTO部落格,原文連結:http://blog.51cto.com/467754239/1620000,如需轉載請自行聯系原作者