歡迎您光臨本站 註冊首頁

linux find命令的使用方法

←手機掃碼閱讀     火星人 @ 2014-03-09 , reply:0

Linux中find常見用法示例

·find path -option [ -print ] [ -exec -ok command ] {} ;

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

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

-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這個用戶的文件

查當前目錄下的所有普通文件

# find . -type f -exec ls -l {} ;
-rw-r–r– 1 root root 34928 2003-02-25 ./conf/httpd.conf
-rw-r–r– 1 root root 12959 2003-02-25 ./conf/magic
-rw-r–r– 1 root root 180 2003-02-25 ./conf.d/README
查當前目錄下的所有普通文件,並在- e x e c選項中使用ls -l命令將它們列出

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

=================================================
查詢文件並詢問是否要顯示
[root@book class]# find ./ -mtime -1 -type f -ok ls -l {} ;
< ls … ./classDB.inc.php > ? y
-rw-r–r– 1 cnscn cnscn 13709 1月 12 12:22 ./classDB.inc.php
[root@book class]# find ./ -mtime -1 -type f -ok ls -l {} ;
< ls … ./classDB.inc.php > ? n
[root@book class]#

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

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

[root@book class]# df -k | awk ‘{print $1}’ | grep -v ’none’ | sed s"//dev///g"
文件系統
sda2
sda1
[root@book class]# df -k | awk ‘{print $1}’ | grep -v ’none’
文件系統
/dev/sda2
/dev/sda1

1)在/tmp中查找所有的*.h,並在這些文件中查找“SYSCALL_VECTOR",最后列印出所有包含"SYSCALL_VECTOR"的文件名 A) find /tmp -name "*.h" | xargs -n50 grep SYSCALL_VECTOR
B) grep SYSCALL_VECTOR /tmp/*.h | cut -d’:' -f1| uniq > filename
C) find /tmp -name "*.h" -exec grep "SYSCALL_VECTOR" {} ; -print

2)find / -name filename -exec rm -rf {} ;

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

3)比如要查找磁碟中大於3M的文件:
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"

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

本文出自 「楊冬的博客」 博客,請務必保留此出處http://yangdong.blog.51cto.com/2959198/546524


[火星人 ] linux find命令的使用方法已經有460次圍觀

http://coctec.com/docs/linux/show-post-48946.html