查找命令:find,locate,xargs,execphp
9.1.locatenode
功能:在数据库中查找,速度快,缺点:不精确,会忽略临时目录/tmp、/var/tmp正则表达式
经常使用选项:docker
-i:忽略大小写shell
-n:打印查找结果的前n行数据库
示例:express
[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(重点掌握)vim
功能:在目录结构中搜索文件,并执行指定的操做。此命令提供了至关多的查找条件,功能很强大。安全
特色:精确查找,磁盘搜索,IO读写,cpu开销相对较大bash
语法: 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 表示找出2五、2六、27号的文件
-3 表示找出2九、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.xargs
功能:从标准输入来执行命令
经常使用选项:
-a file 从指定文件读取数据做为标准输入
-0 处理包含空格的文件名,print0
-d delimiter 分隔符,默认是空格分隔显示
-i 标准输入的结果以{}代替
-I 标准输入的结果以指定的名字代替
-t 显示执行命令
-p 交互式提示是否执行命令
-n 最大命令行参数
--show-limits 查看系统命令行长度限制
示例:
上面find的例子已经不少了
介绍两个经常使用的:
[root@localhost scripts]# cat number.txt 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 [root@localhost scripts]# cat number.txt |xargs -n1 #列变行,-n1就是最大1列打印 1 2 3 4 5 6 7 8 [root@localhost scripts]# cat number.txt |xargs -n2 #这里就是最大两列打印 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 [root@localhost scripts]# cat number.txt |xargs -n3 #3列显示 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
9.4.练习
一、找出根目录下的全部块设备文件,而且将标准输出及标准错误重定向到/tmp/find.test文件中
[root@localhost mnt]# find / -type b > /tmp/find.test 2>&1
二、找出/root目录下小于2M的文件并长列出,同时将其追加到/tmp/find.test文件中
1M:等于1M
+1M:大于1M
-1M:小于1M
[root@localhost mnt]# find /root/ -size -2M -exec ls -l {} \; >>/tmp/find.test
三、在/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 \;