佳豪哥哥教你学Linux的第十九天
原创
©著作权归作者所有:来自51CTO博客作者佳豪哥哥的原创作品,请联系作者获取转载授权,否则将追究法律责任
find文件查找
find基础语法
find [路径] [选项] [表达式] [动作]
find选项
按文件类型查找
-type:
f:可编辑的文件
d:目录
l:软链接文件
b:块设备文件 磁盘,U盘 /dev/sda
c:字符设备文件 终端
s:socket 安全套接字文件
p:管道文件
find [路径] [选项]
[root@wjh ~]
[root@wjh ~]
[root@wjh ~]
596
按文件大小查找
-size
-:小于
+:大于
Num:精准但是又不精准的匹配
[root@wjh ~]
mv 源文件 目标路径
mv -t 目标路径 源文件
[root@wjh ~]
xargs:
-i:指定数据流的位置,将数据流放入{}中
[root@wjh ~]
按文件名查找
-name:严格区分大小写
[root@wjh ~]
[root@wjh ~]
[root@wjh ~]
[root@wjh ~]
-iname:不区分大小写
[root@wjh ~]
按文件时间查找
-atime:文件访问时间差找
-mtime:文件内容创建,修改时间差找
-ctime:文件属性,修改时间差找
Num:查找第N天的文件(不包括今天)
+Num:查找第N天之前的所有文件(不包括今天)
-NUm:查找从今天开始算,7天内的文件
access time:atime
modify time:mtime
change time:ctime
[root@wjh <sub>]
Access: 2022-04-17 12:24:57.385996021 +0800
Modify: 2022-04-17 12:24:57.385996021 +0800
Change: 2022-04-17 12:24:57.385996021 +0800
for i in `seq -w 30`;do date -s 202204$i && touch file-$i;done
[root@wjh ~]
[root@wjh ~]
total 0
-rw-r--r-- 1 root root 0 Apr 24 00:00 file-24
-rw-r--r-- 1 root root 0 Apr 25 00:00 file-25
-rw-r--r-- 1 root root 0 Apr 26 00:00 file-26
-rw-r--r-- 1 root root 0 Apr 27 00:00 file-27
-rw-r--r-- 1 root root 0 Apr 28 00:00 file-28
-rw-r--r-- 1 root root 0 Apr 29 00:00 file-29
-rw-r--r-- 1 root root 0 Apr 30 00:00 file-30
按照文件用户和组查找
-user:查找文件的属主
-nouser:文件没有属主用户的
-group:查找文件的属组
-nogroup:文件没有属组的
多条件组合查找
[root@wjh ~]
/opt/
/opt/file-24
/opt/file-25
/opt/file-26
/opt/file-27
/opt/file-28
/opt/file-29
/opt/file-30
[root@wjh ~]
/opt/
/opt/dir-24
/opt/dir-25
/opt/dir-26
/opt/dir-27
/opt/dir-28
/opt/dir-29
/opt/dir-30
[root@wjh]
按权限查找
-perm
[root@wjh ~]
[root@wjh ~]
./.bash_logout
./.bash_profile
./.cshrc
./.tcshrc
./.bashrc.swp
./.bashrc
./wjh.txt
[root@wjh ~]
622
rw--w--w-
[root@wjh ~]
属主权限位,有一个r或者有一个w就满足条件
属组权限位,有一个r就满足条件
其他用户权限位,有一个r就满足条件
按深度查找
-maxdepth
针对目录层级查找
[root@wjh ~]
find动作
-print:打印查找到的内容到终端上(find命令默认就是打印结果 -print)
-ls:查看文件的详细信息 |xargs ls -l 或者 ls -l $(find xxx) 或者 ls -l `find xxx`
-delete:删除查找到的文件(bug跟ls,ls看不见的,也删除不掉)并且只能删除空目录
其他删除方法: |xargs rm -fr 或者 rm -fr $(find xxx) 或者 rm -fr `find xxx`
[root@wjh ~]
find: cannot delete ‘/run/initramfs/state/etc’: Directory not empty
find: cannot delete ‘/etc’: Directory not empty
find: cannot delete ‘/var/tmp/etc’: Directory not empty
-ok:找到文件后,执行后面的bash命令,询问是否要操作
语法:-ok 系统命令 {} \;
find / -type f -name 'file*' -ok mv {} /tmp \;
-exec:找到文件后,执行后面的bash命令
语法:-exec 系统命令 {} \;
find / -type f -name 'file*' -exec mv {} /tmp \;
find多条件
