Linux基础命令-find搜索文件位置

文章目录

find

命令介绍

语法格式

命令基本参数 

参考实例

1)在root/data目录下搜索*.txt的文件名

2)搜索一天以内最后修改时间的文件;并将文件删除

3)搜索777权限的文件

4)搜索一天之前变动的文件复制到test目录下

5)搜索指定用户的文件

6)在/etc目录下搜索大于5M,小于10M的文件

7)搜索指定组的目录

8)在/var/log目录搜索指定后缀的文件 

 9)在/var/log目录搜索指定后缀不是.log的文件

10)从根目录下搜索可执行的文件并复制到目录下

命令总结

find

命令介绍

        通过帮助文档了解含义

NAME
       find - search for files in a directory hierarchy

        find命令的作用是在目录层次结构中搜索文件所在的位置,此命令可以使用的参数很多,同时支持正则表达式,结合管道符后能够实现更加复杂的功能,是必须掌握的命令之一。

        通常find是从根目录开始全盘搜索,不同于其他几个搜索文件的命令,find搜索时会消耗较多的系统资源,在服务器负载较高的时候,不建议从根目录开始搜索。

语法格式

        find参数是比较多的,而且很多参数都是长格式,要记住真的要花一番心思,下面来看下语法格式:find 【路径】【参数】

SYNOPSIS
       find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]

命令基本参数 

此命令的常用参数有以下这些,以表格形式显示

-name匹配文件的名称
-user匹配用户的文件(所有者)
-group匹配组的文件(所有组)
-mtime -n +n匹配修改内容的时间,-n表示n天之内,+n表示n天之前
-atime -n  +n匹配访问文件的时间,-n表示n天之内,+n表示n天之前
-ctime -n +n匹配改动文件的时间,-n表示n天之内,+n表示n天之前
-perm匹配文件权限
-size匹配文件的大小,单位k M,+nk表示查找大于n的文件,-nk表示查找小于n的文件
-exec { } \;后面可跟用于进一步处理搜索结果的命令
-prune忽略某个目录
-nouser匹配不是这个用户的文件
-nogroup匹配不是这个组的文件

-type

匹配文件类型(b  d c p f l)

-type参数的文件类型

  • b:块设备文件
  • d:目录文件
  • c:字符设备文件
  • p:管道文件
  • f :文本文件
  • l :链接文件

参考实例

1)在root/data目录下搜索*.txt的文件名

[root@localhost ~]# find /root/data -name "*.txt"
/root/data/1.txt
/root/data/2.txt
/root/data/3.txt
/root/data/4.txt
/root/data/5.txt

2)搜索一天以内最后修改时间的文件;并将文件删除

使用-exec参数将前面的文件进行处理,也可使用find配合xargs将文件进行删除。

[root@localhost ~]# find /root/data -mtime -1 
/root/data
/root/data/1.txt
/root/data/2.txt
/root/data/3.txt
/root/data/4.txt
/root/data/5.txt
[root@localhost ~]# find /root/data -mtime -1 -exec rm -f {} \; 

[root@localhost ~]# find /root/data -mtime -1 |xargs -i rm -f {}

[root@localhost ~]# ll /root/data/
总用量 0

3)搜索777权限的文件

[root@localhost ~]# find / -type f -perm 777

4)搜索一天之前变动的文件复制到test目录下

[root@localhost ~]# find /etc -type f -ctime +1 -exec cp -a {} /root/test \;
[root@localhost ~]# ll /root/test | wc -l
1252

5)搜索指定用户的文件

[root@localhost ~]# find / -type f -user host 

6)在/etc目录下搜索大于5M,小于10M的文件

[root@localhost ~]# find /etc -type f -size +5M -and -size -10M 
/etc/udev/hwdb.bin

7)搜索指定组的目录

[root@localhost ~]# find / -type d -group host
/var/tmp/yum-host-u08wM2
/var/tmp/yum-host-u08wM2/x86_64
/var/tmp/yum-host-u08wM2/x86_64/7
[root@localhost ~]# find / -type d -group host | wc -l
18

8)在/var/log目录搜索指定后缀的文件 

-iname表示不区分大小写的的文件名称

[root@localhost ~]# find /var/log -type f -iname "*.log"
/var/log/tuned/tuned.log
/var/log/audit/audit.log
/var/log/anaconda/anaconda.log
/var/log/anaconda/X.log
/var/log/anaconda/program.log
/var/log/anaconda/packaging.log
/var/log/anaconda/storage.log
......

 9)在/var/log目录搜索指定后缀不是.log的文件

[root@localhost ~]# find /var/log -type f ! -name ".log" | wc -l
71

10)从根目录下搜索可执行的文件并复制到目录下

[root@localhost /]# find / -type f -perm /a=x -exec cp -a {} /root/sys_sh \;

命令总结

        find命令很多参数都可以结合起来一起使用的,关键还是要能记住每个参数可以实现的功能。若觉得以上内容还行,可以点赞支持一下!

                

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Linux学习中

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值