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
/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 | wc

查看/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

查看/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

查找指定的一层

只在第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
drwxrwxrwx Apr cmatrix-.2a
-rw-r--r-- root root Dec : cmatrix-.2a.tar.gz
[root@centos71 ~]# find /app/ -size -.5k
find: Invalid argument `-.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--. root root Nov : /etc/sysconfig/network
[root@centos71 ~]# find /etc/sysconfig/network -mtime +
/etc/sysconfig/network

找距离当前时间7天以内的数据

[root@centos71 ~]# ll  -t   /var/log
total
-rw-r--r--. root root Dec : lastlog
-rw-rw-r--. root utmp Dec : wtmp
-rw------- root root Dec : secure
-rw------- root root Dec : messages
-rw------- root root Dec : cron
-rw------- root root Dec : maillog
-rw-------. root root Dec : boot.log
-rw-r--r--. root root Dec : vmware-vmsvc.log
-rw-r--r-- root root Dec : vmware-network.log
-rw-r--r-- root root Dec : vmware-network..log
-rw-r--r-- root root Dec : vmware-network..log
-rw-r--r-- root root Dec : vmware-network..log
-rw-r--r-- root root Dec : vmware-network..log
-rw-r--r-- root root Dec : vmware-network..log
-rw-r--r--. root root Dec : vmware-vgauthsvc.log.
-rw-r--r-- root root Dec : dmesg
-rw-r--r-- root root Dec : dmesg.old
-rw------- root root Dec : spooler
-rw------- root root Dec : boot.log-
-rw------- root root Dec : cron-
-rw------- root root Dec : messages-
-rw------- root root Dec : secure-
-rw------- root root Dec : maillog-
-rw-r--r-- root root Dec : vmware-network..log
-rw-r--r-- root root Dec : vmware-network..log
drwxrwx--- nginx root Dec : nginx
-rw------- root root Dec : boot.log-
-rw-r--r-- root root Dec : vmware-network..log
-rw-r--r-- root root Dec : vmware-network..log
-rw------- root root Dec : boot.log-
-rw-------. root root Dec : yum.log
-rw-------. root utmp Dec : btmp
-rw------- root root Dec : spooler-
-rw------- root root Dec : boot.log-
-rw-------. root root Dec : cron-
-rw-------. root root Dec : messages-
-rw-------. root root Dec : secure-
-rw-------. root root Dec : maillog-
-rw------- root root Dec : boot.log-
-rw------- root root Dec : boot.log-
-rw------- root root Dec : boot.log-
-rw-------. root root Dec : spooler-
-rw-------. root root Dec : cron-
-rw-------. root root Dec : messages-
-rw-------. root root Dec : secure-
-rw-------. root root Dec : maillog-
-rw-------. root utmp Nov : btmp-
drwxr-xr-x. root root Nov : tuned
-rw-r--r--. root root Nov : firewalld
drwx------. root root Nov : audit
drwxr-xr-x. root root Nov : rhsm
drwxr-xr-x. root root Nov : anaconda
-rw-------. root root Nov : spooler-
-rw-r--r--. root root Nov : grubby_prune_debug
-rw-------. root root Nov : tallylog
drwxr-xr-x. chrony chrony Aug : chrony
[root@centos71 ~]# find  /var/log/   -mtime -
/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.
/var/log/yum.log
/var/log/dmesg.old
/var/log/vmware-network..log
/var/log/vmware-network..log
/var/log/boot.log-
/var/log/maillog-
/var/log/cron
/var/log/messages-
/var/log/spooler
/var/log/vmware-network..log
/var/log/boot.log-
/var/log/boot.log-
/var/log/vmware-network..log
/var/log/nginx
/var/log/nginx/access.log
/var/log/nginx/error.log
/var/log/nginx/error.log-.gz
/var/log/vmware-network..log
/var/log/vmware-network.log
/var/log/cron-
/var/log/secure-
/var/log/cron-
/var/log/messages
/var/log/vmware-network..log
/var/log/vmware-network..log
/var/log/btmp
/var/log/messages-
/var/log/boot.log-
/var/log/maillog-
/var/log/maillog
/var/log/secure-
/var/log/secure
/var/log/spooler-
/var/log/vmware-network..log
/var/log/boot.log-
/var/log/dmesg
/var/log/boot.log-
/var/log/vmware-network..log

找距离当前时间之前正好7天的数据

注意只精确到天

[root@centos71 ~]# find   /var/log    -mtime
/var/log/boot.log-
[root@centos71 ~]# ll /var/log/boot.log-
-rw------- root root Dec : /var/log/boot.log-
[root@centos71 ~]# date
Mon Dec :: CST

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  {..}  |  xargs  -n2

[root@centos71 ~]# echo  {..}  |  xargs  -n3

[root@centos71 ~]# echo  {..}  |  xargs  -n4

[root@centos71 ~]# echo  {..}
         

方法三:反引号进行处理

很好理解,反引号就像中学数学里面的四则运算里面的括号

提高了运算的优先级

[root@centos71 ~]# bc
bc 1.06.
Copyright -, , , , , Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
*(+) ^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

Linux文件及目录查找的更多相关文章

  1. CentOS(十)--与Linux文件和目录管理相关的一些重要命令②

    在结束了第二期的广交会实习之后,又迎来了几天休闲的日子,继续学习Linux.在上一篇随笔 Linux学习之CentOS(十七)--与Linux文件和目录管理相关的一些重要命令① 中,详细记录了与Lin ...

  2. Linux - 文件和目录常用命令

    文件和目录常用命令 目标 查看目录内容 ls 切换目录 cd 创建和删除操作 touch rm mkdir 拷贝和移动文件 cp mv 查看文件内容 cat more grep 其他 echo 重定向 ...

  3. linux文件与目录管理笔记

    ### Linux文件与目录管理 ---------- 绝对路径: / 相对路径:不以/开头的 当前目录 . 上一个工作目录 - 用户主目录 ~ root账户的主目录是/root 其他用户是/home ...

  4. Linux文件和目录的属性及权限总结

    本文讲述的是文件或目录的属性及权限,比如索引节点inode.文件类型.文件权限及属主:还对setuid.setgid及粘贴位进行了相关的讲解.其中,对ln.chmod.chown.chgrp.umas ...

  5. Linux文件和目录权限详细讲解

    转载请标明出处: http://www.cnblogs.com/why168888/p/5965180.html 本文出自:[Edwin博客园] Linux文件和目录权限解读 如何设置Linxu文件和 ...

  6. Linux 文件与目录管理

    Linux 文件与目录管理 我们知道Linux的目录结构为树状结构,最顶级的目录为根目录 /. 其他目录通过挂载可以将它们添加到树中,通过解除挂载可以移除它们. 在开始本教程前我们需要先知道什么是绝对 ...

  7. CentOS(九)--与Linux文件和目录管理相关的一些重要命令①

       接上一篇文章,实际生产过程中的目录管理一定要注意用户是root 还是其他用户. 一.目录与路径 1.相对路径与绝对路径 因为我们在Linux系统中,常常要涉及到目录的切换,所以我们必须要了解 & ...

  8. System.Data.SqlClient.SqlError: 对文件……的目录查找失败[转]

    System.Data.SqlClient.SqlError: 对文件……的目录查找失败,出现操作系统错误 3 的处理办法 在还原SQL SERVER数据库时出现了查找目录失败的原因,困扰了我一个多小 ...

  9. linux文件权限目录配置笔记

    ###linux 文件权限目录配置笔记 ---------- 多人多任务环境 linux 一般将文件可存取的身份分为三个类别:owner group others Permission deny ls ...

随机推荐

  1. CMakeLists.txt中使用循环

    编译一个安卓下的so,此so依赖其他的库,通过循环简化操作 set(UVC_LIBS UVCCamera uvc usb100 jpeg-turbo1500) FOREACH(UVC_LIB ${UV ...

  2. Spring 注解概览

    从Java5.0开始,Java开始支持注解.Spring做为Java生态中的领军框架,从2.5版本后也开始支持注解.相比起之前使用xml来配置Spring框架,使用注解提供了更多的控制Spring框架 ...

  3. 15.永恒之蓝exp----

    永恒之蓝exp 2017年,影响于全世界 SMB ripid7官网获取ms17-010的exp信息 Rapid7: https://www.rapid7.com/db/modules/exploit/ ...

  4. 【Linux开发】./configure,make,make install的作用

    这些都是典型的使用GNU的AUTOCONF和AUTOMAKE产生的程序的安装步骤. ./configure是用来检测你的安装平台的目标特征的.比如它会检测你是不是有CC或GCC,并不是需要CC或GCC ...

  5. 深入理解java:1.1. 类加载器

    从java的动态性到类加载机制   我们知道,Java是一种动态语言. 那么怎样理解这个“动态”呢? 或者说一门语言具备了什么特性,才能称之为动态语言呢? 对于java,我是这样理解的. 我们都知道J ...

  6. Binary Tree Level Order Traversal(二叉树广度优先遍历或逐层遍历)

    来源:https://leetcode.com/problems/binary-tree-level-order-traversal Given a binary tree, return the l ...

  7. Ubuntu 16.04简单配置备忘录

    1.几个安装包的地址 1.Linux QQ:https://im.qq.com/linuxqq/index.html 2.网易云音乐:http://s1.music.126.net/download/ ...

  8. Nginx 3.使用配置

    转 https://www.cnblogs.com/wcwnina/p/9946747.html 本文只针对Nginx在不加载第三方模块的情况能处理哪些事情,由于第三方模块太多所以也介绍不完,当然本文 ...

  9. PHP数据结构基本概念

    原文:https://www.cnblogs.com/crystaltu/p/6408484.html 学习任何一种技术都应该先清楚它的基本概念,这是学习任何知识的起点!本文是讲述数据结构的基本概念, ...

  10. SSM框架中数据库无法连接的问题

    首先是SSM框架中所有的配置都是没有问题的,而且项目在其他人的环境上也能正常访问数据库:那么最有可能的就是数据库版本的问题导致数据库连接不上,服务器给我的报错是: 15:37:25.902 [C3P0 ...