码迷,mamicode.com
首页 > 其他好文 > 详细

查找命令(九)

时间:2017-05-31 19:01:15      阅读:277      评论:0      收藏:0      [点我收藏+]

标签:find   locate   

如果有不正确的地方,欢迎广大博友指正!


查找命令:find,locate,xargs,exec


9.1.locate

功能:在数据库中查找,速度快,缺点:不精确,会忽略临时目录/tmp、/var/tmp

常用选项:

-i:忽略大小写

-n:打印查找结果的前n行

示例:

[root@localhost ~]# locate -i /etc/passwd   #报错,没有locate命令
-bash: locate: command not found
[root@localhost ~]# yum install -y mlocate   #安装locate命令
[root@localhost ~]# updatedb  #刷新数据库
[root@localhost ~]# locate -i /etc/passwd  #不区分大小写
/etc/passwd
/etc/passwd-
 [root@localhost ~]# locate -n 1 /etc/passwd
/etc/passwd

9.2.find(重点掌握)

功能:在目录结构中搜索文件,并执行指定的操作。此命令提供了相当多的查找条件,功能很强大。

特点:精确查找,磁盘搜索,IO读写,cpu开销相对较大

语法: find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]

常用选项:

-name: 按照文件名查找,支持*号和[]号。

-iname:忽略大小写按照文件名查找

-perm:按照文件权限来查找,支持完全指定和-号、+号部分符合。

-prune:使用这一选项可以使find命令不在当前指定的目录中查找,如果同时使用-depth选项,那么-prune将被find命令忽略。

-user:按照文件属主来查找

-group:按照文件所属的组来查找

-mtime :-n +n按照文件的更改时间来查找

-amin(atime): -n +n按照文件的访问时间来查找

-cmin (ctime):-n +n按照文件状态的更改时间来查找

stat +文件名 可以查看amc文件时间

- n代表n天以内,+n代表那天以前,n代表当天

如下面的例子:

25 26 27 28 29 30 31 (当天为28)

n=3 表示找出28号的文件

+3 表示找出25、26、27号的文件

-3 表示找出29、30、31号的文件

-nogroup:查找无有效所属组的文件,即该文件所属的组在/etc/groups中不存在。

-nouser:查找无有效属主的文件,即该文件的属主在/etc/passwd中不存在。

-newer file1 ! -newer file2查找更改时间比文件file1新但比文件file2旧的文件。

-type:按照文件类型查找

  b - 块设备文件。

  d - 目录。

  c - 字符设备文件。

  p - 管道文件。

  l - 符号链接文件。

 f - 普通文件。

 s socket文件

-size n:[c] 查找文件长度为n块的文件,带有c时表示文件长度以字节计。

-depth:在查找文件时,首先查找当前目录中的文件,然后再在其子目录中查找。

-fstype:查找位于某一类型文件系统中的文件,这些文件系统类型通常可以在配置文件/etc/fstab中找到,该配置文件中包含了本系统中有关文件系统的信息。

-mount:在查找文件时不跨越文件系统mount点。

-follow:如果find命令遇到符号链接文件,就跟踪至链接所指向的文件。

-regex pattern:对搜索结果的 整个路径 按正规表达式进行过滤,必须对全路径考虑,例如结果是./test,那正规表达式应该用"./t.*",而不能用"t.*"

-cpio:对匹配的文件使用cpio命令。

find命令还支持使用逻辑运算符:

-a 类似&& ,连接两个不同的条件(两个条件必须同时满足)

-o 类似|| ,连接两个不同条件 (两个条件满足其一即可)

!,也是非、否

-exec:直接执行后面所跟的命令,不提示

-ok:交互式执行后面所跟的命令

-not:对条件取反

示例:

#列出当前目录及子目录的所有文件
[root@localhost home]# find
.
./file5
./old03
./old03/.gnome2
./old03/.bash_logout
./old03/.bash_profile
说明:什么参数都不加就是列出当前目录及子目录的所有文件,也可以这样写find .  ,find . -print

#查找特殊目录文件或路径
[root@localhost scripts]# find ./test -name "*.txt"
./test/test.txt
./test/town.txt
./test/number.txt
./test/name.txt
[root@localhost scripts]# find ./test -iname "*.txt"   #i忽略大小写
./test/test.txt
./test/town.txt
./test/number.txt
./test/AV.txt
./test/name.txt
#限制目录查找的深度
需要用到选项maxdepth
[root@localhost ~]# find . -maxdepth 2 -name "*.vim"  
./.vim_old/.vim
./.vim
[root@localhost ~]# find . -maxdepth 3 -name "*.vim"
说明:maxdepth后接的数字代表层级,在当前目录下,vim_old为第一层,.vim到第二层就结束了。跟这个选项还有一个相对的mindepth,这个是从子目录往上找,maxdepth是从父目录往下找。

#反向查找
[root@localhost ~]# find . -not -name "*.vim"
[root@localhost ~]# find .  ! -name "*.vim"
[root@localhost ~]#find . \( ! -name ‘*log*‘ -a ! -name ‘*cfg*‘ \) #找到不包含log和cfg的文件
说明:! -not都是代表不包含.vim后缀的文件
#以文件类型及执行其他命令查找
[root@localhost ~]# find . -type f -mtime -1 -print  #在当前文件夹下搜索最近1小时被修改的文件并打印
[root@localhost ~]# find . -type d -name ".svn"|xargs rm -rf  #删除.svn目录
[root@localhost ~]# find . -type d -name ".svn" -exec rm -rf {} \;  #删除.svn目录
[root@localhost ~]#find . -type f -name "*.php" -delete   #搜索并删除文件
[root@localhost ~]#find ./ -type f -name "*.sh"
f[root@localhost ~]#find /data/bbb -type d \( -name ‘city‘ -o -name ‘equipment‘ -o -name ‘soldier‘  \) #多目录查找
[root@localhost ~]#find . -type d -name "*p*"   #递归找出带有p字符的目录
[root@localhost ~]#find . -type f -exec ls -l {} \; >yourfile #重定向操作
[root@localhost ~]#find . -type d| sort  #列出所有目录并排序
[root@localhost ~]# find /etc -type f -empty  #查找空文件
[root@localhost ~]#find /etc -type d -empty  #查找空目录

#以文件修改时间查找
[root@localhost ~]# find . -type f -atime  -7 -print  # #在当前文件夹下搜索最近7天内被访问过的文件并打印
   1 >date
   2. Mon Aug 23 19:25:44 CST 2010
   3. >find ./ -mtime +22 -a -mtime -54   #列出mtime为7月的文件,+22表示22天以前就在7月份了,但是前面的日期没有限定,22天以前包含1-7月份啊,再加个54天以内的,那就是54-22,刚好是7月份的文件。
[root@docker-node5 ~]#find logs -type f -mtime +5 -exec rm {} \;  #在/ l o g s目录中查找更改时间在5日以前的文件并删除
[root@docker-node5 ~]#find ./ -mtime +7 -ok rm -f {} \;    #3保留7天以内的文件(7天以前删掉)
< rm ... ./file8 > ? y
< rm ... ./file7 > ? y
< rm ... ./file6 > ? y
[root@localhost mnt]# find /var/log/ -mtime +3 -type f -print  #找出3天以前被修改过的文档
[root@localhost mnt]# find /var/log/ -mtime -3 -type f -print  #找出3天内被修改过的文档
[root@localhost mnt]# find /var/log/ -mtime 3 -type f -print  #找出第三天被修改过的文档
[root@localhost mnt]# find /var/log/ -mtime +2 -mtime -4 -type f -print #找出第三天被修改过的文档
#多条件查找
[root@localhost mnt]# find /etc -name "*.sh" -o -name "*.txt" #在etc目录下查找sh或txt结尾的文件,满足其一即可
[root@docker-node5 ~]#find /etc -name "passwd*" -exec grep "sam" {} \; #查找passwd文件,并查看有没有sam这个用户。
[root@localhost mnt]# pwd
/mnt
[root@localhost mnt]# find /mnt/ -name abc
/mnt/test/abc
/mnt/abc
[root@localhost mnt]# find /mnt/ -path /mnt/test -prune -o -name abc -print  #查找abc,除过test下的abc
/mnt/abc
#以文件权限查找
[root@localhost ~]#find . -type f -perm 644 -print  #列出具有特定权限的文件
[root@localhost ~]#find . -group root -exec ls -l {} \;  #搜索属于root组的文件
[root@localhost ~]# find / -perm 2644  #查找有644及s属性
[root@localhost ~]# find / -maxdepth 2 -perm /u=s 2>/dev/null
/bin/umount
/bin/su
/bin/mount
/bin/ping
/bin/ping6
/sbin/unix_chkpwd
/sbin/pam_timestamp_check
[root@localhost ~]# ll /bin/umount 
-rwsr-xr-x. 1 root root 53472 Oct 15  2014 /bin/umount
[root@localhost ~]# find / -maxdepth 1 -perm /u=r
/
/lib64
/boot
/bin
/home
/usr
[root@localhost ~]# find . -user root  
#以文件大小查找
[root@localhost ~]# find . -type f -size +2k  #搜索文件大于2k的文件
#支持正则表达式查找
-regextype 指定所使用的正则表达式类型,可选的有emacs(默认),posix-awk,posix-basic,posix-egrep,posix-extended,喜欢grep -E,就用posix-egrep
用find查找目录下以1-3位数字命名的文件
$find ./ -regextype posix-egrep -regex ‘.*/[0-9]{1,3}‘
[root@localhost mnt]# find /etc -regex ".*\.\(txt\|sh\)"  #在etc目录下查找txt及sh结尾的文件
/etc/kde/env/imsettings-kde.sh
/etc/pki/nssdb/pkcs11.txt
/etc/X11/xinit/xinitrc.d/50-xinput.sh
/etc/X11/xinit/xinitrc.d/localuser.sh
/etc/X11/xinit/xinitrc.d/00-start-message-bus.sh
/etc/bash_completion.d/gdbus-bash-completion.sh
#查找隐藏文件
[root@localhost ~]# find ~ -type f -name ".*"
说明:查找家目录下的隐藏文件
-exec会把find查找到的结果一次性全部交给后面的命令来进行处理,有时候系统对能够传递给exec的命令长度有限制,这样在find命令运行几分钟之后,就会出现 溢出错误。错误信息通常是“参数列太长”或“参数列溢出”。这就是xargs命令的用处所在 ,特别是与find命令一起使用。有些时候匹配到的文件发起的一个进程,并非将匹配到的文件全部作为参数一次执行,会导致进程过多,系统性能下降的问题,因此效率不高。

xargs会把find查找到的结果逐一交给后面的命令进行来处理,而xargs命令每次只获取一部分文件而不是全部,不像-exec选项那样。这样它可以先处理最先获取的一部分文件,然后是下一批,继续下去,直到结束。
-ok和-exec的作用相同,只不过以一种更为安全的模式来执行该参数所给出的shell命令,在执行每一个命令之前,都会给出提示,让用户来确定是否执行

9.3.练习

1、找出根目录下的所有块设备文件,并且将标准输出及标准错误重定向到/tmp/find.test文件中

[root@localhost mnt]# find / -type b > /tmp/find.test 2>&1

2、找出/root目录下小于2M的文件并长列出,同时将其追加到/tmp/find.test文件中

1M:等于1M

+1M:大于1M

-1M:小于1M


[root@localhost mnt]# find /root/ -size -2M -exec ls -l {} \; >>/tmp/find.test

3、在/home/test目录中创建10个文件,并且修改file1~file5的时间为系统时间的5天前,file6的时间为5月28,file7的为5月29,file8的为5月30

[root@localhost home]# pwd
/home
[root@localhost home]# touch file{1..10}
[root@localhost home]# ls
file1   file2  file4  file6  file8  old01  old03  old05  old07  old09  yyl
file10  file3  file5  file7  file9  old02  old04  old06  old08  old10  yyl01
[root@localhost home]# touch -d $(date +%Y%m%d) --date="6 days ago" file{1..5}
[root@localhost home]# ll
total 48
-rw-r--r-- 1 root  root     0 May 25 15:37 file1
-rw-r--r-- 1 root  root     0 May 31 15:35 file10
-rw-r--r-- 1 root  root     0 May 25 15:37 file2
-rw-r--r-- 1 root  root     0 May 25 15:37 file3
-rw-r--r-- 1 root  root     0 May 25 15:37 file4
-rw-r--r-- 1 root  root     0 May 25 15:37 file5
-rw-r--r-- 1 root  root     0 May 31 15:35 file6
-rw-r--r-- 1 root  root     0 May 31 15:35 file7
-rw-r--r-- 1 root  root     0 May 31 15:35 file8
-rw-r--r-- 1 root  root     0 May 31 15:35 file9
[root@localhost home]# touch -d 20170528 file6
[root@localhost home]# touch -d 20170529 file7
[root@localhost home]# touch -d 20170530 file8
[root@localhost home]# ll
total 48
-rw-r--r-- 1 root  root     0 May 25 15:37 file1
-rw-r--r-- 1 root  root     0 May 31 15:35 file10
-rw-r--r-- 1 root  root     0 May 25 15:37 file2
-rw-r--r-- 1 root  root     0 May 25 15:37 file3
-rw-r--r-- 1 root  root     0 May 25 15:37 file4
-rw-r--r-- 1 root  root     0 May 25 15:37 file5
-rw-r--r-- 1 root  root     0 May 28 00:00 file6
-rw-r--r-- 1 root  root     0 May 29 00:00 file7
-rw-r--r-- 1 root  root     0 May 30 00:00 file8
-rw-r--r-- 1 root  root     0 May 31 15:35 file9

要求:

1)找出5天以前的文件并将其拷贝到/backup目录

[root@localhost home]# find ./ -mtime +5 -ok cp {} /backup/ \;

2)找出5天以内的文件并将其拷贝到192.168.5.1上的/home/test目录里,并且重命名成自己的名字。redhat用户密码为123

[root@localhost home]# find /home/redhat -mtime -5 -exec scp {} redhat@192.168.5.1:/home/test/zhangsan \;

本文出自 “烂笔头” 博客,请务必保留此出处http://lanbitou.blog.51cto.com/9921494/1931013

查找命令(九)

标签:find   locate   

原文地址:http://lanbitou.blog.51cto.com/9921494/1931013

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!