find是个很强大的命令,用法很多。

作用:查找目录下的文件,同时也可以调用其他命令执行相应的操作

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

用法:

find [选项] [路径][操作语句]

find [-H] [-L] [-P] [-D debugopts] [-Olevel]  [pathname]  [expression]

expression包含 options(参数) tests(限定的条件) actions(执行的动作) 三个模块

1,先预习ls命令的几个参数

ls -lt: 根据文件修改时间排序,最新的在前面

ghostwu@dev:~$ ls -lt python/
total 40
-rw-rw-r-- 1 ghostwu ghostwu 124 3月  18 21:55 global2.py
-rw-rw-r-- 1 ghostwu ghostwu 150 3月  18 21:53 global.py
-rw-rw-r-- 1 ghostwu ghostwu  99 3月  18 21:48 func5.py
-rw-rw-r-- 1 ghostwu ghostwu  81 3月  18 21:33 func4.py
-rw-rw-r-- 1 ghostwu ghostwu  58 3月  18 21:31 func3.py
-rw-rw-r-- 1 ghostwu ghostwu 179 3月  18 21:29 func2.py
-rw-rw-r-- 1 ghostwu ghostwu  44 3月  18 21:26 func.py
-rw-rw-r-- 1 ghostwu ghostwu  92 3月  18 21:23 while1.py
-rw-rw-r-- 1 ghostwu ghostwu  90 3月  18 21:19 while.py
-rw-rw-r-- 1 ghostwu ghostwu  82 3月  18 21:08 for.py

ls -ult:加上参数u表示 按文件访问时间排序,最新的在前面

ghostwu@dev:~$ ls -ult python/
total 40
-rw-rw-r-- 1 ghostwu ghostwu  92 5月   6 22:21 while1.py
-rw-rw-r-- 1 ghostwu ghostwu  99 5月   6 22:21 func5.py
-rw-rw-r-- 1 ghostwu ghostwu  82 5月   6 22:21 for.py
-rw-rw-r-- 1 ghostwu ghostwu  90 5月   6 22:21 while.py
-rw-rw-r-- 1 ghostwu ghostwu 124 5月   6 22:21 global2.py
-rw-rw-r-- 1 ghostwu ghostwu 150 5月   6 22:21 global.py
-rw-rw-r-- 1 ghostwu ghostwu  44 5月   6 22:21 func.py
-rw-rw-r-- 1 ghostwu ghostwu  81 5月   6 22:21 func4.py
-rw-rw-r-- 1 ghostwu ghostwu  58 5月   6 22:21 func3.py
-rw-rw-r-- 1 ghostwu ghostwu 179 5月   6 22:21 func2.py

2,查找指定时间内访问过的文件, atime:访问时间 -2:2天内, atime后面一般跟 -atime [-n|n|+n]。

-n: 文件访问时间距现在n天内

n: 文件访问时间距现在第n天

+n: 文件访问时间距现在4天以前

ghostwu@dev:~$ find ./python -atime -2
./python
./python/func2.py
./python/func3.py
./python/func4.py
./python/func.py
./python/global.py
./python/global2.py
./python/while.py
./python/for.py
./python/func5.py
./python/while1.py

3,-name 按照文件名查找,一般只支持*, ?, []等匹配符

查找3天前,修改过的日志文件

root@dev:~# find / -atime +3 -name "*.log"
/var/log/apache2/access.log
....
root@dev:~# stat /var/log/apache2/access.log
  File: '/var/log/apache2/access.log'
  Size: 0             Blocks: 0          IO Block: 4096   regular empty file
Device: 806h/2054d    Inode: 403981      Links: 1
Access: (0640/-rw-r-----)  Uid: (    0/    root)   Gid: (    4/     adm)
Access: 2018-02-10 14:25:28.955350445 +0800
Modify: 2018-02-10 14:25:28.955350445 +0800
Change: 2018-02-10 14:25:28.971350236 +0800

4,-type 查找指定类型

b( 块设备文件 ), c( 字符设备文件 ), d( 目录 ), p( 管道文件 ), l( 符号链接文件 ), f( 普通文件 ), s( socket 文件 ), D( door )

ghostwu@dev:~/linux$ ls
cp
ghostwu@dev:~/linux$ tree cp
cp
├── ghostwu_hardlink
├── ghostwu_home -> /home/ghostwu/
├── ghostwu_softlink -> ghostwu.txt
├── ghostwu.tar.gz
└── ghostwu.txt

1 directory, 4 files
ghostwu@dev:~/linux$ mkdir -p cp/{a..d}
ghostwu@dev:~/linux$ tree cp
cp
├── a
├── b
├── c
├── d
├── ghostwu_hardlink
├── ghostwu_home -> /home/ghostwu/
├── ghostwu_softlink -> ghostwu.txt
├── ghostwu.tar.gz
└── ghostwu.txt

5 directories, 4 files
ghostwu@dev:~/linux$ find . -type d
.
./cp
./cp/a
./cp/c
./cp/d
./cp/b
ghostwu@dev:~/linux$ find . ! -type d
./cp/ghostwu.tar.gz
./cp/ghostwu_hardlink
./cp/ghostwu_home
./cp/ghostwu.txt
./cp/ghostwu_softlink

find . ! -type d 这里的感叹号表示 取反

5,-perm 按指定的权限来查找

ghostwu@dev:~/linux$ ls -l cp
total 60
drwxrwxr-x 2 ghostwu ghostwu  4096 5月   7 22:38 a
drwxrwxr-x 2 ghostwu ghostwu  4096 5月   7 22:38 b
drwxrwxr-x 2 ghostwu ghostwu  4096 5月   7 22:38 c
drwxrwxr-x 2 ghostwu ghostwu  4096 5月   7 22:38 d
-rw-rw-r-- 2 ghostwu ghostwu 10240 5月   6 22:15 ghostwu_hardlink
lrwxrwxrwx 1 ghostwu ghostwu    14 5月   6 20:07 ghostwu_home -> /home/ghostwu/
lrwxrwxrwx 1 ghostwu ghostwu    11 5月   6 20:03 ghostwu_softlink -> ghostwu.txt
-rw-rw-r-- 1 ghostwu ghostwu 20480 5月   6 22:17 ghostwu.tar.gz
-rw-rw-r-- 2 ghostwu ghostwu 10240 5月   6 22:15 ghostwu.txt
ghostwu@dev:~/linux$ find ./cp -perm 755
ghostwu@dev:~/linux$ find ./cp -perm 775
./cp/a
./cp/c
./cp/d
./cp/b
ghostwu@dev:~/linux$ find ./cp -perm 664
./cp/ghostwu.tar.gz
./cp/ghostwu_hardlink
./cp/ghostwu.txt
 

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄