Linux-----find的基础应用
find命令应用场景
Linux 系统的运行时间越久,安装的服务越多,时间越久,文件名和位置多少会忘记!!!
或者我们需要在不知道明确文件名的情况下,按照某些特定的需求去查找符合条件的文件。
find——基础语法
find [路径] [参数] [条件] [动作]
用法: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
find——关键参数
-maxdepth N #搜索目录最深处 N表示层级
#常用的限定条件
-name #根据文件名进行查找
-mtime N #按照文件修改时间进行查找
-size N[bkMG] #按照文件大小进行查找
-type #按照文件类型进行查找
-perm #按照文件权限进行查找
-user #按照文件所属用户进行查找
#常用的执行动作
-delete #将find查到的文件进行删除操作
-exec #将find查到的文件传递给后面的shell命令
-ok #在执行每个命令之前,都会让用户来选择是否确认执行
#常用的逻辑运算符
! #取反
-a #并且 and
-o #或 or
#按关键词查找文件
find /etc -name "passwd" find /etc -name "hostname"
find /etc -name "*.conf"
#通过 ! 排除不想查找的文件
find /etc -name "ifcfg-*" ! -name "ifcfg-lo"
按大小查找
#查找大于10M的文件
find /etc -size +10M
#查找小于10M的文件
find /etc -size -10M
#查找等于10M的文件
find /etx -size 10M
#查找范围大于80M小于100M
find / -size +80M -size -100M
#按照文件类型和文件名和文件大小查找
find / -typ f -size +9M -size -11M -name "*.tar.gz"
按类型查找
#查找类型为普通文件
find /dev -type f
#查找类型目录
find /dev -type d
#查找类型为软链接
find /dev -type l
按时间查找
#创建测试文件
mkdir find && cd find for i in {01..26};do date -s 202103$i && touch file-$i;done date -s "2021-03-26"
#查找7天以前的文件
find . -name "file-*" -mtime +7
#查找7天以内的文件
find . -name "file-*" -mtime -7
#查找第7天的文件
find . -name "file-*" -mtime 7
限制查找目录的层级
#创建测试文件
mkdir data1/1/2 -p touch data1/a.txt
touch data1/1/b.txt
touch data1/1/2/c.txt
tree data1/ --------------------
data1/
├── 1
│ ├── 2
│ │ └── c.txt
│ └── b.txt
└── a.txt
#查找不同目录层次的文件
[root@linux find]# find data1/ -maxdepth 1 -type f
data1/a.txt
[root@linux find]# find data1/ -maxdepth 2 -type f
data1/1/b.txt
data1/a.txt
[root@linux find]# find data1/ -maxdepth 3 -type f data1/1/2/c.txt
data1/1/b.txt
data1/a.txt
find查找后执行动作
-exec执行动作
#使用-exec按条件查找文件并显示文件详细信息
find /etc -name "ifcfg*" -exec ls -l {} \;
find /etc -name "ifcfg*" -ls;
#使用-exec按条件查找文件并删除
find . -type f -name "1.txt"
find . -type f -name "1.txt" -exec rm -rf {} \;
find . -type f -name "1.txt"
#使用-exec按条件查找文件并安全删除
find . -type f -name "2.txt" -ok rm -rf {} \;
xargs执行动作
#使用xargs查找文件并显示文件详细信息
find /etc -name "ifcfg*"|xargs ls -l;
#使用xargs执行mv命令
find . -type f -name "*.txt"|xargs -i mv {} /opt/
find . -type f -name "*.txt"|xargs mv -t /opt/
#使用xargs将查找到的文件打包压缩
find . -type f -name "3.txt"|xargs tar zcf 3.tar.gz
tar zcf 3.tar.gz $(find . -type f -name "3.txt")
#删除目录下所有文件,但保留一个指定文件
touch file{1..10}.txt find . -type f ! -name "file10.txt"|xargs rm -f
find . -type f ! -name "file10.txt" -exec rm -f {} \;
#找出7天以前的日志并删除
find . -type f -mtime +7|xargs rm -rf
rm -rf $(find . -type f -mtime +7)
exec和xargs区别
exex是将查找的结果文件逐个传递给后面的命令来执行,如果文件多时则执行的效率低。
xargs是将查找的结果一次性传输给后面的命令执行,执行效率更高。
exec执行演示:
[root@linux find]# touch {1..5}.txt
[root@linux find]# find . -type f ./1.txt
./2.txt
./3.txt
./4.txt
./5.txt
#可以发现exec每个结果都被加上了输出内容
[root@linux find]# find . -type f -exec echo "cnm" {} \;
cnm ./1.txt
cnm ./2.txt
cnm ./3.txt
cnm ./4.txt
cnm ./5.txt
xargs执行演示:
#可以发现xargs只会输出一次cnm
[root@linux find]# find . -type f |xargs echo cnm
cnm./1.txt ./2.txt ./3.txt ./4.txt ./5.txt ./6.txt
./7.txt ./8.txt ./9.txt ./10.txt
find逻辑运算符
#查找目录层级为1L的目录
find . -maxdepth 1 -type d
#查找目录层级为1L的目录,但是排除 .
find . -maxdepth 1 -type d ! -name "."
#查找目录层级为1L的目录,但是排除'.'或oldboy
find . -maxdepth 1 -type d ! -name "." -o -name "oldboy"
#查找目录层级为1L的目录,名字不为'.'并且名字为ext的目录
find . -maxdepth 1 -type d ! -name "." -a -name "ext
xargs将标准输出换成命令行参数
1.xargs命令介绍
xargs命令是向其他命令传递命令行参数的一个过滤器,它能够将管道或者标准输入传递的数据转换成 xargs命令后所跟命令的命令行参数。
2.xargs应用
结合管道和find对文件进行处理 控制每行输出的数量
控制每行输出的数量
3.xargs关键参数
-n #指定每行大参数量是n,可以将标准输入的文本划分为多行,每行显示n个
-d #自定义分隔符
-i #以{}替代前面的结果
-I #以其他符号替代前面的结果