09find命令结合通配符和特殊符号使用


find 命令是 Unix 和 Linux 系统中一个极其强大且多功能的工具,主要用于在文件系统中高效地搜索文件和目录。它支持基于多种条件进行查找,包括但不限于文件名、文件类型、文件大小以及最后修改时间等。通过结合使用通配符和其他特殊符号,用户能够构建出更加灵活和精确的搜索查询,从而极大地提升了文件管理与检索的效率。

1. 通配符和特殊符号说明

通配符与特殊符号是命令行操作中不可或缺的一部分,它们让命令更加灵活高效,能够根据不同的模式匹配文件名、控制程序流程或是实现输入输出的重定向。在Linux系统中,通配符和特殊符号常用于文件名模式匹配,特别是在命令行中使用。这些通配符和特殊符号可以帮助用户更高效地处理文件和目录。

1.1 通配符

通配符其实也算是一类特殊字符,可以实现字符匹配的特殊功能,因此也叫做通配符。例如,用来模糊搜索系统中的文件。当查找文件时,可以使用通配符中的“*”或“?”字符来代替一个或多个真正的字符,从而更快地找到所需的文件。
利用通配符可以在Linux命令行或shell脚本中轻松地匹配或查找所需的文件,以提高工作效率。

  • 通配符列表
符号作用举例
* 匹配任意(0个或多个)数量字符或字符串,包括空字符串例如,*.txt 匹配所有扩展名为 .txt 的文件。
?匹配任意1个字符,有且只有一个字符例如,file?.txt 匹配 file1.txtfile2.txt 类型的,但不匹配 file12.txt
[]匹配 [] 中任何一个字符,可以是任意不连续字符例如,file[123].txt 匹配 file1.txtfile2.txtfile3.txt
[-]匹配 [] 中的任意一个字符,字符前后要连续,也可以用连续数字,即[1-9]例如,file[a-c].txt 匹配 filea.txtfileb.txtfilec.txt
[!]不匹配括号里面的任何一个字符,这里的“!”号可以用“^”替代例如,file[!a-c].txt 匹配 file1.txtfileq.txtfilep.txt等。
  • 符号实践举例
# 通配符“*”号的实践
[root@localhost test]# ls
lisi.txt  wangwu.sh  zhangsan.txt
[root@localhost test]# ls *.txt      # 以.txt结尾的文件
lisi.txt  zhangsan.txt
[root@localhost test]# ls *.sh       # 以.sh结尾的文件
wangwu.sh

# 通配符“?”号的实践
[root@localhost test]# ls
lisi.txt  wangwu.sh  zhangsan1.txt  zhangsan2.txt  zhangsan.txt
[root@localhost test]# ls zhangsan?.txt      # 成功匹配
zhangsan1.txt  zhangsan2.txt
[root@localhost test]# ls ?.txt              # 没有匹配到对应文件
ls: 无法访问?.txt: 没有那个文件或目录
[root@localhost test]# ls ????.txt           # 成功匹配到lisi.txt
lisi.txt

# 通配符“[]”的实践
# 任意连续字符,例如[abcd]、[1-9]等。
[root@localhost test]# ls
a.txt  c.sh  lisi.txt  wangwu.sh  zhangsan1.txt  zhangsan2.txt  zhangsan.txt
[root@localhost test]# ls [abcd].txt          # 匹配【abcd】中任意一个
a.txt  
[root@localhost test]# ls zhangsan[1-5].txt   # 匹配【12345】中任意一个
zhangsan1.txt  zhangsan2.txt
[root@localhost test]# ls [!zhangsan]*        # 匹配【zhangsan】以外的任意一个
c.sh  lisi.txt  wangwu.sh
[root@localhost test]# ls [^zhangsan]*        # 匹配【zhangsan】以外的任意一个
c.sh  lisi.txt  wangwu.sh

1.2 路径相关的特殊符号

  • 符号列表
符号作用
~用户的家目录,超级用户为“/root”,普通用户为“/home”
-代表上一次(相对于当前路径)用户所在的路径
.代表当前目录
..代表上一级目录
  • 符号实践举例
# 波浪线(~)的实践
[root@localhost test]# pwd
/root/test                   #<==当前路径。
[root@localhost test]# cd ~  #<==切换到用户家目录,即/root。
[root@localhost ~]# pwd
/root                        #<==root用户家目录。

# 短横杠(-)的实践
[root@localhost ~]# pwd
/root
[root@localhost ~]# echo $PWD       #<==查看上一次所在路径的变量命令。
/root/test                          #<==上一次所在路径为/root/test。
[root@localhost ~]# cd -            #<==切换到上一次所在路径,即/root/test。
/root/test
[root@localhost test]# echo $PWD    #<==再查看上一次所在路径。
/root
[root@localhosty test]# cd -        #<==切到上一次所在路径,即/root。
/root

# 点号(.)的实践
[root@localhost test]# pwd                  #<==查看当前路径。
/root/test
[root@localhost test]# cd .                 #<==切换到点号所在路径。
[root@localhost test]# pwd
/root/test        #<==还是在原来的路径下。
[root@localhost test]# find . -name "*.sh"  #<==查找当前路径下的所有符合“*.sh”的文件。
./c.sh
./inca.sh
./a.sh
./cd.sh
./d.sh

# 双点号(..)的实践
[root@localhost test]# pwd        #<==查看当前路径。
/root/test
[root@localhost test]# cd ..      #<==切换到双点号所在路径。
[root@localhost ~]# pwd
/root                             #<==路径变到了“/root”下,原来是/root/test,上级正好是/root。
[root@localhost ~]# mkdir dir1
[root@localhost ~]# ls -a dir1/
.  ..                             #<==任何一个新目录下都有一个点号(当前目录)和一个双点号(上级目录)。

1.3 引号特殊符号

  • 符号列表
符号作用
''引号里面有什么内容,处理时就一直保持引号之前的内容不变
""如果双引号内容中有命令(要反引一下)、变量、特殊转义符等,会先将变量、命令、转义字符解析出来,然后输出最终的内容。
若在平时引用字符串时,不知道应该如何引用,则可以默认使用双引号,这种引用也称为弱引用。
不用引号不使用引号的功能与双引号类似,但由于没有引号,很难确定字符串的边界,因此很容易出现各种未知的操作错误。(不建议使用
``反引号引起来的字符串,系统首先会当作命令进行解析,然后针对解析后的结果做进一步的处理。反引号一般用于引用命令,执行的时候命令会被执行,相当于“$()”,赋值和输出都要将命令用反引号(``)引起来。
  • 符号实践举例
# 单引号('')的实践
[root@localhost test]# echo 'date'       #<==即使是命令也会作为字符串原样输出。
date
[root@localhost test]# echo '`date`'     #<==用反引号引起来也没用,还是原样输出。
'date'

# 双引号("")的实践
[root@localhost test]# echo "date"       #<==被引用的内容作为字符串输出。
date
[root@localhost test]# echo "`date`"     #<==经过反引号引起来的命令会在解析后输出。
Sat Mar 31 21:05:59 CST 2018
[root@localhost test]# echo "$(date)`which mkdir`"      #<==经过反引号或“$()”引起来的命令会在解析后输出。
Sat Mar 31 21:06:28 CST 2018 /bin/mkdir
[root@localhost test]# echo "today is `date +%w`"
today is 6

# 无引号实践
[root@localhost test]# echo "today is 'date +%w'"
today is 6
[root@localhost test]# echo today is 'date +%w'
today is 6
[root@localhost test]# echo I am man
I am man
[root@localhost test]# echo $(date)'which mkdir'
Sat Mar 31 21:13:51 CST 2018 /bin/mkdir

# 反引号(``)的实践
[root@localhost ~]# date          #<==打印系统当前日期的命令。
Sat Mar 31 20:50:35 CST 2018
[root@localhost ~]# echo date     #<==若不加反引号,就会将date当作字符串输出。
date
[root@localhost ~]# echo `date`   #<==将date命令反引起来,系统会将date当作是一个命令。
Sat Mar 31 20:50:42 CST 2018
[root@localhost ~]# echo `aa`     #<==aa被反引起来之后,将作为命令解析。
-bash: aa: command not found      #<==aa命令找不到,从提示信息可以得知,系统将aa当作命令执行了。
[root@localhost ~]# cd test/
[root@localhost test]# touch `date +%F`.txt  #<==将命令反引起来作为待创建的文件名。
[root@localhost test]# ls `date +%F`.txt     #<==查看
2018-03-31.txt
[root@localhost test]# ls -l `which cat`     #<==将查看cat命令反引起来作为ls的待处理文件。
-rwxr-xr-x. 1 root root 48568 Mar 23  2017 /bin/cat

1.4 重定向特殊符号

  • 符号列表
名称描述符说明
标准输入代码为0,配合“<”或“<<”使用数据流从右到左
标准输出代码为1,配合“>”或“>>”使用数据流从左到右
标准错误代码为2,配合“>”或“>>”使用数据流从左到右
重定向符号,数据流是箭头方向
标准输入重定向“0<”或“<”清空已有内容,数据一般从文件流向处理命令
标准输入追加重定向“0<<”或“<<”追加内容到底部,数据一般从文件流向处理命令
标准输出重定向“1>”或“>”正常输出重定向到文件,会清空已有的内容
标准输出追加重定向“1>>”或“>>”将内容追加重定向到文件底部,不会清空已有的内容
标准错误输出重定向“2>”将标准错误内容重定向到文件,如果文件存在内容则清空
标准错误输出追加重定向“2>>”将标准错误内容追加到文件底部,不会清空已有的内容

此外还有一个特殊重定向用法:将标准错误重定向到标准输出,即标准错误和标准输出一样重定向到文件中,这个功能有如下3种实现命令的方法。

# 方法1: 
echo "I am man" 1>>boy.txt 2>>boy.txt       # 分别进行标准输出和标准错误
# 方法2: 
echo "I am man" &>>boy.txt                  # 将标准输出和标准错误同时追加
# 方法3: 
echo "I am man" >>boy.txt 2>&1              # 先标准输出,然后标准错误重定向标准输出。

1.5 逻辑操作符

  • 符号列表
符号解释
&&前一个命令执行成功,再执行后面一个命令
||前一个命令执行失败,再执行后面一个命令
!在 Bash 中表示取反
在 vi/vim 中表示强制
“!ls” 表示找出最近一次以ls开头的命令并运行

既可以在Bash脚本中使用,也可以在Linux命令行中使用。

  • 符号实践举例
# &&的实践
# 命令分隔符,前面的命令必须执行成功才可以执行&&后面的命令
# 执行正确的命令
[root@localhost ~]# mkdir boy && cd /tmp 
[root@localhost tmp]# pwd
/tmp
# 命令执行错误
[root@localhost ~]# ping -W1 -c1 www.baiduuuuuu.com && cd /tmp
ping: www.baiduuuuuu.com: Name or service not known
[root@localhost ~]# ping -W1 -c1 www.baiduuuuuu.com
ping: www.baiduuuuuu.com: Name or service not known
[root@localhost ~]# echo $?
2

# ||的实践
# 前面的必须必须执行失败,才执行||后面的命令
# 执行成功的命令
[root@localhost ~]# mkdir boy || cd boy
[root@localhost ~]# ll
total 0
drwxr-xr-x 2 root root 6 Nov 14 09:29 boy

# 执行错误的命令
[root@localhost ~]# cd boy || mkdir boy
-bash: cd: boy: No such file or directory
[root@localhost ~]# ll
total 0
drwxr-xr-x 2 root root 6 Nov 14 09:31 boy

1.6 其他特殊符号

  • 符号列表
符号说明
;表示一个命令的结束,也是命令间的分隔符。在有些配置文件中,以分号开头的内容代表注释。
#表示是注释内容,是用来给管理员看的,系统不会执行以“#”号开头的内容;也是 root 用户的命令提示符
|表示管道,将一个命令处理后的中间内容输出给下一个命令继续处理
$字符串前加“$”符号,代表字符串变量内容;也代表普通用户的命令提示符
\将含有特殊含义的字符(通配符或正则表达式)还原成字符本意
{}生成序列;引用变量作为变量与普通字符的分隔
  • 符号实践举例
# 分号(;)的实践
# 在Linux命令行中,分号(;)表示一个命令的结束,它也是命令间的分隔符。在有些配置文件中,以分号开头的内容代表注释,例如在keepalived.conf高可用配置中就是如此。
[root@localhost test]# pwd;     #<==结尾加了分号。
/root/test
[root@localhost test]# pwd;pwd  #<==分隔两个命令。
/root/test
/root/test
# 命令执行错误不影响后续命令执行
[root@localhost boy]# llll ;mkdi test ;echo hehe
-bash: llll: command not found
-bash: mkdi: command not found
hehe

# 井号(#)的实践
# 在Linux系统的配置文件中,经常会看到以“#”号开头的内容,表示后面的是注释内容,这部分内容是给管理员看的,系统不会执行以“#”号开头的内容。
[root@localhost test]# tail /etc/ssh/sshd_config 
# Banner none
# override default of no subsystems
Subsystem sftp  /usr/libexec/openssh/sftp-server
# Example of overriding settings on a per-user basis
#Match User anoncvs
#    X11Forwarding no
#    AllowTcpForwarding no
#    ForceCommand cvs server

# 管道符(|)的实践
# 在Linux系统命令行中,管道符(|)是极其重要的符号,是命令行中十分常见的符号。
# Linux中管道的作用与现实环境中管道作用是类似的,只不过Linux管道里传输的是数据流,而不是水流或气流,在实际命令行中,一个命令处理后并未得到想要的结果为止,此时需要将数据交给其他命令继续处理,直到得到最终结果为止,在命令和命令之间就需要用管道衔接将数据流发过来进行处理。
[root@localhost test]# ifconfig|grep 10.0.0         #<==将ifconfig的输出结果输出给grep进行过滤。
    inet addr:10.0.0.7  Bcast:10.0.0.255  Mask:255.255.255.0
[root@localhost test]# cat /etc/services|grep 3306  #<==读取服务文件输出给grep过滤。
mysql           3306/tcp                        # MySQL
mysql           3306/udp                        # MySQL

# 美元符($)的实践
# 在Linux系统命令行或Shell脚本程序中,在字符串前加“$”符号,代表的是字符串变量的内容。
[root@localhost ~]# echo $PWD
/root/test
[root@localhost ~]# echo $PS1
[\u@\h \W]\$
[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root@localhost ~]# boy=1
[root@localhost ~]# echo $boy
1

# 大括号({})的实践
# “{}”表示生成序列(一连串的文本)
[root@localhost test]# echo {1..10} #<==两个点号表示从1到10,默认以空格分隔。
1 2 3 4 5 6 7 8 9 10
[root@localhost test]# echo {a..g} #<==两个点号表示从a到g的所有字符,默认以空格分隔。
a b c d e f g
[root@localhost test]# echo {o,l,d,b,o,y}#<==用逗号分开,列出来。
o l d b o y

# 利用“{}”功能来快速备份关键文件(特殊应用)
[root@localhost test]# cp /etc/ssh/sshd_config{,.ori} 
#<==等价/etc/ssh/sshd_config /etc/ssh/sshd_config.ori。
[root@localhost test]# ls -l /etc/ssh/sshd_config{,.ori}
-rw-------. 1 root root 3879 Mar 22  2017 /etc/ssh/sshd_config
-rw-------. 1 root root 3879 Mar 31 22:23 /etc/ssh/sshd_config.ori

# 将变量括起来作为变量的分隔符
[root@localhost test]# week=6               #<==定义变量week并赋值6。
[root@localhost test]# echo $week           #<==输出变量值。
6
[root@localhost test]# echo "$week_boy"   #<==输出为空,系统将week_boy作为变量了。
[root@localhost test]# echo "${week}_boy" #<==将变量week用大括号括起来再输出就对了。
6_boy

2. find命令

2.1 命令详解

【功能说明】

find 命令用于在指定目录下查找文件和目录,可以使用不同的选项来过滤和限制查找的结果。

【语法格式】

find [pathname] [expression]
find [pathname] [Option] [Tests] [Actions]  
find  查找路径    参数     条件    动作         

# 查找路径
相对路径: * ./* .
绝对路径: /* /var/log/
注意: 在企业中在对外提供服务器上查找使用相对路径或者针对某个目录查找,禁止直接从/开始查找

# 查找方向
find /opt/ 按文件类型-type查找
find /opt/ 按照名称-name查找
find /opt/ 按照inode查找
find /opt/ 按照深度等级-maxdepth查找,默认递归查找
find /opt/ 按照文件的大小-size
find /opt/ 修改时间-mtime

【选项说明】

参数选项解释说明
pathname命令所查找的目录文件。
Options模块
-depth从指定目录下最深层的子目录开始查找
-maxdepth levels查找的最大目录级数
-regextype type改变正则表达式的模式,默认是emacs。
Tests模块
-mtime/-mmin按照文件的修改时间来查找文件。单位是天/分钟
-atime/-amin按照文件的状态改变时间来查找文件。单位是天/分钟
-ctime/-cmin按照文件的访问时间来查找文件。单位是天/分钟
-group按照文件所属的组来查找文件
-user按照文件的属主来查找文件
-name按照文件名查找文件,支持*[]等一些特殊通配符
-size按照文件大小进行查找文件。-size [+-]n[cwbkMG]支持使用 +- 表示大于或小于指定大小;单位可以是 c(字节)、w(字数)、b(块数)、k(KB)、M(MB)或 G(GB)
-newer查找更改时间比指定文件新的文件
-nogroup/-nouser查找没有有效用户组/属主的文件。
-path pattern指定路径样式,配合-prune参数排除指定目录
-perm按照文件权限来查找文件
-regex接正则表达式
-iregex接正则表达式,不区分大小写
-type按照文件类型来查找文件b -- 块设备文件 c -- 字符设备文件 d -- 目录 p -- 管道文件 l -- 符号链接文件 f -- 普通文件 s -- socket文件
Actions 模块
-delete将查找出的文件删除
-exec对匹配的文件执行该参数所得出的shell命令
-prune使用这一选项可以使find命令不在当前指定的目录中查找
-print将匹配的文件输出到标准输出
取反
-a取交集
-o取并集

2.2 使用范例

按照文件类型查找

# 环境:
-rw-r--r-- 1 root root 0 Nov 13 10:15 1.txt
-rw-r--r-- 1 root root 0 Nov 13 10:15 2.txt
-rw-r--r-- 1 root root 0 Nov 13 10:15 3.txt
-rw-r--r-- 1 root root 0 Nov 13 10:17 4.txt
-rw-r--r-- 1 root root 0 Nov 13 10:17 5.txt
-rw-r--r-- 1 root root 0 Nov 13 10:17 6.txt
-rw-r--r-- 1 root root 0 Nov 13 10:17 7.txt
drwxr-xr-x 2 root root 6 Nov 13 10:45 test1
drwxr-xr-x 2 root root 6 Nov 13 10:45 test2
drwxr-xr-x 2 root root 6 Nov 13 10:45 test3

# 查找所有的普通文件
[root@localhost test]# find ./ -type f
./1.txt
./2.txt
./3.txt
./4.txt
./5.txt
./6.txt
./7.txt

[root@localhost test]# find ./ -type d
./
./test1
./test2
./test3

# 绝对路径查找:
[root@localhost ~]# find /root/test -type d
/root/test
/root/test/test1
/root/test/test2
/root/test/test3

# 查找字节文件
[root@localhost ~]# find /dev/ -type c|grep null
/dev/null
[root@localhost ~]# find /dev/ -type c|grep zero
/dev/zero

按照文件的名称查找

# 查找test目录下3.txt文件
[root@localhost test]# find test/ -name "3.txt"
test/3.txt

# 查找所有.txt结尾的文件
[root@localhost test]# find test/ -name "*.txt"
test/1.txt
test/2.txt
test/3.txt
test/4.txt
test/5.txt
test/6.txt
test/7.txt

# 不区分大小写查找 -iname
[root@localhost test]# find test/ -name "*.TXT"
test/1.TXT
test/2.TXT
test/3.TXT
[root@localhost test]# find test/ -iname "*.txt"
test/1.txt
test/2.txt
test/3.txt
test/4.txt
test/5.txt
test/6.txt
test/7.txt
test/1.TXT
test/2.TXT
test/3.TXT

按照深度等级查找文件

[root@localhost test]# find ./ -maxdepth 1 -type f
./1.txt
./2.txt
./3.txt

按照inode查找文件

在无法删除文件的时候使用inode查找辅助删除。

[root@localhost test]# ll -i
total 0
33574997 -rw-r--r-- 1 root root  0 Nov 13 10:15 1.txt
33575033 -rw-r--r-- 1 root root  0 Nov 13 10:15 2.txt
33598404 -rw-r--r-- 1 root root  0 Nov 13 10:15 3.txt

[root@localhost test]# find ./ -inum 33574997
./1.txt

按照文件的大小查找

[root@localhost test]# dd if=/dev/zero of=3.txt bs=1M count=100
100+0 records in
100+0 records out
104857600 bytes (105 MB) copied, 0.553434 s, 189 MB/s

[root@localhost test]# ll -h
total 112M
-rw-r--r-- 1 root root 1.3M Nov 13 10:58 1.txt
-rw-r--r-- 1 root root  11M Nov 13 10:59 2.txt
-rw-r--r-- 1 root root 100M Nov 13 10:59 3.txt
drwxr-xr-x 2 root root   58 Nov 13 10:54 test1
drwxr-xr-x 2 root root    6 Nov 13 10:45 test2
drwxr-xr-x 2 root root    6 Nov 13 10:45 test3

# 查找文件大于1M的
[root@localhost test]# find ./ -size +1M
./1.txt
./2.txt
./3.txt

# 查找文件等于100M的文件
[root@localhost boy]# find ./ -size 100M
./3.txt

# 查找文件小于100M的文件
[root@localhost boy]# find ./ -size -100M
./
./1.txt
./2.txt
./test1
./test1/4.txt
./test1/5.txt
./test1/6.txt
./test1/7.txt
./test2
./test3

# 查找文件大于1M并且小于100M
[root@localhost test]# find ./ -size +1M -size -100M
./1.txt
./2.txt
# -a表示并且关系,同时成立,默认find就是并且关系
[root@localhost test]# find ./ -size +1M -a -size -100M
./1.txt
./2.txt

# -o or 表示或者关系,成立一个即可
# 查找文件大于10M或者等于1M的 # 不支持小数
[root@localhost test]# find ./ -size +10M -o -size 1M

# 注意: find 使用!取反
[root@localhost test]# find ./ ! -type f
./
./test1
./test2
./test3

按照修改时间查找

atime ——访问时间

mtime ——修改时间 常用

ctime ——改变时间

-mtime -4 # 修改时间在4天内的,即最近4天内的

-mtime 4 # 修改时间在之前第4天的,当天

-mtime +4 # 修改时间在4天之前的

-mtime 0 # 修改时间在一天内的,当前时间到24小时前的时间段内
# 查找修改时间为10天前的
[root@localhost test]# find ./ -mtime +10
./
./1.log
./2.log
./3.log

# 查找修改时间2天内的
[root@localhost test]# find ./ -mtime -2
./1.txt
./2.txt
./3.txt

# 什么情况下按修改时间查找:
1.文件删除
2.系统文件被攻击

# 查找文件修改时间大于7天前 或者查找大于30天前
find /data/ -mtime +7
find /data/ -mtime +30
  • 小结:
find ./ -name             # 按照名称查找
find ./ -iname            # 按照名称查找,不区分大小写
find ./ -name "*.txt"     # 表示所有.txt 结尾
find ./ -name "boy*"   # 表示所有boy开头的文件
find ./ -type f           # 按照类型查找,类型: f  d b c l
find ./ -inum inode号码    # 按照inode号码查找
find ./ -size +10M        # 按照大小查找
		                  # 大小 10M 等于10M | +10M 大于10M | -10M 小于10M
find ./ -mtime +7         # 按照文件的修改时间查找
                          # 时间 +7 大于7天前 | -7 7天内的 | 7 第7天的
find ./ -maxdepth 1 -name # 按照深度等级查找

# 并且关系: -a and
find ./ -type f -name "*.txt"
find / -size +10M -size -1G
# 或者关系: -o or
find ./ -type f -o -size +10M

2.3 配合其他命令使用

使用xargs处理find的结果(推荐)

xargs 是一个强有力的命令,它能够捕获一个命令的输出,然后传递给另外一个命令。之所以用这个命令是因为很多命令不支持 |管道来传递参数,而日常工作中有需求。

  • find查找到的内容进行删除,交给rm处理
[root@localhost test]# find ./ -name "3.txt"|xargs rm

xargs后面的别名无效,即使命令是有别名引用也只会表达其最初的意思。

  • 将查找到内容交给ls查看
[root@localhost test]# find ./ -name "2.txt"|xargs ls -l
-rw-r--r-- 1 root root 0 Nov 13 11:58 ./2.txt
  • 查找到的内容进行复制 交给cp命令
# 比如查找所有的.txt文件复制到/opt目录
[root@localhost test]# ll
total 0
-rw-r--r-- 1 root root 0 Nov 13 12:03 10.txt
-rw-r--r-- 1 root root 0 Nov 13 11:58 1.txt
-rw-r--r-- 1 root root 0 Nov 13 11:58 2.txt
-rw-r--r-- 1 root root 0 Nov 13 12:01 45.txt
[root@localhost test]# rm -rf /opt/*

[root@localhost test]# find ./ -name "*.txt"|xargs -i cp {} /opt/
[root@localhost test]# ll /opt/
total 0
-rw-r--r-- 1 root root 0 Nov 13 12:17 10.txt
-rw-r--r-- 1 root root 0 Nov 13 12:17 1.txt
-rw-r--r-- 1 root root 0 Nov 13 12:17 2.txt
-rw-r--r-- 1 root root 0 Nov 13 12:17 45.txt
  • find命令结果交给mv移动
[root@localhost test]# find /opt/ -name "*.txt"|xargs -i mv {} /tmp/
[root@localhost test]# ll /opt/
total 0
[root@localhost test]# ll /tmp/
total 4
-rw-r--r-- 1 root root   0 Nov 13 12:17 10.txt
-rw-r--r-- 1 root root   0 Nov 13 12:17 1.txt
-rw-r--r-- 1 root root   0 Nov 13 12:17 2.txt
-rw-r--r-- 1 root root   0 Nov 13 12:17 45.txt

使用exec参数处理find的结果

-exec 参数对匹配的文件执行该参数所给出的shell命令。 形式为command {} \;,注意{}\;之间有空格

  • find查找到的内容进行删除 交给rm处理
[root@localhost test]# find ./ -name "1.txt" -exec rm {} \;
[root@localhost test]# ll
total 0
-rw-r--r-- 1 root root 0 Nov 13 12:03 10.txt
-rw-r--r-- 1 root root 0 Nov 13 11:58 2.txt
-rw-r--r-- 1 root root 0 Nov 13 12:01 45.txt
  • 查找到的内容进行复制 交给cp命令
[root@localhost test]# find ./ -name "*.txt" -exec cp {} /opt/ \;
[root@localhost test]# ll /opt/
total 0
-rw-r--r-- 1 root root 0 Nov 13 12:23 10.txt
-rw-r--r-- 1 root root 0 Nov 13 12:23 2.txt
-rw-r--r-- 1 root root 0 Nov 13 12:23 45.txt
  • 查找到的内容进行复制 交给mv命令
[root@localhost test]# ll /opt/
total 0
-rw-r--r-- 1 root root 0 Nov 13 12:23 10.txt
-rw-r--r-- 1 root root 0 Nov 13 12:23 2.txt
-rw-r--r-- 1 root root 0 Nov 13 12:23 45.txt
[root@localhost test]# find /opt/ -name "*.txt" -exec mv {} /tmp/ \;
[root@localhost test]# ll /opt/
total 0
[root@localhost test]# ll /tmp/
total 0
-rw-r--r-- 1 root root 0 Nov 13 12:23 10.txt
-rw-r--r-- 1 root root 0 Nov 13 12:23 2.txt
-rw-r--r-- 1 root root 0 Nov 13 12:23 45.txt

直接使用反引号包裹或使用$()包裹

这里只是简单使用rm删除命令举例

[root@localhost test]# rm -rf `find ./ -name "2.txt"`
[root@localhost test]# ll
total 0
-rw-r--r-- 1 root root 0 Nov 13 12:03 10.txt
-rw-r--r-- 1 root root 0 Nov 13 12:01 45.txt

[root@localhost test]# rm -rf $(find ./ -name "2.txt")
[root@localhost test]# ll
total 0
-rw-r--r-- 1 root root 0 Nov 13 12:03 10.txt
-rw-r--r-- 1 root root 0 Nov 13 12:01 45.txt
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值