搜尋
首頁php教程PHP开发Linux find 用法範例

Linux find 用法範例

Dec 14, 2016 pm 04:36 PM

Linux中find常見用法範例

·find   path   -option   [   -print ]   [ -exec   -ok  例如用.來表示目前目錄,用/來表示系統根目錄。

-print: find指令將符合的檔案輸出到標準輸出。

-exec: find指令對符合的檔案執行該參數所給予的shell指令。對應指令的形式為'command' { } ;,注意{ }和;之間的空格。

-ok: 和-exec的作用相同,只不過以更安全的模式來執行該參數所給予的shell指令,在執行每一個指令之前,都會給予提示,讓使用者來決定是否執行。


#-print 將查找到的檔案輸出到標準輸出
#-exec   command   {} ;      —–將查到的檔案執行command作業,{} 與;之間有空格
#-ok 和-exec相同,ok 和-exec相同,ok 和-exec相同,ok 和-exec相同,ok 和-exec相同,只不過在操作前要詢問使用者

例:find . -name .svn | xargs rm -rf

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

-name   filename             #尋找名為filename的檔案

-perm   

- user    username             #以記憶屬主尋找

-group groupname            #按文件更改時間來查找文件,-n指n天以內,+n指n天以前

-atime -n +n               #使用中檢查GIN: 0px">

-ctime    -n +n    

-nogroup                     #查無有效屬組的文件,即文件的屬組於/etc/groups屬主的文件,即文件的屬主在/etc/passwd中不存
-newer   f1 !f2              尋找文件,且-n指n天以內,+n指n天前 以內,+n指n天以前 
-nogroup                     #查無有效屬組的文件,文件的屬組在/    #查無有效屬主的文件,即文件的屬主在/etc/passwd中不記憶體
-newer   f1 !f2               #檢查上變更時間比f1新但比f2舊的檔案
-type    普通文件
-size      n[c]               #以n區塊[或n位元組]的檔案🎠   子目錄前先比f2舊的文件
-type    b/d/c/p/l/f         #以對區塊裝置、目錄、字元裝置、管道、符號連結、普通檔案
-size  [或n位元組]的文件
-depth                          #查位於某一類型檔案系統中的文件,這些檔案系統類型通常可在/etc/fstab中找到
-mount                       #在檢查文件中不跨越文件系統mount點
-follow   指的文件
-cpio                %;      #以某一型態檔案系統中所尋找的文件,這些文件系統類型通常可在/etc/fstab中找到
-mount                      ,   #如果遇到符號連結文件,就追蹤連結所指的文件
-cpio                        #對配對的檔案使用cpio指令,備份到磁帶裝置
-prune                       #忽略某目錄

================================================= ====
$find   ~   -name   "*.txt"   -print    #在$HOME中檢查.txt檔案並顯示
$find   .   遠. ]*"   -print   #查閱以大寫字母開頭的文件
$find   /etc   -name   "host*"   -print #查以host開頭的文件
$find   .  -print #查以host開頭的文件
$find   .  -print #查以host開頭的文件
$find   .  -name ][0–9].txt"    -print   #查詢以兩個小寫字母、兩個數字開頭的txt檔案
$find .   -perm   755   -print
$find  } . ;   #檢查所有使用者都可讀寫執行的文件同-perm 777
$find   . -type d   -print
$find   .   !   -type    .   - size   +1000000c   -print        #檢視長度大於1Mb的檔案

$find   .   -size   100c     .   -size       .   -size   +10   -print              #以重複期作廢10塊的檔案(1區塊=512位元組)


$cd /
$find   etc   home   apps    -depth   -print      -exec grep   "cnscn"   {} ;   #看是否有cnscn用戶

$find . -name "yao*"   | xargs file

$find   . -name "yao*"   |   xargs   echoano"$ *"   | xargs   chmod   o-w

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

find   -name april*                     在以目前目錄下尋找以april開始的檔案並且把結果輸出到file中
find   -name ap* -o -name may*   尋找以ap或may開頭的文件
find   /mnt   -name tom.txt   -ftype vfat   在/mnt下尋找名稱為tom.txt txt ! -ftype vfat   在/mnt下查找名稱為tom.txt且檔案系統類型不為vfat的檔案
find   /tmp   -name wa* -type l       文件
find   /home   -mtime   -2                 -2        查1天內存取過的文件
find /home -mmin    +60                  / home   -amin  +30                     在/home下查更新時間比tmp.txt近的文件或目錄
find /home   -anewer   tmp.txt            /home下檢視時間與tmp.近距離的文件或目錄🠎   /home      列出檔案或目錄被改動過之後,在2日內被存取的文件或目錄
find   /home   -user cnscn                -user cnscn            列出/home目錄內使用者的辨識碼大於501的文件或目錄
find   /home   -group   cnscn              所列/home內組為cnscn      列出/home內組id為501的文件或目錄
find   /home   -nouser                             /home內不屬於本地用戶的文件或目錄
find   /home   -nogroup                     列出/home內的tmp. txt 查時深度最多為3層
find   /home   -name tmp.txt   -mindepth   3   從第2層開始檢查
find     文件或空白目錄
find   /home   -size   +512k                使用大於512k的文件
find   /home   -size   -512k               發現與512k    對檢查硬連線數大於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    -empty             # 以系統尋找已為空白的文件或資料夾
find    /   -group   cat        # 尋找在系統中屬於groupcat的檔案
find      /   -mtime   -1       #以系統尋找系統最後24小時裡修改過的文件
find    /   -nouser           #以系統中屬於為廢使用者的檔案
find    普通文件

# find . -type f -exec ls -l {} ; 

-rw-r–r–    1 root      root           1 root root         12959 2003-02-25   ./conf/magic 

-rw-r–r–    .d/README 

查目前目錄下的所有普通文件,並在- e x e c選項中使用ls -l指令將它們列出


===================================== ============
在/ l o g s目錄中尋找更改時間在5日以前的檔案並刪除它們:
$ find logs -type f -mtime +5 -exec   -ok   rm {} ;

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

查詢當天修改過的檔案
[root@book class]# find   ./   -mtime   -1   -type f   -exec   ls -l   {} ;

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

查詢檔案並詢問是否要顯示
[root@book class] # find   ./   -mtime   -1   -type f   -ok   ls -l   {} ;  
[root@book class]# find   ./   -mtime   -1   -type f   -ok   ls -l   {} ;  


========================================== =======
查詢並交給awk去處理
[root@book class]# who   |   awk   '{print $1"t"$2}'
cnscn    pts/0

===

cnscn    pts/0


========= ==========================================
awk—grep—sed

[root@book class]# df   -k |   awk '{print $1}' |   grep   -v   'none' |   sed   s"//dev///g"

檔案系統
sda2

]# df   -k |   awk '{print $1}' |   grep   -v   'none'

檔案系統
/dev/sda2
/dev/sda1


/dev/sda2
/dev/sda1

5並在這些檔案中尋找「SYSCALL_VECTOR",最後列印所有包含"SYSCALL_VECTOR"的檔案名稱

A) find   /tmp   -name   "*.h"  /tmp/* .h | cut    -d':'   -f1| uniq > filename

C) find   /tmp   -name "*.h"   -exec grep "SYSCALL_VECTOR"in  {} rm -rf {} ;

    find / -name filename -ok rm -rf {} ;

3)例如要找出磁碟中大於3M的檔案:

find . -size +3000k -exec ls -ld {} ;

find . -size +3000k -exec ls -ld {} ;

4)將find出來的東西拷到另一個地方

find *.c -exec cp '{}' /tmp ';'

如果有特殊文件,可以用cpio,也可以用這樣的語法:

find dir -name filename -print | cpio -pdv newdir

6)查找2004-11-30 16:36:37時更改過的檔案
# A=`find ./ -name "*php"` |   ls -l –full-time $A 2>/dev/null | grep "2004-11-30 16:36:37"

Linux-all, Linux | No Comments »

find 實例

🀜/188, 2006

 *.h,並在這些檔案中尋找「SYSCALL_VECTOR",最後列印所有包含"SYSCALL_VECTOR"的檔案名,有以下幾種方法實作

find /usr/linux -name "*.h" | xargs -n50 grep SYSCALL_VECTOR

grep SYSCALL_VECTOR /usr/linux/*.h | cut -d':' -f1 | uniq > filename
find /usr/linux -name "*.h" -exec grep "SYSCALL_VECTOR" {} ;
  我用find / -name filename| rm -rf,不成功,請問為什麼不成功?
find / -name filename -exec rm -rf {} ;

find . -name filename |rm -rf試試看{} 表示你找出來的結果。

; 則相當於“憲法”,沒什麼說頭,就是這麼規定的,在 -exec 後面需要一個表示該命令終結的的符號。可以在 man find 中找到答案。
要讓rm辨識find的結果,如下:
find / -name filename |xargs rm -rf
之所以find . -name filename |rm -rf不通過,是因為rm指令不接受從標準輸入傳過來的指令
尋找包含特定字串的檔案
例如尋找目前目錄下含有"the string you want find…"字串的檔案:
$find . -type f -exec grep “the string you want find…” {} ; - print

  從根目錄開始查tmpfile,一旦查到馬上刪除
find / -name "tmpfile" -exec rm {} ;

  find 的perm問題
請問一下以下指令什麼意思?關鍵是那個數字前的-,其他都還知道

find -name ".*" -perm -007

我知道
find -name ".*" -perm 755
這個是用來查找權限位為755的隱藏檔案
噢,對了還有,我上邊的指令都省略了find的pathname參數find預設是找目前的工作目錄嗎?
如果我用 -ok 取代 -exec, 那麼還需要加上 {} ; 嗎?
這個已經清楚,仍然需要,因為-ok 只是-exec 的提示模式,它只是多了一個確認操作的步驟,剛才沒有讀懂那幾句E文的意思呵呵不好意思
-007是指查找所有用戶都可讀、寫、執行的文件,要小心~~~
解釋解釋?
find -name ".*" -perm -007 和 find -name ".*" -perm 777 有差別嗎?
-007是怎麼來得?
不過有一個問題
我用 find . -perm -100 會列出當前目錄 . , 這是為什麼呢?

下面引用explover在2002/10/01 06:15am 發表的內容:
-007是指查找所有用戶都可讀、寫、執行的文件,要小心~~~

-007是指查找所有用戶都可讀、寫、執行的文件,要小心~~~

-007是指查找其它使用者(不同群組,非屬主)可讀,寫,執行的檔案.並不一定要同組可讀寫,-是指最少權限為007.
下面引用由一顆小白菜在2002/10/01 10:16am 發表的內容:
OK了, 呵呵
不過有一個問題
我用find . -perm -100 會列出當前目錄. , 這是為什麼呢?
這種方法不會準確的找出目錄的. -100是指權限至少是屬主可運行.
在unix系統下,你可以擁有對目錄文件的執行權你才可以進入一個目錄.這便是目錄檔被列出的原因.
find . -perm -001 -print找到往往是目錄檔.
我的意思當然不是使用這種方法來找目錄,只不過不明白其中的-100 意義了
那以此類推,是不是-010是指權限至少是owner同組可執行的嗎?也就是說其實這裡的010和-是分開的,-表示一個至少的意思,而且010才是真正用來描述權限位的?
這樣子就明白了謝謝你噢

  將find出來的東西拷到另一個地方?

find *.c -exec cp '{}' /tmp ';'

如果有特殊文件,可以用cpio,也可以用這樣的語法:
find dir -name filename -print | cpio -pdv newdir

  中某個大小範圍內的檔案

例如要找出磁碟中大於3M的檔案:

find . -size +3000k -exec ls -ld {} ;

  如何用find找出某一天更改的檔案?

可以使用這行指令來實現:

A=`find ~ -print` | ls -l –full-time $A 2>/dev/null | grep "Jun 27" | grep 1998

  使用find 指令找出某個時間段的shell怎麼寫。例如11點到12點的。 thanks
建立一個腳本judgetime,內容如下:
ls -l $*|awk '{split($8,hour,":");if((hour[1]>23 || hour[1] 到要尋找的目錄下,執行
find ./ -name "*" -exec judgetime {} ;
注意時間格式為24小時制。
thank you ,如果我要精確到分鐘呢
touch -t 04241112 starttemp #精確到12分鐘
touch -t 04241220 endtemp #截止到12點20
find [dir] -newer temptempa 截止到12點20
find [dir] -newer temptempa ls -l {} ;
newer?
那昨天12:10文件如何?
每天執行的時候,用當天的日期和時間戳記替換一下不就行了嗎?
我不知道他是不是把所有的11:00~12:00的都找出來,是不是只執行一次還是每天都執行?
這種情況俺猜想是自己的東西放在哪裡忘了,只記得當時是深夜了。
有道理!
不愧是斑竹!
不光知道怎麼解決問題,還知道在什麼情況下出現這類問題,佩服佩服!
問題又出現了。創建這個文件的時候。本來應該是時間的一欄現在寫上了2002,而不是12:00.

等到12:00過了吧!


  刪除指定日期的檔案
find ./ -name 檔案名稱-exec rm -f {} ;
例:刪除目前30天內沒用過的檔案,用下列指令:
find / -atime +30 -exec rm -f {} ;
我自己試著寫了一小段SHELL,也用ll ,grep, rm 幾個指令,用起來還差強人意。
對過濾出來的檔案名稱清單中用了一個FOR語句,然後再執行rm 。現在我想把這段SHELL 擴展一下讓它每天定時運行將 n 天前的文件刪掉,有沒有人能給我一些提示,謝謝!
還有個問題,對於前面那位朋友提到的"find / -atime +30 -exec rm -f {} ;
"方法,我很早就試過幾次,不過好像都不太對,參數-atime n 是查找n天前被訪問過的文件,我不明白的是這裡的時間參考點是什麼,以及這個n天是怎麼計算的。
問題二、對於"ll |cut -f 1" 這個命令我是不是用錯了,我只想取出ll 中列出的文件名,但用cut -f 命令做不到,我只好換用ll | cut -c 59- 這個方式得到我想要的檔名,but it's a pool idear !我也試過用awk ,好像也不對,看看大家可不可以給我一些小小的提醒,TKS SO MUCH
問題三、如何改變I結點的日期格式我現在的系統顯示的格式是:
- rw-r—– 1 msahz01 users 2253 2002年2月2日poheader.i
我想把這換成
-rw-rw-rw- 1 house users 2193 Apr 19 2001 hkdisp.p

awk 應該可以
ll | awk '{print $9}'
刪除多少天之前的文件
find /yourpath -mtime +31 -exec rm {} ;

find /yourpath -mtime +366 -exec rm {} ;

find中, -ctime, -mtime及其-atime有何區別


請問-ctime 和-mtime 有什麼關係?
如果父目錄的ctime 改變, 那它下面的文件的ctime 就會自動都改了嗎?

-ctime 和-mtime ,-atime 這些資訊是存在哪兒呢?


我用-mtime -1 找到了新建或改的文件.

但怎樣才能找到一天內mv 來的文件呢( 它們的時間是原有的時間,早於一天) ?


用-newer選項啊。
你可以先touch一個你想要的時間的文件如下:
$ touch -t 08190800 test
$ ls -l test
-rw-r–r– 1 dba other 0 Aug 19 08:00 testw-r–r– 1 dba other 0 Aug 19 08:00 testw-r–r– 1 dba other 然後
find . -newer test -print
.
./.sh_history
$ ls -l .sh_history
-rw——- 1 dba other 154 Aug 20 17:39 .sh_history

時間的文件,然後用-newer ,! -newer選項即可成功。

1.ctime含inode資訊修改的時間.mtime只指文件內容建立或修改的時間.
2 不會.
3.這些資訊應該是存在文件系統的超級塊裡.

我查了書- ctime 是指inode 的改變(或稱文件的狀態改變).
請問inode 存了哪些資訊?
做了些小測試,-mtime 改, -ctime 一定也改.
改檔名, -ctime 也會改.
誰能回答i-node 存了哪些東西?

vi /usr/include/sys/inode.h

班主,我不能access /usr/include/sys/inode.h .
摘書如下:
Directories contain directory entries. Each entry contains a file or subdirectory name and an index node reference number (i-node number). increase speed and enhance use of disk space, the data in a file is stored at various locations in the computer's memory. The i-node contains the addresses used to locate all the scattered blocks of data associated with a file. records other information about the file including time of modification and access, access modes, number of links, file owner, and file type.
可我發現-atime 改了, -ctime 還沒改. why ?
( 我先擁有一個 atime 改了, why ?
( 我先擁有一個 一個ASCII 文件,再用-atime -1 有它用-ctime -1 居然沒有它.)

著豈不跟inode 信息改變, ctime 就改矛盾嗎?


我不同意你貼出來的那段文章,正如我提到的,atime,ctime,mtime是放到超級塊裡,在sco unix下是一種叫stat的結構.(stat_32),不同的系統文件系統可能不同.

sco 下inode的結構如下:


typedef struct inode
{
struct inode *i_forw; /* inode hash chain */
struct inode *i_back; /* '' */
struct inode *av_forw; /* freelist; /* '' */
int *i_fsptr; /* "typeless" pointer to fs dependent */
ino32_t i_number; /* i number, 1-to-1 with dev address */
ushort i_ftype; /* file type = IFDIR, IFREG, etc. */
short i_fstyp; /* File system type */
off_t i_size; /* size of file */
ushort i_uid; /* owner */

ushort i_flag;
ushort i_want; /* i_flag extension to avoid MP races */
ushort i_want; /* i_flag extension to avoid MP races */
ushort i_count; /* reference */
short i_nlink; /* directory 0. #define i_namtype i_rdev /* i_ftype==IFNAM subtype */
dev_t i_dev; /* device where inode resides */
struct mount *i_mton;/* ptr to mount table entry that */
/ mount y/
/ 是 */
on di. /
struct region *i_rp; /* ptr to shared region if any */
struct stdata *i_sp; /* ptr to associated stream */
struct iisem *isem; /* ptr to XENIX semaphores */sd ; /* ptr to XENIX shared data */
} i_un;
#define i_mnton i_un.i_mton /* i_ftype==IFDIR IMOUNT */
#define i_rptr i_un.i_rp /* i_ID /
#define i_sptr i_un.i_sp /* i_ftype==IFCHR || i_ftype==IFIFO */
#define i_sem i_un.isem /* i_ftype==IFNAM && i_namtype==IF/
#./

#i_i * i_ftype==IFNAM && i_namtype==IFSHD */


struct fstypsw *i_fstypp; /* ptr to file system switch FSPTR */
long *i_filocks; /* pointer to filock (structure) list /* number of pages currently cached */
unsigned long i_vcode; /* read-ahead block save (NFS) */
short i_wcnt; /* write open count or ITEXT count */
cynone i
i_flag changes */
ushort i_rdlocks; /* count of non-exclusive lockers */

} inode_t;


所以,訪問一個文件不能改變inode信息.
使用chown, chgrp, chmod
chown改變一個文件的屬主,用ctime可以找到,用mtime便找不到.

試試看.


多謝斑竹! 我是在Solaris 上面試的.我是對-ctime 不明白.
試的結果如下:
修改檔案,-mtime 改了, -ctime 也會改.
存取檔案,-atime 改了, -ctime 沒變.
chown, chgrp, chmod,mv, 都會使-ctime 改變,但不影響- atime 和-mtime.
touch 可以改-mtime and/or -atime,但touch -a 只改訪問時間時,-ctime也改了.
touch -m 改修改時間時,-ctime當然也改了.
好像還有別的很多東西可以令-ctime 改變, 搞不清楚.
有什麼方法可以顯示-mtime,atime,ctime 嗎?
可以用-ctime 來實現對目錄的增量文件進行備份或transfer 嗎?

多謝!


沒有什麼工具顯示,(可能是俺不知道)
把下面程式裡的st_mtime換成st_ctime,或st_atime便可以得到你要的了.
#include
int
main (int argc, char **argv)
{
struct stat buf;
char date[80];
char fname[80];
printf("Enter filename (with full path) to check mtime : ");
scanf("%s ",fname);
stat(fname, &buf);
printf ("mtime (in sec) of %s = %ldn", fname, buf.st_mtime);
strcpy(date, ctime((time_t *)&( buf.st_mtime)));
printf ("mtime (in date) of %s = %sn", fname, date);

}

至於檔案備份,有什麼不可以的麼?


mtime ls -ll最近修改文件內容的時間
atime ls -lu 最近訪問文件的時間

ctime ls -li 最近文件有所改變的狀態,如文件修改,屬性屬主改變,節點,鏈接變化等,應該是不拘泥只是時間前後的改變

俺看了ls的幫助,以為只是按ctime或atime排序,顯示的時間還是mtime.

仔細比較了一下,ayhan說的是對的.謝謝ayhan.🎜

多謝ahyan 提示! 我在Solaris 上試過如下:
mtime 用ls -l 看到
atime 用ls -lu 看到
ctime 用ls -lc 看到. (ls -li 只有inode number)
摘書如下:
-c Uses time of last modification of the i-node (file
created, mode changed, and so forth) for sorting (-t)
or printing (-l 或 -n).
-u Uses time of last access instead of last modification
for sorting (with the -t option) or printing (with the
-l option).
-i For each file, prints the i-node number in the first
column of the report.

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
3 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用