文件查找Find
  DcpJeOZ6VzTX 2023年11月02日 39 0

文件查找-find

find可以根据不同的条件来进行查找文件:例如权限、拥有者、修改日期/时间、文件大小等等

find

语法
find 路径 选项 参数

按文件名查找

-name
# 在根下找名字为1.txt的文件或目录
[root@localhost ~]# find / -name 1.txt
/root/1.txt

# 不区分大小写(-i)
[root@localhost ~]# find /root -iname 1.txt
/root/1.txt
/root/1.TXT

# 找.txt结尾的
[root@localhost ~]# find / -name "*.txt"
/etc/pki/nssdb/pkcs11.txt
/root/1.txt
/root/4.txt
/var/cache/yum/x86_64/7/timedhosts.txt

按文件类型

-type
-------------------------------------------------
d 目录
f 文件(file)
l 链接文件
s socket(安全套接字)
p 管道文件
b 块设备(磁盘)
c 块设备(设备)
-------------------------------------------------
# root下找文件
find /root -type f
# root下找目录
find /root -type d
# 根下找socket文件
find / -type s
# 根下找c设备文件
find / -type c
# 根下找块设备文件
find / -type b
# 根下找链接文件
find / -type l
# 根下找管道文件
find / -type p

按文件大小

-size
-------------------------------------------------
a and 且 
o or  或
!no  非
-------------------------------------------------
# -size 按大小查找
   +5M 查找大于5M的
   -4M 查找小于4M的
   3M  精确查找3M
   -# 查找根下小于5M大于1M的
   find / -size -5M -a -size +1M 
   -# 查找11k的文件
   [root@web03 ~]# find -type f -size 11k
   ./ab
   [root@web03 ~]# find -type f -a -size 11k
   ./ab
   
# 查看文件大小(du -sh)
[root@web03 ~]# du -sh 1.txt
20M     1.txt

根据文件用户查找

-user
# 查找home下属主是haha
find /home -user haha

-group 
# 查找home下属组是haha
find /home -group haha

# 查找home下属主是haha或ha
find /home -user haha -o -user ha
# 查找home下属主是haha且属组数ha的
find /home -user haha -a -group ha

# 没有属主
find /home/ -nouser
# 没有属组
find /home/ -nogroup

按时间查找

-mtime
# +7 查找7天之前的文件,不包含当天
find / -type f -name '*.txt' -mtime +7
# -7 查找最近7天的文件,包含当天
find / -type f -name '*.txt' -mtime -7
# 7 查找不包含当天的,往前数第7天的
find / -type -name '*.txt' -mtime 7



-mtime:Modified time 是在写入文件时随文件内容的更改而更改的
# 修改内容的命令都会刷新mtime 和 ctime

-atime:Access time 是在读取文件或者执行文件时更改 
# 类似 cat vim命令都会刷新atime

-ctime:Change time 是在写入文件、更改所有者、权限或链接设置
# 修改内容的命令都会刷新mtime 和 ctime 只有修改文件属性的时候ctime才会单独刷新时间戳

[root@web02 ~]# stat 1.txt 
  File: ‘1.txt’
  Size: 1512            Blocks: 8          IO Block: 4096   regular file
Device: 803h/2051d      Inode: 33574979    Links: 1
Access: (0600/-rw-------)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2023-04-12 11:19:55.230174198 +0800
Modify: 2023-04-12 11:19:55.236174199 +0800
Change: 2023-04-12 11:19:55.236174199 +0800
 Birth: -

按深度查找

-maxdepth
# 查找一级目录
find /etc/ -type d -maxdepth 1
# 查找二级目录
find /etc/ -type d -maxdepth 2
# 查找三级目录
find /etc/ -type d -maxdepth 3

按权限查找

-perm 
find -perm 644
find ./ -perm 600
# 精确查找
-perm 222  (后面的数字是文件的数字权限,也可以是777 744等等)
# 模糊匹配
-perm -222
# 包含set uid
find /user/sbin -perm -4000 -ls (# -ls是将查找到的文件展开)
# 包含set gid
 find /usr/sbin -perm -2000 -ls
# 包含sticky
find /usr/sbin -perm -1000 -ls

find 的动作(了解)

动作

含义

-print

打印查找到的内容(默认)

-ls

以长格式显示的方式打印查找到的内容

-delete

删除查找到的文件(仅能删除空目录)

-ok

后面跟自定义shell命令(会提示是否操作)

-exec

后面跟自定义shell命令(标准写法-exec \;)(例如:-exec rm -fr {} \;)

[root@pkq ~]# find ./ -type f -name 6.txt -
ok cp {} /tmp \;
< cp ... ./6.txt > ? y
[root@pkq ~]# ll /tmp/
total 0
-rw-r--r--. 1 root root 0 Mar 27 18:44 1.sh
-rw-r--r--. 1 root root 0 Mar 27 20:27
6.txt
[root@pkq ~]# find ./ -type f -name 7.txt -
exec cp {} /tmp \;

练习

1.查找/tmp目录下,属主不是root,且文件名不是以f开头的文件
#find /tmp -type f -a ! -user root  ! -name 'f*'
#find /tmp  ! -user root  ! -name 'f*'
2.查找/var目录下属主为root,且属组为mail的所有文件
find /var -user root -a -group mail
3.查找/var目录下不属于root、oldboy、zls组的所有文件
find /var ! -group root -a ! -group oldboy -a ! -group -zls 
4.查找/var目录下最近一周内其内容修改过,同时属主不为root,也不是postfix的文件
find /var -mtime -7 ! -user root ! -user postfix
5.查找/etc/下所有大于1M且类型为普通文件的所有文件
find /etc -size +1M -type f
6.将/etc中的所有目录(仅目录)复制到/tmp下,目录结构不变
find /etc -type d | xargs -I {} cp -a {} /tmp
#find /etc -type d | xargs -I {} mkdir -p /tmp/{} 
7.将/etc目录复制到 /var/tmp,/var/tmp/etc的所有目录权限为777,/var/tmp/etc/目录中所有文件权限为666
 cp -a /etc /var/tmp
 find /var/tmp/etc -type d |xargs chmod 777
 find /var/tmp/etc -type f |xargs chmod 666 
9.创建touch file{1..10}10个文件,保留file9,其他一次全部删除
touch file{1..10}
find ! -name 'file9' -exec rm -vf {} \;
find ! -name 'file9' -delete
10.解释如下每条命令的含义
mkdir /root/dir1
在root下创建目录dir1
touch /root/dir1/file{1..10}
在root下的dir1目录里创建文件file1到file10
find /root/dir1 -type f -name 'file5'
查找在root下的dir1目录里名为file5的普通文件
find /root/dir1 ! -name 'file5'
查找在root下的dir1目录里除了名为file5的文件
find /root/dir1 -name 'file5' -o -name 'file9'
查找在root下的dir1目录里名为file5的文件或名为file9的文件
find /root/dir1 -name 'file5' -o -name 'file9' -ls
查找在root下的dir1目录里名为file5的文件或名为file9的文件并列出详细信息
find /root/dir1 (-name 'file5' -o -name 'file9' ) -ls
(find /root/dir1 \( -name 'file5' -o -name 'file9' \) -ls)
查找在root下的dir1目录里除了名为file5的文件或名为file9的文件并列出详细信息
find /root/dir1 (-name 'file5' -o -name 'file9' ) -exec rm -rvf {} \;
查找强制删除在root下的dir1目录里名为file5的文件或名为file9的文件
find /root/dir1 ! (-name 'file5' -o -name 'file9' ) -exec rm -vf {} \;
查找删除在root下的dir1目录里除了名为file5的文件或名为file9的文件

拓展

文件查找Find_find查找

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

上一篇: 压缩打包 下一篇: 重定向
  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  DcpJeOZ6VzTX   2023年11月02日   40   0   0 find查找