Linux文件及目录查找
一which——显示命令的完整路径
[root@centos71 ~]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
[root@centos71 ~]# which cp
alias cp='cp -i'
/usr/bin/cp
[root@centos71 ~]# which cat
/usr/bin/cat
[root@centos71 ~]# which cd
/usr/bin/cd
[root@centos71 ~]# which ping
/usr/bin/ping
[root@centos71 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@centos71 ~]#
二whereis
[root@centos71 ~]# whereis cp
cp: /usr/bin/cp /usr/share/man/man1/cp.1.gz
[root@centos71 ~]# mv /usr/share/man/man1/cp.1.gz /tmp/
[root@centos71 ~]# ls /tmp/cp.1.gz
/tmp/cp.1.gz
[root@centos71 ~]# man cp
No manual entry for cp
[root@centos71 ~]# whereis cd
cd: /usr/bin/cd /usr/share/man/man1/cd.1.gz
[root@centos71 ~]# whereis cat
cat: /usr/bin/cat /usr/share/man/man1/cat.1.gz
[root@centos71 ~]# whereis pwd
pwd: /usr/bin/pwd /usr/include/pwd.h /usr/share/man/man1/pwd.1.gz
[root@centos71 ~]# whereis ping
ping: /usr/bin/ping /usr/share/man/man8/ping.8.gz
三locate
缺点:无法区分类型,要及时更新数据库
updatedb是更新数据库文件
巧记:
数据库文件相当于大学宿舍名单册,里面记录了大学生的住宿情况,名字和宿舍对应起来了。
宿舍的人搬走了,但是没有在名单上更新就无法在宿舍找到这个人了。
[root@centos71 ~]# yum provides locate
Loaded plugins: fastestmirror
Repository base is listed more than once in the configuration
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
mlocate-0.26-8.el7.x86_64 : An utility for finding files by name
Repo : base
Matched from:
Filename : /usr/bin/locate
mlocate-0.26-8.el7.x86_64 : An utility for finding files by name
Repo : @base
Matched from:
Filename : /usr/bin/locate
[root@centos71 ~]# locate ping.sh
locate: can not stat () `/var/lib/mlocate/mlocate.db': No such file or directory
[root@centos71 ~]# update
-bash: update: command not found
[root@centos71 ~]# updatedb
[root@centos71 ~]# locate ping.sh
/shell_scriprs/.ping.sh.swp
/shell_scriprs/ping.sh
[root@centos71 ~]# touch hahah.sh
[root@centos71 ~]# locate hahah.sh
[root@centos71 ~]# updatedb
[root@centos71 ~]# locate hahah.sh
/root/hahah.sh
[root@centos71 ~]# yum provides find
Loaded plugins: fastestmirror
Repository base is listed more than once in the configuration
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
1:findutils-4.5.11-6.el7.x86_64 : The GNU versions of find utilities (find and xargs)
Repo : base
Matched from:
Filename : /usr/bin/find
1:findutils-4.5.11-6.el7.x86_64 : The GNU versions of find utilities (find and xargs)
Repo : @anaconda
Matched from:
Filename : /usr/bin/find
四find
find的查找条件
语法:find 查找区域目录信息 搜索的条件
(一)根据目录层级查找数据
注意此参数一定要放在命令的最前面
查找目录里面的内容,没有指定文件类型那么就是文件和目录
[root@centos71 ~]# find /etc/sysconfig/network-scripts/ -maxdepth 1 /etc/sysconfig/network-scripts/ /etc/sysconfig/network-scripts/ifcfg-lo /etc/sysconfig/network-scripts/ifdown /etc/sysconfig/network-scripts/ifdown-bnep /etc/sysconfig/network-scripts/ifdown-eth /etc/sysconfig/network-scripts/ifdown-ippp /etc/sysconfig/network-scripts/ifdown-ipv6 /etc/sysconfig/network-scripts/ifdown-isdn /etc/sysconfig/network-scripts/ifdown-post /etc/sysconfig/network-scripts/ifdown-ppp /etc/sysconfig/network-scripts/ifdown-routes /etc/sysconfig/network-scripts/ifdown-sit /etc/sysconfig/network-scripts/ifdown-tunnel /etc/sysconfig/network-scripts/ifup /etc/sysconfig/network-scripts/ifup-aliases /etc/sysconfig/network-scripts/ifup-bnep /etc/sysconfig/network-scripts/ifup-eth /etc/sysconfig/network-scripts/ifup-ippp /etc/sysconfig/network-scripts/ifup-ipv6 /etc/sysconfig/network-scripts/ifup-isdn /etc/sysconfig/network-scripts/ifup-plip /etc/sysconfig/network-scripts/ifup-plusb /etc/sysconfig/network-scripts/ifup-post /etc/sysconfig/network-scripts/ifup-ppp /etc/sysconfig/network-scripts/ifup-routes /etc/sysconfig/network-scripts/ifup-sit /etc/sysconfig/network-scripts/ifup-tunnel /etc/sysconfig/network-scripts/ifup-wireless /etc/sysconfig/network-scripts/init.ipv6-global /etc/sysconfig/network-scripts/network-functions /etc/sysconfig/network-scripts/network-functions-ipv6 /etc/sysconfig/network-scripts/ifdown-Team /etc/sysconfig/network-scripts/ifdown-TeamPort /etc/sysconfig/network-scripts/ifup-Team /etc/sysconfig/network-scripts/ifup-TeamPort /etc/sysconfig/network-scripts/ifcfg-eth0 [root@centos71 ~]# find /etc/sysconfig/network-scripts/ -maxdepth 1 | wc 36 36 1529
查看/var/目录下的所有文件和目录
[root@centos71 ~]# ls -al /var/
total 16
drwxr-xr-x. 19 root root 267 Nov 26 17:39 .
dr-xr-xr-x. 24 root root 4096 Dec 16 01:25 ..
drwxr-xr-x. 2 root root 6 Apr 11 2018 adm
drwxr-xr-x. 6 root root 59 Nov 26 17:33 cache
drwxr-xr-x. 2 root root 6 Nov 5 2018 crash
drwxr-xr-x. 3 root root 34 Nov 26 17:33 db
drwxr-xr-x. 3 root root 18 Nov 26 17:33 empty
drwxr-xr-x. 2 root root 6 Apr 11 2018 games
drwxr-xr-x. 2 root root 6 Apr 11 2018 gopher
drwxr-xr-x. 3 root root 18 Nov 26 17:32 kerberos
drwxr-xr-x. 30 root root 4096 Dec 12 15:55 lib
drwxr-xr-x. 2 root root 6 Apr 11 2018 local
lrwxrwxrwx. 1 root root 11 Nov 26 17:31 lock -> ../run/lock
drwxr-xr-x. 8 root root 4096 Dec 16 05:14 log
lrwxrwxrwx. 1 root root 10 Nov 26 17:31 mail -> spool/mail
drwxr-xr-x. 2 root root 6 Apr 11 2018 nis
drwxr-xr-x. 2 root root 6 Apr 11 2018 opt
drwxr-xr-x. 2 root root 6 Apr 11 2018 preserve
lrwxrwxrwx. 1 root root 6 Nov 26 17:31 run -> ../run
drwxr-xr-x. 10 root root 118 Nov 26 17:33 spool
drwxrwxrwt. 3 root root 18 Dec 16 09:59 tmp
-rw-r--r--. 1 root root 163 Nov 26 17:31 .updated
drwxr-xr-x. 2 root root 6 Apr 11 2018 yp
[root@centos71 ~]# ls -al /var/ | wc
24 215 1156
查看/var/目录下的所有目录,注意有3个是软链接
[root@centos71 ~]# ls -d /var/* | wc
20 20 205
[root@centos71 ~]# ls -ld /var/* | wc
20 186 1104
[root@centos71 ~]# ls -ld /var/*
drwxr-xr-x. 2 root root 6 Apr 11 2018 /var/adm
drwxr-xr-x. 6 root root 59 Nov 26 17:33 /var/cache
drwxr-xr-x. 2 root root 6 Nov 5 2018 /var/crash
drwxr-xr-x. 3 root root 34 Nov 26 17:33 /var/db
drwxr-xr-x. 3 root root 18 Nov 26 17:33 /var/empty
drwxr-xr-x. 2 root root 6 Apr 11 2018 /var/games
drwxr-xr-x. 2 root root 6 Apr 11 2018 /var/gopher
drwxr-xr-x. 3 root root 18 Nov 26 17:32 /var/kerberos
drwxr-xr-x. 30 root root 4096 Dec 12 15:55 /var/lib
drwxr-xr-x. 2 root root 6 Apr 11 2018 /var/local
lrwxrwxrwx. 1 root root 11 Nov 26 17:31 /var/lock -> ../run/lock
drwxr-xr-x. 8 root root 4096 Dec 16 05:14 /var/log
lrwxrwxrwx. 1 root root 10 Nov 26 17:31 /var/mail -> spool/mail
drwxr-xr-x. 2 root root 6 Apr 11 2018 /var/nis
drwxr-xr-x. 2 root root 6 Apr 11 2018 /var/opt
drwxr-xr-x. 2 root root 6 Apr 11 2018 /var/preserve
lrwxrwxrwx. 1 root root 6 Nov 26 17:31 /var/run -> ../run
drwxr-xr-x. 10 root root 118 Nov 26 17:33 /var/spool
drwxrwxrwt. 3 root root 18 Dec 16 09:59 /var/tmp
drwxr-xr-x. 2 root root 6 Apr 11 2018 /var/yp
查找一级目录,不显示软链接对应的目录,但是把父目录也显示了
[root@centos71 ~]# find /var/ -maxdepth 1 -type d
/var/
/var/tmp
/var/lib
/var/log
/var/adm
/var/cache
/var/db
/var/empty
/var/games
/var/gopher
/var/local
/var/nis
/var/opt
/var/preserve
/var/spool
/var/yp
/var/kerberos
/var/crash
[root@centos71 ~]# find /var/ -maxdepth 1 -type d | wc
18 18 182
查找指定的一层
只在第2层查找,那么最大和最小都是2层
[root@centos72 ~]# find /etc -mindepth 2 -maxdepth 2 -name "network"
/etc/sysconfig/network
最小层比较难理解的
-mindepth 查找数据最小深度
搜索最大3层,最小2层和搜索最小2层,最大3层显示结果是一样的,其位置不讲究
[root@centos72 ~]# find /etc -mindepth 2 -maxdepth 3 -name "network"
/etc/rc.d/init.d/network
/etc/sysconfig/network
[root@centos72 ~]# find /etc -maxdepth 3 -mindepth 2 -name "network"
/etc/rc.d/init.d/network
/etc/sysconfig/network
(二)根据文件名称查找数据
可以结合通配符*进行匹配
[root@centos71 ~]# find /shell_scriprs -name "ping"
[root@centos71 ~]# find /shell_scriprs -name "ping*"
/shell_scriprs/ping.sh
[root@centos71 ~]# find /shell_scriprs -name "usr*"
/shell_scriprs/usr_ramdel.sh
/shell_scriprs/usr_rampswd.sh
/shell_scriprs/usr_del.sh
/shell_scriprs/usr_pswd.sh
/shell_scriprs/usr_ram_crt.sh
/shell_scriprs/usr_ram_crt.txt
/shell_scriprs/usr.sh
/shell_scriprs/usrname.txt
(三)根据文件类型查找数据
-type TYPE:
f: 普通文件
d: 目录文件
l: 符号链接文件
s:套接字文件
b: 块设备文件
c: 字符设备文件
p: 管道文件
[root@centos71 ~]# find /shell_scriprs/ -type f
/shell_scriprs/usr_ramdel.sh
/shell_scriprs/usr_rampswd.sh
/shell_scriprs/usr_del.sh
/shell_scriprs/usr_pswd.sh
/shell_scriprs/hacker.sh
/shell_scriprs/usr_ram_crt.sh
/shell_scriprs/usr_ram_crt.txt
/shell_scriprs/usr.sh
/shell_scriprs/usrname.txt
/shell_scriprs/create_usr.sh
/shell_scriprs/ping.sh
(四)根据文件大小查找数据
寻找小于10k文件,注意是小写字母k
[root@centos71 ~]# find /shell_scriprs -size -10k
/shell_scriprs
/shell_scriprs/usr_ramdel.sh
/shell_scriprs/usr_rampswd.sh
/shell_scriprs/usr_del.sh
/shell_scriprs/usr_pswd.sh
/shell_scriprs/hacker.sh
/shell_scriprs/usr_ram_crt.sh
/shell_scriprs/usr_ram_crt.txt
/shell_scriprs/usr.sh
/shell_scriprs/usrname.txt
/shell_scriprs/create_usr.sh
/shell_scriprs/ping.sh
寻找大小正好是20K的文件
[root@centos71 ~]# find /tmp/ -size 20k
/tmp/yum_save_tx.2019-12-11.12-42.LMzjWj.yumtx
[root@centos71 ~]# ll -h /tmp/yum_save_tx.2019-12-11.12-42.LMzjWj.yumtx
-rw------- 1 root root 20K Dec 11 12:42 /tmp/yum_save_tx.2019-12-11.12-42.LMzjWj.yumtx
寻找大于10M文件
[root@centos71 ~]# find /var/ -size +10M
/var/lib/rpm/Packages
/var/log/messages-20191212
/var/cache/yum/x86_64/7/base/gen/primary_db.sqlite
/var/cache/yum/x86_64/7/base/gen/filelists_db.sqlite
/var/cache/yum/x86_64/7/updates/gen/primary_db.sqlite
/var/cache/yum/x86_64/7/updates/gen/filelists_db.sqlite
/var/cache/yum/x86_64/7/epel/gen/primary_db.sqlite
注意找不到大小带浮点数的文件
[root@centos71 ~]# du -s /app/ -h 412K /app/ [root@centos71 ~]# ls /app/ -l total 84 drwxrwxrwx 2 500 500 4096 Apr 1 2002 cmatrix-1.2a -rw-r--r-- 1 root root 74376 Dec 10 15:53 cmatrix-1.2a.tar.gz [root@centos71 ~]# find /app/ -size -1.5k find: Invalid argument `-1.5k' to -size
[root@centos71 ~]# dd if=/dev/zero of=/app/03.txt bs=150M count=10
10+0 records in
10+0 records out
1572864000 bytes (1.6 GB) copied, 69.2731 s, 22.7 MB/s
[root@centos71 ~]# ll -h /app/
total 1.5G
-rw-r--r-- 1 root root 1.5G Dec 13 14:48 03.txt
drwxrwxrwx 2 500 500 4.0K Dec 10 15:55 cmatrix-1.2a
-rw-r--r-- 1 root root 73K Dec 10 15:53 cmatrix-1.2a.tar.gz
[root@centos71 ~]# find /app/ -size 1.5G
find: Invalid argument `1.5G' to -size
(五)根据条件逻辑查找数据
请找出文件大小是大于6k 并且找出文件名称xxxx开头的文件
注意逻辑关系 -a 是默认的,可加可不加
[root@centos71 test]# pwd
/test
[root@centos71 test]# ll -h
total 140K
-rw-r--r-- 1 root root 137K Dec 16 00:52 english_words.txt
[root@centos71 test]# find . -name "english*" -size +6k
./english_words.txt
注意如果没有指明路径那么默认就是当前路径
[root@centos71 test]# find -name "english*" -a -size +6k
./english_words.txt
[root@centos71 test]# find -size +6k -a -name "english*"
./english_words.txt
找出文件名称是chinese开头的文件或者english开头的文件
联系中学数学并和或的关系
[root@centos71 test]# find -name "chinese*" -o -name "english*"
./english_words.txt
./chinese.txt
(六)根据时间条件查找数据
查找距离当前时间7天以前的数据,也就是比七天更长的数据
数据包括文件和目录
[root@centos71 ~]# ll -d /etc/sysconfig/network -rw-r--r--. 1 root root 22 Nov 26 17:36 /etc/sysconfig/network [root@centos71 ~]# find /etc/sysconfig/network -mtime +7 /etc/sysconfig/network
找距离当前时间7天以内的数据
[root@centos71 ~]# ll -t /var/log total 134236 -rw-r--r--. 1 root root 300760 Dec 16 12:12 lastlog -rw-rw-r--. 1 root utmp 153600 Dec 16 12:12 wtmp -rw------- 1 root root 4547 Dec 16 12:12 secure -rw------- 1 root root 52361 Dec 16 12:12 messages -rw------- 1 root root 6310 Dec 16 12:01 cron -rw------- 1 root root 784 Dec 16 09:58 maillog -rw-------. 1 root root 32204 Dec 16 09:58 boot.log -rw-r--r--. 1 root root 74329 Dec 16 09:58 vmware-vmsvc.log -rw-r--r-- 1 root root 678 Dec 16 09:51 vmware-network.log -rw-r--r-- 1 root root 700 Dec 16 09:51 vmware-network.1.log -rw-r--r-- 1 root root 678 Dec 16 09:50 vmware-network.2.log -rw-r--r-- 1 root root 700 Dec 16 09:50 vmware-network.3.log -rw-r--r-- 1 root root 678 Dec 16 08:32 vmware-network.4.log -rw-r--r-- 1 root root 700 Dec 16 07:45 vmware-network.5.log -rw-r--r--. 1 root root 51229 Dec 16 05:14 vmware-vgauthsvc.log.0 -rw-r--r-- 1 root root 122258 Dec 16 05:14 dmesg -rw-r--r-- 1 root root 122225 Dec 16 05:07 dmesg.old -rw------- 1 root root 0 Dec 15 17:16 spooler -rw------- 1 root root 8082 Dec 15 17:16 boot.log-20191215 -rw------- 1 root root 14461 Dec 15 17:16 cron-20191215 -rw------- 1 root root 452288 Dec 15 17:06 messages-20191215 -rw------- 1 root root 15561 Dec 15 16:51 secure-20191215 -rw------- 1 root root 1780 Dec 15 16:51 maillog-20191215 -rw-r--r-- 1 root root 678 Dec 15 16:51 vmware-network.6.log -rw-r--r-- 1 root root 700 Dec 14 21:12 vmware-network.7.log drwxrwx--- 2 nginx root 70 Dec 14 10:36 nginx -rw------- 1 root root 7985 Dec 14 10:36 boot.log-20191214 -rw-r--r-- 1 root root 678 Dec 14 09:43 vmware-network.8.log -rw-r--r-- 1 root root 756 Dec 13 22:48 vmware-network.9.log -rw------- 1 root root 31945 Dec 13 09:17 boot.log-20191213 -rw-------. 1 root root 9666 Dec 12 21:47 yum.log -rw-------. 1 root utmp 1536 Dec 12 11:41 btmp -rw------- 1 root root 0 Dec 12 03:45 spooler-20191215 -rw------- 1 root root 15955 Dec 12 03:45 boot.log-20191212 -rw-------. 1 root root 25722 Dec 12 03:45 cron-20191212 -rw-------. 1 root root 134742312 Dec 12 03:01 messages-20191212 -rw-------. 1 root root 69243 Dec 11 23:17 secure-20191212 -rw-------. 1 root root 2896 Dec 11 23:04 maillog-20191212 -rw------- 1 root root 23986 Dec 11 09:39 boot.log-20191211 -rw------- 1 root root 24281 Dec 10 03:22 boot.log-20191210 -rw------- 1 root root 23606 Dec 9 03:43 boot.log-20191209 -rw-------. 1 root root 0 Dec 5 09:19 spooler-20191212 -rw-------. 1 root root 13252 Dec 5 09:19 cron-20191205 -rw-------. 1 root root 1192899 Dec 5 09:15 messages-20191205 -rw-------. 1 root root 18702 Dec 5 08:34 secure-20191205 -rw-------. 1 root root 1746 Dec 5 08:34 maillog-20191205 -rw-------. 1 root utmp 1920 Nov 27 23:24 btmp-20191205 drwxr-xr-x. 2 root root 23 Nov 26 17:39 tuned -rw-r--r--. 1 root root 0 Nov 26 17:39 firewalld drwx------. 2 root root 23 Nov 26 17:39 audit drwxr-xr-x. 2 root root 6 Nov 26 17:37 rhsm drwxr-xr-x. 2 root root 176 Nov 26 17:37 anaconda -rw-------. 1 root root 0 Nov 26 17:33 spooler-20191205 -rw-r--r--. 1 root root 193 Nov 26 17:32 grubby_prune_debug -rw-------. 1 root root 0 Nov 26 17:32 tallylog drwxr-xr-x. 2 chrony chrony 6 Aug 8 19:40 chrony
[root@centos71 ~]# find /var/log/ -mtime -7 /var/log/ /var/log/lastlog /var/log/wtmp /var/log/tuned/tuned.log /var/log/audit/audit.log /var/log/boot.log /var/log/vmware-vmsvc.log /var/log/vmware-vgauthsvc.log.0 /var/log/yum.log /var/log/dmesg.old /var/log/vmware-network.4.log /var/log/vmware-network.3.log /var/log/boot.log-20191210 /var/log/maillog-20191212 /var/log/cron /var/log/messages-20191215 /var/log/spooler /var/log/vmware-network.1.log /var/log/boot.log-20191212 /var/log/boot.log-20191213 /var/log/vmware-network.5.log /var/log/nginx /var/log/nginx/access.log /var/log/nginx/error.log /var/log/nginx/error.log-20191214.gz /var/log/vmware-network.8.log /var/log/vmware-network.log /var/log/cron-20191212 /var/log/secure-20191212 /var/log/cron-20191215 /var/log/messages /var/log/vmware-network.7.log /var/log/vmware-network.9.log /var/log/btmp /var/log/messages-20191212 /var/log/boot.log-20191215 /var/log/maillog-20191215 /var/log/maillog /var/log/secure-20191215 /var/log/secure /var/log/spooler-20191215 /var/log/vmware-network.2.log /var/log/boot.log-20191214 /var/log/dmesg /var/log/boot.log-20191211 /var/log/vmware-network.6.log
找距离当前时间之前正好7天的数据
注意只精确到天
[root@centos71 ~]# find /var/log -mtime 7 /var/log/boot.log-20191209 [root@centos71 ~]# ll /var/log/boot.log-20191209 -rw------- 1 root root 23606 Dec 9 03:43 /var/log/boot.log-20191209 [root@centos71 ~]# date Mon Dec 16 12:17:30 CST 2019
find命令实现批量处理数据
(一)find查找数据&&批量删除
步骤一:将要处理数据找出来
步骤二:将数据批量删除
方法一:find命令参数及动作
[root@centos71 ~]# ls /test
mailx.conf mantisbt_format_analyzer_libreport.conf mariadb-x86_64.conf
mailx_event.conf mantisbt_format.conf mke2fs.conf
man_db.conf mantisbt_formatdup_analyzer_libreport.conf
mantisbt.conf mantisbt_formatdup.conf
[root@centos71 ~]# ls /test2
mailx.conf mantisbt_format_analyzer_libreport.conf mariadb-x86_64.conf
mailx_event.conf mantisbt_format.conf mke2fs.conf
man_db.conf mantisbt_formatdup_analyzer_libreport.conf
mantisbt.conf mantisbt_formatdup.conf
[root@centos71 ~]# ls /test3
mailx.conf mantisbt_format_analyzer_libreport.conf mariadb-x86_64.conf
mailx_event.conf mantisbt_format.conf mke2fs.conf
man_db.conf mantisbt_formatdup_analyzer_libreport.conf
mantisbt.conf mantisbt_formatdup.conf
[root@centos71 ~]# mkdir /test4
[root@centos71 ~]# cp /test/* /test4
[root@centos71 ~]# ls /test4
mailx.conf mantisbt_format_analyzer_libreport.conf mariadb-x86_64.conf
mailx_event.conf mantisbt_format.conf mke2fs.conf
man_db.conf mantisbt_formatdup_analyzer_libreport.conf
mantisbt.conf mantisbt_formatdup.conf
找到所有以m开头的数据
[root@centos71 ~]# find /test -type f -name "m*"
/test/mailx.conf
/test/mailx_event.conf
/test/man_db.conf
/test/mantisbt.conf
/test/mantisbt_format_analyzer_libreport.conf
/test/mantisbt_format.conf
/test/mantisbt_formatdup_analyzer_libreport.conf
/test/mantisbt_formatdup.conf
/test/mariadb-x86_64.conf
/test/mke2fs.conf
将m开头的数据全部删除
注意不要轻易删除目录,所以得指定文件类型
[root@centos71 ~]# find /test -type f -name "m*" -delete
[root@centos71 ~]# ls /test
[root@centos71 ~]# find /test -type f -name "m*"
方法二:管道+xargs命令进行处理
直接使用rm无法删除
xargs起到了过渡作用,是中介,识别前面的字符信息,作为管道后面命令的参数
注意对象实际上也是参数,正就像目录也是文件
[root@centos71 ~]# find /test2 -type f -name "m*"
/test2/mailx.conf
/test2/mailx_event.conf
/test2/man_db.conf
/test2/mantisbt.conf
/test2/mantisbt_format_analyzer_libreport.conf
/test2/mantisbt_format.conf
/test2/mantisbt_formatdup_analyzer_libreport.conf
/test2/mantisbt_formatdup.conf
/test2/mariadb-x86_64.conf
/test2/mke2fs.conf
[root@centos71 ~]# find /test2 -type f -name "m*" | xargs rm
[root@centos71 ~]# find /test2 -type f -name "m*"
[root@centos71 ~]# ls /test2
[root@centos71 ~]# echo {01..10} | xargs -n2 01 02 03 04 05 06 07 08 09 10 [root@centos71 ~]# echo {01..10} | xargs -n3 01 02 03 04 05 06 07 08 09 10 [root@centos71 ~]# echo {01..10} | xargs -n4 01 02 03 04 05 06 07 08 09 10 [root@centos71 ~]# echo {01..10} 01 02 03 04 05 06 07 08 09 10
方法三:反引号进行处理
很好理解,反引号就像中学数学里面的四则运算里面的括号
提高了运算的优先级
[root@centos71 ~]# bc bc 1.06.95 Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty'. 2*(3+6) 18 ^C (interrupt) Exiting bc.
[root@centos71 ~]# find /test3 -type f -name "m*"
/test3/mailx_event.conf
/test3/mantisbt.conf
/test3/mantisbt_format.conf
/test3/mantisbt_format_analyzer_libreport.conf
/test3/mantisbt_formatdup.conf
/test3/mantisbt_formatdup_analyzer_libreport.conf
/test3/mailx.conf
/test3/mariadb-x86_64.conf
/test3/man_db.conf
/test3/mke2fs.conf
[root@centos71 ~]# rm -f ` find /test3 -type f -name "m*"`
[root@centos71 ~]# find /test3 -type f -name "m*"
[root@centos71 ~]# ls /test3
方法四: find参数 -exec
钓上一条鱼就放到框里面,exec相当于钓鱼,{ }相当于放鱼的桶
;作用是隔开命令
注意xarg和exec的区别,前者有管道,后者有;
[root@centos71 ~]# find /test4 -type f -name "m*"
/test4/mailx.conf
/test4/mailx_event.conf
/test4/man_db.conf
/test4/mantisbt.conf
/test4/mantisbt_format_analyzer_libreport.conf
/test4/mantisbt_format.conf
/test4/mantisbt_formatdup_analyzer_libreport.conf
/test4/mantisbt_formatdup.conf
/test4/mariadb-x86_64.conf
/test4/mke2fs.conf
[root@centos71 ~]# find /test4 -type f -name "m*" -exec rm {} ;
[root@centos71 ~]# find /test4 -type f -name "m*"
[root@centos71 ~]# ls /test4
(二)find查找数据&&批量复制
[root@centos71 ~]# find /etc/ -name "m*.conf" /etc/mailx_event.conf /etc/mantisbt.conf /etc/mantisbt_format.conf /etc/mantisbt_format_analyzer_libreport.conf /etc/mantisbt_formatdup.conf /etc/mantisbt_formatdup_analyzer_libreport.conf /etc/mailx.conf /etc/mariadb-x86_64.conf /etc/man_db.conf /etc/mke2fs.conf
[root@centos71 ~]# ls /test2 [root@centos71 ~]# find /etc/ -name "m*.conf" | xargs -i cp {} /test2 [root@centos71 ~]# ls /test2 mailx.conf mantisbt_format_analyzer_libreport.conf mariadb-x86_64.conf mailx_event.conf mantisbt_format.conf mke2fs.conf man_db.conf mantisbt_formatdup_analyzer_libreport.conf mantisbt.conf mantisbt_formatdup.conf
(三)find查找数据&&批量移动
注意移动和复制方法相同,换命令即可
mv不需要添加参数
方法一
[root@centos71 ~]# ls /test2 mailx.conf mantisbt_format_analyzer_libreport.conf mariadb-x86_64.conf mailx_event.conf mantisbt_format.conf mke2fs.conf man_db.conf mantisbt_formatdup_analyzer_libreport.conf mantisbt.conf mantisbt_formatdup.conf [root@centos71 ~]# ls /test3 [root@centos71 ~]# find /test2 -type f -name "m*conf" /test2/mailx_event.conf /test2/mantisbt.conf /test2/mantisbt_format.conf /test2/mantisbt_format_analyzer_libreport.conf /test2/mantisbt_formatdup.conf /test2/mantisbt_formatdup_analyzer_libreport.conf /test2/mailx.conf /test2/mariadb-x86_64.conf /test2/man_db.conf /test2/mke2fs.conf [root@centos71 ~]# find /test2 -type f -name "m*conf" | xargs -i mv -t {} /test3 mv: target ‘/test2/mailx_event.conf’ is not a directory mv: target ‘/test2/mantisbt.conf’ is not a directory mv: target ‘/test2/mantisbt_format.conf’ is not a directory mv: target ‘/test2/mantisbt_format_analyzer_libreport.conf’ is not a directory mv: target ‘/test2/mantisbt_formatdup.conf’ is not a directory mv: target ‘/test2/mantisbt_formatdup_analyzer_libreport.conf’ is not a directory mv: target ‘/test2/mailx.conf’ is not a directory mv: target ‘/test2/mariadb-x86_64.conf’ is not a directory mv: target ‘/test2/man_db.conf’ is not a directory mv: target ‘/test2/mke2fs.conf’ is not a directory [root@centos71 ~]# find /test2 -type f -name "m*conf" | xargs -i mv {} /test3 [root@centos71 ~]# ls /test3 mailx.conf mantisbt_format_analyzer_libreport.conf mariadb-x86_64.conf mailx_event.conf mantisbt_format.conf mke2fs.conf man_db.conf mantisbt_formatdup_analyzer_libreport.conf mantisbt.conf mantisbt_formatdup.conf
方法二
[root@centos71 ~]# ls /test3 mailx.conf mantisbt_format_analyzer_libreport.conf mariadb-x86_64.conf mailx_event.conf mantisbt_format.conf mke2fs.conf man_db.conf mantisbt_formatdup_analyzer_libreport.conf mantisbt.conf mantisbt_formatdup.conf [root@centos71 ~]# ls /test2 [root@centos71 ~]# find /test3 -type f -name "m*conf" | xargs -i mv -t /test2 mv: missing file operand Try 'mv --help' for more information. mv: missing file operand Try 'mv --help' for more information. mv: missing file operand Try 'mv --help' for more information. mv: missing file operand Try 'mv --help' for more information. mv: missing file operand Try 'mv --help' for more information. mv: missing file operand Try 'mv --help' for more information. mv: missing file operand Try 'mv --help' for more information. mv: missing file operand Try 'mv --help' for more information. mv: missing file operand Try 'mv --help' for more information. mv: missing file operand Try 'mv --help' for more information. [root@centos71 ~]# find /test3 -type f -name "m*conf" | xargs mv -t /test2 [root@centos71 ~]# ls /test2 mailx.conf mantisbt_format_analyzer_libreport.conf mariadb-x86_64.conf mailx_event.conf mantisbt_format.conf mke2fs.conf man_db.conf mantisbt_formatdup_analyzer_libreport.conf mantisbt.conf mantisbt_formatdup.conf [root@centos71 ~]# ls /test3
方法三
[root@centos71 ~]# ls /test2 mailx.conf mantisbt_format_analyzer_libreport.conf mariadb-x86_64.conf mailx_event.conf mantisbt_format.conf mke2fs.conf man_db.conf mantisbt_formatdup_analyzer_libreport.conf mantisbt.conf mantisbt_formatdup.conf [root@centos71 ~]# ls /test3 [root@centos71 ~]# mv `find /test2 -type f -name "m*conf"` /test3 [root@centos71 ~]# ls /test3 mailx.conf mantisbt_format_analyzer_libreport.conf mariadb-x86_64.conf mailx_event.conf mantisbt_format.conf mke2fs.conf man_db.conf mantisbt_formatdup_analyzer_libreport.conf mantisbt.conf mantisbt_formatdup.conf [root@centos71 ~]# ls /test2