Linux的find命令:文件及目录查找

find:查找目录下的文件

有些时候,我们可能会忘了某个文件所在的位置,此时就需要通过find来查找。还有些时候,我想要找到,某个目录下,所有小于1k的文件。……还还还有些时候,我们想找到,某个目录下,所有以.sh结尾的脚本。

Linux中的find命令用于查找目录下的文件。

# find的语法(注意模块先后顺序)
命令    需查找的路径    参数模块        限定条件        动作     (后面这三个合一块叫表达式)
find    [path...]     [options]       [tests]       [action]
​
find     /etc/       -name "*conf*" -a -name "*config*" 
​
# [path...]:目录路径
# [options]模块   
-depth   //从指定目录下最深层的子目录开始查找
-maxdepth levels  #//查找的最大目录级数,levels 为自然数 
-regextype type   //改变正则表达式的模式。默认为 emacs,还有posix-awk、posix-basic等
# [tests]模块
-mtime [-n|n|+n] #按照文件的修改时间来在找文件(这个参数最常用),具体说明如下。
-n 表示文件更改时间距现在n天以内
+n 表示文件更改时间距现在n天以前
n是距现在第n天
-atime [-n|n|+n] 按照文件的访问时间来查找文件,单位是天
-ctime [-n|n|+n] 按照文件的状态改变时问来查找文件,单位是天
-amin 按照文件的访问时间来查找文件,单位是分钟
-cmin 按照文件的状态改变时间来查找文件,单位是分钟
-mmin 按照文件的修改时间来查找文件,单位是分钟
-group 按照文件所属的组来查找文件
-name #按照文件名查找文件,只支持* ? []等特殊通配符 
-newer 查找更改时间比指定文件新的文件
-nogroup 查找没有有效用户组的文件,即该文件所属的组在/ete/groups 中不存在
-nouser 查找没有有效属主的文件,即该文件的属主在 /etc/passwd 中不存在
-path pattern 指定路径样式,配合-prune参数排除指定目录
-perm 按照文件权限来查找文件
-regex 接正则表达式
-iregex 接正则表达式,不区分大小写
-size n[cwbkMG] 可以按照文件大小来查找文件(块或字节)
-user 按照文件属主来查找文件
-type # 查找某一类型的文件,具体说明如下
b (块设备文件)
c(字符设备文件)
d(目录)
p(管道文件)
l(符号链接文件)
f(普通文件)
s (socket 文件)
D (door)
​
# [action]模块:当查找到一个文件后,需要对文件进行如何处理。默认动作-print
-print     将匹配的文件输出到标准输出(默认功能,使用中可省略)
-delete    删除查找到的文件(仅能删除空目录)
-exec      #对匹配的文件执行该参数所给出的 shell 命令(标准写法-exec \;) 
-ok        与-exec的作用相同,但在执行每个命令之前,都会让用户来确定是否执行
-prune     使用这一选项可以使find命令不在当前指定的目录中查找
​
# find支持如下逻辑运算符(常用)
!  取反添
-a 取交集,全拼为and
-o 取并集,全拼为or

[options]模块示例

# [options]模块例子
-maxdepth levels  //查找的最大目录级数,levels 为自然数 #常用
[root@db04 ~]# find /home/ -maxdepth 1 -type d  -user zls 
​
​

[tests]模块示例

# 按文件名查找,只支持* ? []等特殊通配符
-name
-iname    忽略大小写
​
如
[root@localhost ~]# touch /etc/sysconfig/network-scripts/{ifcfg-eth1,IFCFG-ETH1}  //创建文件
​
[root@localhost ~]# find /etc/ -name "ifcfg-eth1"     //查找/etc目录下包含ifcfg-eth0名称的文件       
/etc/sysconfig/network-scripts/ifcfg-eth1
​
[root@localhost ~]# find /etc/ -iname "ifcfg-eth1"      //-i 忽略大小写
/etc/sysconfig/network-scripts/ifcfg-eth1
/etc/sysconfig/network-scripts/IFCFG-ETH1
​
[root@localhost ~]# find /etc/ -iname "ifcfg-eth*"      // *查找所有名字以ifcfg-eth开头的文件
/etc/sysconfig/network-scripts/ifcfg-eth0
/etc/sysconfig/network-scripts/ifcfg-eth1
/etc/sysconfig/network-scripts/IFCFG-ETH1
​
[root@localhost ~]# find / -name "xxx*"
/root/xxx
/root/xxx1
/var/spool/mail/xxx
/tmp/xxx1
/home/xxx
[root@localhost ~]# find / -name *"xxx*"                // 两个* 查找/下所有名字包含xxx的文件
/root/xxx
/root/xxx1
/var/spool/mail/xxx
/tmp/xxx1
/usr/lib/modules/3.10.0-957.el7.x86_64/kernel/drivers/crypto/qat/qat_c3xxx
/usr/lib/modules/3.10.0-957.el7.x86_64/kernel/drivers/crypto/qat/qat_c3xxx/qat_c3xxx.ko.xz
/usr/lib/modules/3.10.0-957.el7.x86_64/kernel/drivers/crypto/qat/qat_c3xxxvf
/usr/lib/modules/3.10.0-957.el7.x86_64/kernel/drivers/crypto/qat/qat_c3xxxvf/qat_c3xxxvf.ko.xz
......
​
​

# 按照文件类型找+逻辑运算符应用
-type 查找某一类型的文件,具体说明如下
b (块设备文件)
c(字符设备文件)
d(目录)
p(管道文件)
l(符号链接文件)
f(普通文件)
s (socket 文件)
D (door)
​
如:
[root@localhost ~]# find / -type d -name 'conf'      查找根下名字conf的目录
/proc/sys/net/ipv4/conf
/proc/sys/net/ipv6/conf
[root@localhost ~]# find / -type f -name '*conf'  查找根下名字带conf的文件
一大堆
​
[root@localhost ~]# find /etc/ -type d ! -name "conf"      /etc/下名字不带conf的目录 出来一大堆
......
​
​
[root@localhost ~]# find /etc/ -name "*conf*" -a -name "*config*"     /etc下 名字有这两种的
/etc/chkconfig.d
/etc/systemd/system/multi-user.target.wants/rhel-configure.service
/etc/pki/nss-legacy/nss-rhel7.config
/etc/pm/config.d
......
​
[root@localhost ~]# find /etc/ -name "*conf*" -o -name "*config*"   比上面出来多得多
/etc/resolv.conf
/etc/chkconfig.d
/etc/libaudit.conf
/etc/security/pwquality.conf
/etc/security/access.conf
/etc/security/chroot.conf
......
​

# 按时间查找,常结合动作模块使用
-mtime [-n|n|+n] #按照文件的修改时间来在找文件(这个参数最常用),具体说明如下。
-n 查找最近n天的文件,包含当天的文件,不建议使用
+n 查找n天以前的文件(不包含今天,不包含第n天)
n  查找第n天的文件
​
如:
# find /opt -mtime -1   
​

动作模块示例

# [action]模块:当查找到一个文件后,需要对文件进行如何处理。默认动作-print
-print     将匹配的文件输出到标准输出(默认功能,使用中可省略)
-delete    删除查找到的文件(仅能删除空目录)
-exec    # 对匹配的文件执行该参数所给出的 shell 命令(标准写法-exec 命令 {} \;) 
-ok        与-exec的作用相同,但在执行每个命令之前,都会提示
-prune     使用这一选项可以使find命令不在当前指定的目录中查找
​
## 例1:匹配当前目录下的所有普通文件,并在-exec选项中使用ls -l命令将它们列出
[root@localhost ~]# find ./ -type f 
./.bash_logout
./.bash_profile
./.bashrc
./.cshrc
./.tcshrc
./anaconda-ks.cfg
./.bash_history
./.lesshst
./.viminfo
./1.txt
./2.txt
​
[root@localhost ~]# find ./ -type f -exec ls  {} \;
./.bash_logout
./.bash_profile
./.bashrc
./.cshrc
./.tcshrc
./anaconda-ks.cfg
./.bash_history
./.lesshst
./.viminfo
./1.txt
./2.txt
[root@localhost ~]# find . -type f -exec ls -l {} \;    
-rw-r--r--. 1 root root 18 Dec 29  2013 ./.bash_logout
-rw-r--r--. 1 root root 176 Dec 29  2013 ./.bash_profile
-rw-r--r--. 1 root root 176 Dec 29  2013 ./.bashrc
-rw-r--r--. 1 root root 100 Dec 29  2013 ./.cshrc
-rw-r--r--. 1 root root 129 Dec 29  2013 ./.tcshrc
-rw-------. 1 root root 1310 Jul 14 17:14 ./anaconda-ks.cfg
-rw-------. 1 root root 4001 Jul 21 13:42 ./.bash_history
-rw-------. 1 root root 43 Jul 15 21:23 ./.lesshst
-rw------- 1 root root 4394 Jul 20 22:50 ./.viminfo
-rw-r--r-- 1 root root 4 Jul 21 13:43 ./1.txt
-rw-r--r-- 1 root root 4 Jul 21 13:47 ./2.txt
​
# 说明:
-exec后面跟的是shell命令,最后以分号(;)作为结束标志。考虑到各个系统中分号会有不同的意义,所以前面要加反斜杠对分号进行转义。
{}:指代前面find命令查找到的文件或目录。{}前后都要有空格。
命令可以是其他任何命令,例如,示例代码中的 ls、rm 等命令
​
​
## 例2:匹配当前目录下更改时间7天前的文件,并删除
[root@localhost ~]# find . -type f -mtime +7 -exec rm {} \; 等同于下面命令
[root@localhost ~]# find . -type f -mtime +7 |xargs rm  
​
## 例3:find结合xargs使用
[root@localhost ~]# find . -type f -exec ls -l {} \;    等同于下面命令
[root@localhost ~]# find . -type f |xargs ls -l   
​
​

其他不常用的

# 按文件大小
​
find 路径 -size ±/number
+      大于
-      小于
number 等于
​
[root@localhost ~]# find /root -size 1k      查找/root下小于1k的文件
/root
/root/.bash_logout
/root/.bash_profile
...
​
#看大小
[root@localhost ~]# du -sh /root/.bash_logout    由于1k占用1个block,出来的是4k。(计算机最小单位1个block是4k)
4.0K    /root/.bash_logout                    
​
​
[root@localhost ~]# find /etc -size +5M     查找/etc下大于5M的文件
/etc/udev/hwdb.bin
​
[root@localhost ~]# ll -h  /etc/udev/hwdb.bin     看一下大小
-r--r--r--. 1 root root 7.6M Aug 17 15:33 /etc/udev/hwdb.bin
​

# 按文件的用户查找
​
find 路径 选项 xxx
-user
-group
-nouser
-nogroup
​
[root@localhost ~]# find /home -user xxx
/home/xxx
/home/xxx/.bash_logout
/home/xxx/.bash_profile
/home/xxx/.bashrc
/home/xxx/.bash_history
/home/xxx/test1
[root@localhost ~]# cd /home
[root@localhost home]# ll
total 0
drwx------. 2 xxx xxx      96 Aug 25 12:14 xxx
drwx------. 2 zls students 62 Aug 18 11:34 zls
[root@localhost home]# find /home -user xxx -group xxx
/home/xxx
/home/xxx/.bash_logout
/home/xxx/.bash_profile
/home/xxx/.bashrc
/home/xxx/.bash_history
/home/xxx/test1
[root@localhost home]# find -user xxx -group xxx
./xxx
./xxx/.bash_logout
./xxx/.bash_profile
./xxx/.bashrc
./xxx/.bash_history
./xxx/test1

# 按文件的权限查找
find 路径 -perm (权限值)

精确匹配
find / -perm 022

模糊匹配 包含-后面的对应权限
find / -perm -022

find . -perm 644 -ls

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值