> 文章列表 > Linux基础命令-find搜索文件位置

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

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

                

报价网