find命令
1、查看当前目录下以.txt结尾的文件
[root@test ~]# find . -name "*.txt"
./.subversion/README.txt
./2.txt
./nmaptest.txt
./1.txt
不是以.txt结尾的文件查找:
[root@test ~]# find . ! -name "*.txt"| more
.
./.cshrc
./.subversion
./.subversion/servers
./.subversion/config
2、根据文件类型查找
f 普通文件 l 符号连接 d 目录 c 字符设备 b 块设备 s 套接字 p Fifo
[root@test ~]# find /tmp/ -type d -name mysql
/tmp/mysql
/tmp/mysql/mysql
3、根据文件时间戳进行搜索
Linux文件系统每个文件都有三种时间戳:
访问时间(-atime/天,-amin/分钟):用户最近一次访问时间。
修改时间(-mtime/天,-mmin/分钟):文件最后一次修改时间。
变化时间(-ctime/天,-cmin/分钟):文件数据(例如权限等)最后一次修改时间。
[root@test ~]# stat 1.txt
File: `1.txt'
Size: 37 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768dInode: 21749 Links: 2
Access: (0626/-rw--w-rw-) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2017-10-23 12:12:07.619932872 +0800#当文件被访问时变化
Modify: 2017-10-23 14:36:12.180013439 +0800#当文件被修改时变化
Change: 2017-10-23 14:36:12.192013624 +0800#当文件被修改时变化
+2:2天以外的;-2:2天以内的;xargs:额外扩展
[root@test ~]# find . -ctime +2 -name "*.txt"|xargs ls -lt
-rw-r--r-- 1 root root 4276 Oct 17 14:45 ./.subversion/README.txt
-rw-r--r-- 1 root root 4821 Oct 11 04:12 ./setuptools-36.5.0/setuptools.egg-info/SOURCES.txt
-rw-r--r-- 1 root root 2939 Oct 11 04:12 ./setuptools-36.5.0/setuptools.egg-info/entry_points.txt
该目录10分钟之前被访问过的
[root@test ~]# find . -type f -amin +10
You have new mail in /var/spool/mail/root
该目录10分钟之内被访问过的
[root@test ~]# find . -type f -amin -10
./2.txt
./1.txt
4、根据文件大小进行匹配(K/M/G)
4.1 搜索大于10KB的文件
[root@test ~]# find . -type f -size +10k
./get-pip.py
./.bypy/bypy.hashcache.json
小于10KB的文件
[root@test ~]# find . -type f -size -10k
5、借助-exec选项与其他命令结合使用
将".txt"结尾的文件都删除
[root@test76 83-server]# find . -name '*.txt' -exec rm {} \;
6、用到-o选项(两个条件满足一个即可);找出当前目录下以.txt,.php结尾的文件;
[root@test ~]# find . -name '*.php' -o -name '*.txt'
./.subversion/README.txt
./2.txt
./123.txt
./index.php
用到-a选项;两个条件同时满足
[root@test ~]# find . -size +10k -a -size -100k|xargs du -sh
52K ./.cache/pip/http/2/e/a/f/9/2eaf9e7adb17e72f1ab2b6510f37425dc6772b
16K ./.cache/pip/http/9/e/6/1/9/9e61964f51d8a05a20ecf21eef694877f28cb
52K ./.cache/pip/http/c/e/8/8/0/ce880eded052487dc850e45bc
88K ./.cache/pip/http/d/f/6/b/4/df6b402f6800301aea4d1b04d0c
68K ./.cache/pip/http/e/8/a/4/5/e8a45d85bbef483
56K ./.cache/pip/http/f/c/9/f/0/fc9f0666469c64f19c
20K ./.pip/pip.log
32K ./.bash_history
文件名后缀
file 文件名
[root@~]# file test.sh
test.sh: POSIX shell script text executable
.php php解释语言
.so 库文件
.bz2 bzip2的压缩文件
.gz gzip的压缩文件
.tar tar打包文件
.sh shell脚本
.log 日志文件