本文共 2405 字,大约阅读时间需要 8 分钟。
find命令是Linux系统中常用的一款强大文件查找工具,其命令格式如下:
find pathname -options [ -print | -exec | -ok ... ]
find命令的参数说明:
.
表示当前目录,/
表示系统根目录。command { } /;
,注意空格和分号的使用。-exec
类似,但更安全,执行前会提示用户确认。常用选项:
-name "*.txt"
匹配所有文本文件。-perm 755
匹配权限为755的文件。-nouser
可查找无效属主文件。-nogroup
可查找无效组文件。-5
(最近5天)或+5
(5天以前)。-size Nc
或-size N
,c
表示以字节计。-type d
匹配目录,-type f
匹配文件。-prune
可排除指定目录。1. 查找当前用户主目录下的所有文件:
$ find $HOME -print
2. 查找特定权限的文件并列出详细信息:
$ find . -perm 644 -exec ls -l { } /
3. 查找系统中所有长度为0的普通文件:
$ find / -type f -size 0 -exec ls -l { } /
4. 在/var/logs
目录中查找更改时间在7日以前的文件并删除:
$ find /var/logs -type f -mtime +7 -ok rm { } /
5. 查找属于root
组的文件:
$ find . -group root -exec ls -l { } /
6. 查找名为admin.log*
且更改时间在7日以内的文件并删除:
$ find . -name "admin.log*" -atime -7 -ok rm { } /
7. 查找当前文件系统中的所有目录并排序:
$ find . -type d | sort
8. 查找系统中所有的rmt磁带设备:
$ find /dev/rmt -print
xargs命令是find命令与-exec
选项结合使用时的替代方案,主要用于处理find命令输出过长的问题。与-exec
相比,xargs命令更适合大文件量的处理。以下是xargs命令的常用示例:
1. 在当前目录下查找所有以.log
结尾的文件并列出详细信息:
$ find . -name "*.log" -print | xargs ls -l
2. 在根目录下查找核心文件并写入日志文件:
$ find / -name "core" -print | xargs echo "" > /tmp/core.log
3. 在当前目录下查找所有文件名以file*
结尾的文件并删除:
$ find . -name "file*" -print | xargs rm
4. 在当前目录下查找所有用户具有读、写和执行权限的文件并修改权限:
$ find . -perm -7 -print | xargs chmod o-w
5. 在当前目录下查找所有普通文件并搜索hostname
关键字:
$ find . -type f -print | xargs grep "hostname"
1. 忽略某个特定目录:
$ find /apps -path "/apps/bin" -prune -o -print
2. 避开多个文件夹:
$ find /usr/sam \[ -path /usr/sam/dir1 -o -path /usr/sam/file1 \] -prune -o -print
3. 查找无有效属主或所属组的文件:
$ find / -nouser -print$ find / -nogroup -print
4. 按更改时间或访问时间查找文件:
$ find / -mtime -5 -print$ find /var/adm -mtime +3 -print
5. 查找比某个文件新或旧的文件:
$ find -newer file1 ! file2$ find . -newer temp -print
6. 按文件类型查找目录或符号链接文件:
$ find /etc -type d -print$ find /etc -type l -print
7. 按文件大小查找文件:
$ find . -size +1000000c -print$ find /home/apache -size 100c -print$ find . -size +10 -print
8. 按深度限制查找文件:
$ find / -name "CON.FILE" -depth -print
9. 忽略文件系统的挂载点:
$ find . -mount -name "*.XC" -print
本文是zhyfly兄贴在LinuxSir.Org的一个帖子而整理出来的。如果您对版权有疑问,请在本帖后面跟帖。谢谢!
转载地址:http://ickfk.baihongyu.com/