Linux中find命令mtime參數與用法

來源:互聯網
上載者:User


大家在使用find命令中的mtime參數時候,會看到官方的解釋如下:

-mtime n
File's data was last modified n*24 hours ago.  See the comments for -atime to understand how rounding affects  the

interpretation of file modification times.

但是在各種參考的使用方式中有用+號,-號,不帶符號的用法,那麼這裡又有什麼區別呢?

注意這裡的n,如果n為帶有+號的值,意思為刪除n天前所有的檔案,比如n=+1且今天是15號,那麼刪除14號以前的資料,不包括14號,如果是負號(n=-1)則為刪除一天內的檔案,比如今天15號,那麼刪除15號的資料,如果是(n=-2)則代表刪除一天前到今天的所有資料,比如今天15號,那麼從14號開始刪除。如果不帶有符號,那麼則刪除指定前n天中這一天的資料,比如(n=1)且今天是15號,則刪除14號這一天所有資料。

注意這裡的一天是指當前系統時間算起的,而不是0-24小時算一天


·find   path   -option   [   -print ]   [ -exec   -ok   command ]   {} ;
find命令的參數;

pathname: find命令所尋找的目錄路徑。例如用.來表示目前的目錄,用/來表示系統根目錄。
-print: find命令將匹配的檔案輸出到標準輸出。
-exec: find命令對匹配的檔案執行該參數所給出的shell命令。相應命令的形式為'command' { } ;,注意{ }和;之間的空格。
-ok: 和-exec的作用相同,只不過以一種更為安全的模式來執行該參數所給出的shell命令,在執行每一個命令之前,都會給出提示,讓使用者來確定是否執行。

#-print 將尋找到的檔案輸出到標準輸出
#-exec   command   {} ;      —–將查到的檔案執行command操作,{} 和 ;之間有空格
#-ok 和-exec相同,只不過在操作前要詢使用者
例:find . -name .svn | xargs rm -rf
====================================================

-name   filename             #尋找名為filename的檔案
-perm                        #按執行許可權來尋找
-user    username             #按檔案屬主來尋找
-group groupname            #按組來尋找
-mtime   -n +n                #按檔案更改時間來尋找檔案,-n指n天以內,+n指n天以前
-atime    -n +n               #按檔案訪問時間來查GIN: 0px">

-ctime    -n +n              #按檔案建立時間來尋找檔案,-n指n天以內,+n指n天以前

-nogroup                     #查無有效屬組的檔案,即檔案的屬組在/etc/groups中不存在
-nouser                     #查無有效屬主的檔案,即檔案的屬主在/etc/passwd中不存
-newer   f1 !f2              找檔案,-n指n天以內,+n指n天以前
-ctime    -n +n               #按檔案建立時間來尋找檔案,-n指n天以內,+n指n天以前
-nogroup                     #查無有效屬組的檔案,即檔案的屬組在/etc/groups中不存在
-nouser                      #查無有效屬主的檔案,即檔案的屬主在/etc/passwd中不存
-newer   f1 !f2               #查更改時間比f1新但比f2舊的檔案
-type    b/d/c/p/l/f         #查是塊裝置、目錄、字元裝置、管道、符號連結、普通檔案
-size      n[c]               #查長度為n塊[或n位元組]的檔案
-depth                       #使尋找在進入子目錄前先行尋找完本目錄
-fstype                     #查更改時間比f1新但比f2舊的檔案
-type    b/d/c/p/l/f         #查是塊裝置、目錄、字元裝置、管道、符號連結、普通檔案
-size      n[c]               #查長度為n塊[或n位元組]的檔案
-depth                       #使尋找在進入子目錄前先行尋找完本目錄
-fstype                      #查位於某一類型檔案系統中的檔案,這些檔案系統類型通常可 在/etc/fstab中找到
-mount                       #查檔案時不跨越檔案系統mount點
-follow                      #如果遇到符號連結檔案,就跟蹤連結所指的檔案
-cpio                %;      #查位於某一類型檔案系統中的檔案,這些檔案系統類型通常可 在/etc/fstab中找到
-mount                       #查檔案時不跨越檔案系統mount點
-follow                      #如果遇到符號連結檔案,就跟蹤連結所指的檔案
-cpio                        #對匹配的檔案使用cpio命令,將他們備份到磁帶裝置中
-prune                       #忽略某個目錄

=====================================================
$find   ~   -name   "*.txt"   -print    #在$HOME中查.txt檔案並顯示
$find   .    -name   "*.txt"   -print
$find   .    -name   "[A-Z]*"   -print   #查以大寫字母開頭的檔案
$find   /etc   -name   "host*"   -print #查以host開頭的檔案
$find   .   -name   "[a-z][a-z][0–9][0–9].txt"    -print   #查以兩個小寫字母和兩個數字開頭的txt檔案
$find .   -perm   755   -print
$find   .   -perm -007   -exec ls -l {} ;   #查所有使用者都可讀寫執行的檔案同-perm 777
$find   . -type d   -print
$find   .   !   -type   d   -print
$find   .   -type l   -print

$find   .   -size   +1000000c   -print        #查長度大於1Mb的檔案
$find   .   -size   100c         -print       # 查長度為100c的檔案
$find   .   -size   +10   -print              #查長度超到期作廢10塊的檔案(1塊=512位元組)

$cd /
$find   etc   home   apps    -depth   -print   | cpio   -ivcdC65536   -o   /dev/rmt0
$find   /etc -name "passwd*"   -exec grep   "cnscn"   {}   ;   #看是否存在cnscn使用者
$find . -name "yao*"   | xargs file
$find   . -name "yao*"   |   xargs   echo    "" > /tmp/core.log
$find   . -name "yao*"   | xargs   chmod   o-w

======================================================

find   -name april*                     在目前的目錄下尋找以april開始的檔案
find   -name   april*   fprint file        在目前的目錄下尋找以april開始的檔案,並把結果輸出到file中
find   -name ap* -o -name may*   尋找以ap或may開頭的檔案
find   /mnt   -name tom.txt   -ftype vfat   在/mnt下尋找名稱為tom.txt且檔案系統類型為vfat的檔案
find   /mnt   -name t.txt ! -ftype vfat   在/mnt下尋找名稱為tom.txt且檔案系統類型不為vfat的檔案
find   /tmp   -name wa* -type l            在/tmp下尋找名為wa開頭且類型為符號連結的檔案
find   /home   -mtime   -2                 在/home下查最近兩天內改動過的檔案
find /home    -atime -1                  查1天之內被存取過的檔案
find /home -mmin    +60                  在/home下查60分鐘前改動過的檔案
find /home   -amin   +30                  查最近30分鐘前被存取過的檔案
find /home   -newer   tmp.txt             在/home下查更新時間比tmp.txt近的檔案或目錄
find /home   -anewer   tmp.txt            在/home下查存取時間比tmp.txt近的檔案或目錄
find   /home   -used   -2                  列出檔案或目錄被改動過之後,在2日內被存取過的檔案或目錄
find   /home   -user cnscn                列出/home目錄內屬於使用者cnscn的檔案或目錄
find   /home   -uid   +501                  列出/home目錄內使用者的識別碼大於501的檔案或目錄
find   /home   -group   cnscn              列出/home內組為cnscn的檔案或目錄
find   /home   -gid 501                   列出/home內組id為501的檔案或目錄
find   /home   -nouser                    列出/home內不屬於本機使用者的檔案或目錄
find   /home   -nogroup                   列出/home內不屬於本機群組的檔案或目錄
find   /home    -name tmp.txt    -maxdepth   4   列出/home內的tmp.txt 查時深度最多為3層
find   /home   -name tmp.txt   -mindepth   3   從第2層開始查
find   /home   -empty                     尋找大小為0的檔案或空目錄
find   /home   -size   +512k                查大於512k的檔案
find   /home   -size   -512k               查小於512k的檔案
find   /home   -links   +2                查硬串連數大於2的檔案或目錄
find   /home   -perm   0700                查許可權為700的檔案或目錄
find   /tmp   -name tmp.txt   -exec cat {} ;
find   /tmp   -name   tmp.txt   -ok   rm {} ;

find    /   -amin    -10     # 尋找在系統中最後10分鐘訪問的檔案
find    /   -atime   -2        # 尋找在系統中最後48小時訪問的檔案
find    /   -empty             # 尋找在系統中為空白的檔案或者檔案夾
find    /   -group   cat        # 尋找在系統中屬於 groupcat的檔案
find    /   -mmin   -5         # 尋找在系統中最後5分鐘裡修改過的檔案
find    /   -mtime   -1       #尋找在系統中最後24小時裡修改過的檔案
find    /   -nouser           #尋找在系統中屬於作廢使用者的檔案
find    /   -user    fred     #尋找在系統中屬於FRED這個使用者的檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.