linux find 命令!

tonykorn97發表於2008-07-11

一,basic find example

1,find -name snow.png
search for files named snow.png in the current directory

2,find -iname snow.png
case-insensitive search for files named snow.png,Snow.png,SNOW.PNG, in the current directory

[@more@]

3,find / -name *.txt
search for files anywhere on the system that end in .txt

4,find /etc -name *pass*
search ofr files in /etc/ that contaion pass in their name

5,find /home -user joe -group joe
search ofr files owned by the user joe and the group joe in /home


二,find and logical operators

Criteria are ANDed together by default.
Can be OR'd or negated with -o or -not
parentheses can be used to determine logic order,but must be escaped in bash.

find -user joe -not -group joe
find -user joe -o -user jane
find -not ( -user joe -o -user jane )


三,find and permissions

can match ownership by name or id
find / -user joe -o -uid 500

can match octal or symbolic permissions

1,find -perm 755
matches if mode is exactly 755

2, find -perm +222
matches if anyone can write

3, find -perm -222
matches if everyone can write

4, find -perm -002
matches if other can write


四,find and numeric criteria

many find criteria take numeric values

1, find -size 10M
files with a size of exactly 10 megabytes

2, find -size +10M
files with a size over 10 megabytes

3, find -size -10M
files with a size less than 10 megabytes

other modifiers are available such as k for KB,G for GB ,etc


五,find and access times
find can match by inode timestamps
-atime when file was last read
-mtime when file data last changed
-ctime when file data or metadata last change

value given is in days

1, find /tmp -ctime +10
files changed more than 10 days ago

can use a value of minutes
-amin
-mmin
-cmin

find /etc -amin -60


六,executing commands with find
commands can be executed on fond files
command must be preceded with -exec or -ok
-ok prompts before acting on each file

command must end with space;
can use {} as a filename placeholder

find -size +100M -ok mv {} /tmp/largefiles/ ;


find execution examples

1,find -name "*.conf" -exec cp {} {}.orig ;
back up configuration files form the current directory, adding a .orig extension

2, find /tmp -ctime +3 -user joe -ok rm {} ;
prompt ot remove joe's tmp files that are over 3 days old

3, find ~ -perm -002 -exec chmod o-w {} ;
fix other-writabled files in your home directory

4, find /home -type d -ls
do an ls-l style listing of all directories in /home

5, find /data -type f -perm 644 -name *.sh -ok chmod 755 {} ;
find files that end in .sh found in the /data/ directory with a current permission of 644, and ask to make them executable

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/312079/viewspace-1007120/,如需轉載,請註明出處,否則將追究法律責任。

相關文章