Linux 常用命令 -- find

Linux 常用命令 -- find

根据名字,顾名思义就是查找的意思,可以通过多个维度来查找你想要的文件,比如类型,

find - search for files in a directory hierarchy

常用用法

1. 通过名字查找文件

find pathname -name filename

2. 通过权限来查找 find pathname -perm
首先什么是权限,Linux操作系统中,文件和目录有三种权限,即r,w,x,同时usergroupother三个部分对文件或者目录又会拥有不同的r,w,x权限,这些权限可以通过ll filename可以看到,通常会用三位数X1X2X3(X1:user mode,X2:group mode,X3:other mode,0<= X <= 7),除此之外还有setuid,setgid权限。

知道了权限后,我们再来看find命令,根据权限来查找,find有三种模式:

1. find pathname -perm mode  
2. find pathname -perm -mode   
3. find pathname -perm +mode

find pathname -perm mode
这个表示严格比配,查找的文件权限必须和mode的大小一样

find pathname -perm -mode
这个表示mode的二进制表示中,1所在的位必须要严格匹配

find pathname -perm +mode
表示mode的二进制表示中,有一个1匹配就可以

3. 通过username或者groupname来查找

find pathname -user username
find pathname -group groupname
find pathname -nouser 
find pathname -nogroup

4. 通过时间来查找

find pathname -ctime -n / +n (-n表示最近n天,+n表示n天以前)
find pathname -mtime -n / +n
find pathname -atime -n / +n
find pathname -cmin -n / +n (这里表示分钟)
find pathname -mmin -n / +n
find pathname -amin -n / +n
find pathname -newer filename (find the file which was modified more recently than filename)
find pathname -cnewer filename
find pathname -anewer filename 

其中: c--create(创建) ,m--modify(修改),a--access(访问)

5. 通过文件类型来查找

find pathname -type typename

typename有:
1. b -- block
2. d -- directory
3. c -- character
4. p -- named pipe(FIFO)
5. f -- regular file
6. s -- socket
7. l -- symbolic link

6. 通过深度来查找

find pathname -depth 
find pathname -mindepth levels
find pathname -maxdepth levels

7. 通过大小来查找

find pathname -size n / -n / +n [cwbkMG]
  1. c : for bytes
  2. w : for two-bytes word
  3. b : 512-bytes block
  4. k : kilobytes
  5. M : Megabytes
  6. G : Gigabytes

其中: n:正好为n个单位大小的文件,-n:表示小于,+n:表示大于

8. exec功能
用于对搜索出来的结果实行shell命令

find ... -exec COMMAND {} ;

注意格式
例子:查找本目录下文件名中包含test的文件,并列出详细的文件信息

$find  -name '*test*'  -exec ls -l {} ;
-rw-rw-r-- 1 admin admin 70 May 23 17:00 ./test_IFS.sh
-rw-rw-r-- 1 admin admin 147 May 23 17:19 ./test_read.sh
-rw-rw-r-- 1 admin admin 809 Jun 16 18:15 ./test.sh

9. 查找时怎么避开一些文件夹

find ... -path pathname -prune -o -print

10. 例子

Demo 1

find /tmp -name core -type f -print | xargs /bin/rm -f

在/tmp目录下查找名为core的文件,并且将其直接删除,但是这里如果文件名中包含空格,换行符,引号,执行会报错

$find -name "cor*" -type f -print
./cor e
./cor'e
./cor
e

$find -name "cor*" -type f -print | xargs /bin/rm 
/bin/rm: cannot remove `./cor': No such file or directory
/bin/rm: cannot remove `e': No such file or directory
xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option

这是因为空格,换行符,引号,对xargs默认来说是特殊意义。
这时可以通过find -print0xargs -0来解决, 让 find 在打印出一个文件名之后接着输出一个 NULL 字符 ('') 而不是换行符, 然后再告诉 xargs 也用 NULL 字符来作为记录的分隔符.

$find -name 'cor*' -type f -print0 | xargs -0 /bin/rm

Demo 2

find . -type f -exec file ’{}’ ;

对当前目录下的所有文件运行file命令,

Demo 3

find $HOME -mtime 0

Search for files in your home directory which have been modified in the last twenty-four hours. This command works this way because the time since each file was last modified is divided by 24 hours and any remainder is discarded. That means that to match -mtime 0, a file will have to have a modification in the past which is less than 24 hours ago.

Demo 4

find . -perm /220
find . -perm /u+w,g+w
find . -perm /u=w,g=w

上面的三个命令是同样的功能,即在当前目录下寻找user,group可以写的文件,只要其中一个为可写即可以

Demo 5

find . -perm -444 -perm /222 ! -perm /111
find . -perm -a+r -perm /a+w ! -perm /a+x

These two commands both search for files that are readable for everybody (-perm -444 or -perm -a+r), have at least on write bit set (-perm /222 or -perm /a+w) but are not executable for anybody (! -perm /111 and ! -perm /a+x respectively)

还是直接英文吧。。。。。英文看着更舒服一些

原文地址:https://www.cnblogs.com/zk47/p/3905220.html