Linux 命令:find

1. 写在前面

本文主要介绍 Linux find 命令:是一种用于搜索文件层次结构的命令行实用程序。它可用于搜索文件和目录,并对其执行后续操作。它支持按文件、文件夹、名称、创建日期、修改日期、所有者和权限进行搜索。通过使用 -exec,还可以对找到的文件或文件夹执行其他命令。

关注 公众号 获取最新博文: 滑翔的纸飞机

2. find 命令

find 命令的基本语法是:

$ find [where to start searching from]
 [expression determines what to find] [-options] [what to find]

参数:

参数描述
-exec CMD对匹配的文件执行该参数所给出的shell命令,命令执行成功后的退出状态返回 0
-ok CMD与exec作用相同,区别在于,在执行命令之前,都会给出提示,让用户确认是否执行
-inum N搜索inode为 "N "的文件
-links N搜索带有 "N "链接的文件
-name demo搜索 "demo "指定的文件
-newer file搜索在 "file"之后修改/创建的文件
-perm octal按执行权限来搜索
-print显示通过其他条件找到的文件的路径名
-empty搜索空文件和目录
-size +N/-N按文件大小搜索,支持使用 + 或 - 表示大于或小于指定大小,单位可以是 c(字节)、w(字数)、b(块数)、k(KB)、M(MB)或 G(GB)
-user name按文件所有者搜索

2.1 示例

树状层次结构:

.
├── PAPER
│   ├── demo
│   │   ├── sample.txt
│   │   └── sample1.txt
│   ├── demo1
│   └── demo12
├── head
│   ├── a.txt
│   ├── b.txt
│   ├── c.txt
│   ├── capital.txt
│   ├── d.txt
│   └── state.txt
├── pattern.txt
└── text.txt

(1)搜索具有特定名称的文件

root@dev:~/linux# find ./PAPER/ -name sample.txt 
---------------------------------------------------
./PAPER/demo/sample.txt

它会搜索 PAPER 目录中的 sample.txt。

(2)用匹配符搜索文件

  • 搜索所有尾部带有".txt "的文件
root@dev:~/linux# find ./PAPER -name '*.txt' 
---------------------------------------------------
./PAPER/demo/sample1.txt
./PAPER/demo/sample.txt
  • 搜索所有大写字母开头,尾部带有".txt "的文件
root@dev:~/linux# find . -name '[A-Z]*.txt' 
---------------------------------------------------
./Text.txt
  • 搜索所有 patt 开头,尾部带有".txt "的文件
root@dev:~/linux# find . -name 'patt*.txt' 
./pattern.txt
  • 搜索不是 patt 开头的所有文件
root@dev:~/linux# find . -name "patt*" -prune -o -name "*.txt"
----------------------------------------------------------------
./head/state.txt
./head/a.txt
./head/b.txt
./head/c.txt
./head/capital.txt
./head/d.txt
./PAPER/demo/sample1.txt
./PAPER/demo/sample.txt
./text.txt
./Text.txt
./pattern.txt
  • 搜索当前目录除 PAPER 之外的子目录内搜索 txt 文件
root@dev:~/linux# find . -path "./PAPER" -prune -o -name "*.txt"
----------------------------------------------------------------
./head/state.txt
./head/a.txt
./head/b.txt
./head/c.txt
./head/capital.txt
./head/d.txt
./PAPER
./text.txt
./Text.txt
./pattern.txt
  • 当前目录,不再子目录中,搜索txt文件
root@dev:~/linux# find . ! -name "." -type d -prune -o -type f -name 
"*.txt" 
----------------------------------------------------------------
./head
./PAPER
./text.txt
./Text.txt
./pattern.txt

(3)执行命令

  • 搜索确认后并删除文件
root@dev:~/linux# find ./PAPER/ -name sample.txt -exec rm -i {} \; 
rm: remove regular empty file './PAPER/demo/sample.txt'? y
-------------------------------------------------------------------------

如果输入 “Y/y”,就会删除该文件。

  • 搜索 sample1.txt 并备份为sample1.txt.bak
root@dev:~/linux# find . -name 'sample1.txt' -exec cp {} {}.bak \;
root@dev:~/linux# ls ./PAPER/demo/
sample.txt  sample1.txt  sample1.txt.bak
  • 搜索文件,并 ls -l 列出
root@dev:~/linux# find . -type f -exec ls -l {} \; 
-------------------------------------------------------------------------
-rw-r--r-- 1 root root 287 Dec  7 00:46 ./head/state.txt
-rw-r--r-- 1 root root 0 Dec  7 00:57 ./head/a.txt
-rw-r--r-- 1 root root 0 Dec  7 00:57 ./head/b.txt
-rw-r--r-- 1 root root 0 Dec  7 00:57 ./head/c.txt
-rw-r--r-- 1 root root 213 Dec  7 00:46 ./head/capital.txt
-rw-r--r-- 1 root root 0 Dec  7 00:57 ./head/d.txt
-rw-rw-r-- 1 root root 0 Dec  7 23:33 ./PAPER/demo/sample1.txt
-rw-r--r-- 1 root root 0 Dec  8 00:19 ./PAPER/demo/sample1.txt.bak
-rw-r--r-- 1 root root 0 Dec  7 23:45 ./PAPER/demo/sample.txt
-rw-r--r-- 1 root root 185 Nov 29 23:47 ./text.txt
-rw-r--r-- 1 root root 0 Dec  7 23:46 ./Text.txt
-rw-r--r-- 1 root root 11 Nov 30 00:12 ./pattern.txt
  • 在目录中搜索更改时间在3日以前的文件并删除它们
root@dev:~/linux# find . -mtime +3 -type f -ok -exec rm {} \;
  • 查询当天修改过的文件
root@dev:~/linux# find . -mtime -1 -type f -exec ls -l {} \;
-------------------------------------------------------------------------
-rw-r--r-- 1 root root 287 Dec  7 00:46 ./head/state.txt
-rw-r--r-- 1 root root 0 Dec  7 00:57 ./head/a.txt
-rw-r--r-- 1 root root 0 Dec  7 00:57 ./head/b.txt
-rw-r--r-- 1 root root 0 Dec  7 00:57 ./head/c.txt
-rw-r--r-- 1 root root 213 Dec  7 00:46 ./head/capital.txt
-rw-r--r-- 1 root root 0 Dec  7 00:57 ./head/d.txt
-rw-rw-r-- 1 root root 0 Dec  7 23:33 ./PAPER/demo/sample1.txt
-rw-r--r-- 1 root root 0 Dec  8 00:19 ./PAPER/demo/sample1.txt.bak
-rw-r--r-- 1 root root 0 Dec  7 23:45 ./PAPER/demo/sample.txt
-rw-r--r-- 1 root root 0 Dec  7 23:46 ./Text.txt
  • 查询文件并询问是否要显示
root@dev:~/linux# find . -mtime -1 -type f -ok ls -l {} \;  
-------------------------------------------------------------------------
< ls ... ./head/state.txt > ? y
-rw-r--r-- 1 root root 287 Dec  7 00:46 ./head/state.txt
< ls ... ./head/a.txt > ? y
-rw-r--r-- 1 root root 0 Dec  7 00:57 ./head/a.txt

(4)搜索空文件和目录

root@dev:~/linux# find ./PAPER/ -empty
-------------------------------------------------------------------------
./PAPER/demo12
./PAPER/demo1
./PAPER/demo/sample1.txt

该命令可搜索目录或子目录中的所有空文件夹和文件。

(5)按执行权限来搜索

root@dev:~/linux# find ./PAPER/ -perm 664
-------------------------------------------------------------------------
./PAPER/demo/sample1.txt

该命令以给定的权限搜索 PAPER 目录或子目录中的所有文件。

(6)按类型搜索( b/d/c/p/l/f )

按目录搜索:d,其他这里不再举例

root@dev:~/linux# find . -type d
-------------------------------------------------------------------------
.
./head
./PAPER
./PAPER/demo12
./PAPER/demo1
./PAPER/demo

(7)搜索多个文件中的文本

root@dev:~/linux# find ./ -type f -name "*.txt" -exec grep 'Unix'  {} \;
-------------------------------------------------------------------------
Unix linux which one you choose.

该命令打印包含 "Unix “的行,”-type f "指定输入类型为文件。

(8)按时间搜索

  • 搜索2天内被更改过的文件
root@dev:~/linux# find . -mtime -2 -type f 
-------------------------------------------------------------------------
./head/state.txt
./head/a.txt
./head/b.txt
./head/c.txt
./head/capital.txt
./head/d.txt
./PAPER/demo/sample1.txt
./PAPER/demo/sample.txt
./Text.txt
  • 搜索1天前被更改过的文件
root@dev:~/linux# find . -mtime +1 -type f
-------------------------------------------------------------------------
./text.txt
./pattern.txt
  • 搜索一天内被访问的文件
root@dev:~/linux# find . -atime -1 -type f
-------------------------------------------------------------------------
./head/state.txt
./head/a.txt
./head/b.txt
./head/c.txt
./head/capital.txt
./head/d.txt
./PAPER/demo/sample1.txt
./PAPER/demo/sample.txt
./text.txt
./Text.txt
./pattern.txt
  • 搜索一天前被访问的文件
find . -atime +1 -type f
  • 搜索一天内状态被改变的文件
root@dev:~/linux# find . -ctime -1 -type f 
-------------------------------------------------------------------------
./head/state.txt
./head/a.txt
./head/b.txt
./head/c.txt
./head/capital.txt
./head/d.txt
./PAPER/demo/sample1.txt
./PAPER/demo/sample.txt
./Text.txt
  • 搜索一天前状态被改变的文件
root@dev:~/linux# find . -ctime +1 -type f 
-------------------------------------------------------------------------
./text.txt
./pattern.txt
  • 搜索10分钟以前状态被改变的文件
root@dev:~/linux# find . -cmin +10 -type f 
-------------------------------------------------------------------------
./head/state.txt
./head/a.txt
./head/b.txt
./head/c.txt
./head/capital.txt
./head/d.txt
./PAPER/demo/sample1.txt
./PAPER/demo/sample.txt
./text.txt
./Text.txt
./pattern.txt

(9)按属主及属组

  • 搜索属组 root 的文件
root@dev:~/linux# find . -group root -type f
-------------------------------------------------------------------------
./head/state.txt
./head/a.txt
./head/b.txt
./head/c.txt
./head/capital.txt
./head/d.txt
./PAPER/demo/sample1.txt
./PAPER/demo/sample.txt
./text.txt
./Text.txt
./pattern.txt
  • 搜索用户组被删掉的文件
root@dev:~/linux# find / -nogroup -type f 
  • 搜索属主是 root 的文件
root@dev:~/linux# find . -user root -type f 
-------------------------------------------------------------------------
./head/state.txt
./head/a.txt
./head/b.txt
./head/c.txt
./head/capital.txt
./head/d.txt
./PAPER/demo/sample1.txt
./PAPER/demo/sample.txt
./text.txt
./Text.txt
./pattern.txt
  • 搜索属主被删除的文件
find . -nouser -type f 

(10)按文件新旧

  • 搜索比 pattern.txt 新的文件
root@dev:~/linux# find . -newer "pattern.txt" -type f
-------------------------------------------------------------------------
./head/state.txt
./head/a.txt
./head/b.txt
./head/c.txt
./head/capital.txt
./head/d.txt
./PAPER/demo/sample1.txt
./PAPER/demo/sample.txt
./Text.txt
  • 搜索比 pattern.txt 旧的文件
root@dev:~/linux# find . ! -newer "pattern.txt" -type f 
-------------------------------------------------------------------------
./text.txt
./pattern.txt
  • 搜索比text.txt新,比pattern.txt旧的文件
root@dev:~/linux# find . -newer 'text.txt' ! -newer 'pattern.txt' -type f
-------------------------------------------------------------------------
./pattern.txt

(11)按大小搜索

  • 搜索超过1字节的文件
root@dev:~/linux# find . -size +1c -type f 
-------------------------------------------------------------------------
./head/state.txt
./head/capital.txt
./text.txt
./pattern.txt
  • 搜索等于1字节的文件
root@dev:~/linux# find . -size 1c -type f 
  • 搜索小于1字节的文件
root@dev:~/linux# find . -size -1c -type f 
-------------------------------------------------------------------------
./head/a.txt
./head/b.txt
./head/c.txt
./head/d.txt
./PAPER/demo/sample1.txt
./PAPER/demo/sample.txt
./Text.txt
感谢您花时间阅读文章!
关注公众号不迷路!
  • 5
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值