您现在的位置是:首页 > 文章详情

Linux常用基本命令之[find]用法

日期:2018-05-08点击:831

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

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

用法:

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
原文链接:https://www.centoschina.cn/command/help/10525.html
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章