shell之find命令

本文详细介绍了Linux下的find命令及其各种选项的使用方法,并通过实例演示了如何查找特定条件的文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

find命令格式: find pathname -options [-print -exec -ok]

-print: find命令将匹配的文件输出到标准输出。
-exec: find命令对匹配的文件执行该参数所给出的shell命令。相应命令的形式为'command' { } \;,注意{ }和\;之间的空格。
-ok: 和-exec的作用相同,只不过以一种更为安全的模式来执行该参数所给出的shell命令,在执行每一个命令之前,都会给出提示,让用

户来确定是否执行。
xargs:

-option:

-name  filename           #查找名为filename的文件
-perm                     #按执行权限来查找
-user  username           #按文件属主来查找
-group groupname          #按组来查找
-mtime -n +n              #按文件更改时间来查找文件,-n指n天以内,+n指n天以前
-atime -n +n              #按文件访问时间来查找文件,-n指n天以内,+n指n天以前
-ctime -n +n              #按文件创建时间来查找文件,-n指n天以内,+n指n天以前
-amin  -n +n              #按文件访问时间来查找文件,-n指n分钟以内,+n指n分钟以前
-mmin  -n +n              #按文件修改时间来查找文件,-n指n分钟以内,+n指n分钟以前
-cmin  -n +n              #按文件创建时间来查找文件,-n指n分钟以内,+n指n分钟以前
-nogroup                  #查无有效属组的文件,即文件的属组在/etc/groups中不存在
-nouser                   #查无有效属主的文件,即文件的属主在/etc/passwd中不存
-newer   f1 !f2           #查更改时间比f1新但比f2旧的文件
-type    b/d/c/p/l/f      #查是块设备、目录、字符设备、管道、符号链接、普通文件
-size      n[c]           #查长度为n块[或n字节]的文件
-depth                    #使查找在进入子目录前先行查找完本目录
-fstype                   #查位于某一类型文件系统中的文件,这些文件系统类型通常可 在/etc/fstab中找到
-mount                    #查文件时不跨越文件系统mount点
-follow                   #如果遇到符号链接文件,就跟踪链接所指的文件
-cpio                     #对匹配的文件使用cpio命令,将他们备份到磁带设备中
-prune                    #忽略某个目录
-empty                    #查找在系统中为空的文件或者文件夹
-links                    #查找硬链接数的文件或者目录
-uid                      #查找uid为多少的文件或者目录
-gid                      #查找gid为多少的文件或者目录

应用举例:

find /root/ -name ran* -exec cp '{}' /tmp ';' #将以ran开头的文件或目录拷贝到/tmp目录下

find /tmp/ -path "/tmp/test2" -prune -o -print #要在/tmp目录下查找不在test2子目录之内的所有文件

find /etc/ -name passwd* -type f -exec grep oracle {} \;

find /tmp/ -user oracle

find /tmp/ -perm  755 -type d |xargs ls -ld #严格权限匹配

find /tmp/ -perm  -755 -type d |xargs ls -ld #匹配权限位为1的
drwxrwxrwt. 21 root root 4096 Apr  2 11:40 /tmp/
drwxrwxrwt   2 root root 4096 Oct 31 21:36 /tmp/.ICE-unix
drwxr-xr-x   2 root root 4096 Apr  2 10:44 /tmp/test2

find /tmp/ -perm  +755 -type d |xargs ls -ld #任意一个1的权限为被匹配
drwx------   2 oracle oinstall 4096 Sep  2  2013 /tmp/.esd-500
drwxrwxrwt   2 root   root     4096 Oct 31 21:36 /tmp/.ICE-unix
drwxr-xr-x   3 root   root     4096 Jul 15  2012 /tmp/vmware-tools-distrib/lib

find /var/log -mtime -2 -type f -exec rm {} \;

find /var/log -mtime -2 -type f -ok rm {} \;

find /u01/  -type f -size 5G -exec ls -l {} \;

find /u01/  -type f -size -5G -exec ls -l {} \;

find /u01/  -type f -size +5G -exec ls -l {} \;

find /tmp/ -name ranyuan* -type f -exec chmod o+w {} \;

find /tmp/ -name ranyuan* -type f | xargs chmod o-w ;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值