shell脚本攻略读书笔记之七&&&find命令精解
.
specifies current directory and ..
specifies the parent directory。
【译:.
指的是当前目录,..
指的是当前目录的上层目录】
find . -print
:Print lists of files and folders
【打印当前目录中的文件和文件夹】
-name
参数指定和filename匹配的字符串。
[root@server4 hadoop]# ll
total 156
-rw-r--r--. 1 500 500 4113 Mar 8 2016 mapred-queues.xml.template
-rw-r--r--. 1 500 500 865 Jul 4 07:56 mapred-site.xml
-rw-r--r--. 1 root root 535 Jun 18 01:05 out.txt
-rw-r--r--. 1 500 500 930 Jul 13 10:31 yarn-site.xml
[root@server4 hadoop]# find . -name '*.txt' -print
./out.txt
.
:当前目录。
-name
: 指定查询的文件名
*.txt
:通配符
-print
:打印出所有结果
- 打印出指定的文件【使用了\( \)
】
[root@server4 backup]# ll
total 0
-rw-r--r--. 1 root root 0 Aug 10 19:01 example.txt
-rw-r--r--. 1 root root 0 Aug 10 19:01 EXAMPLE.txt
-rw-r--r--. 1 root root 0 Aug 10 19:01 file.txt
-rw-r--r--. 1 root root 0 Aug 10 19:02 new.txt
-rw-r--r--. 1 root root 0 Aug 10 19:02 some.jpg
-rw-r--r--. 1 root root 0 Aug 10 19:02 text.pdf
[root@server4 backup]# find . \( -name "*.txt" -o -name "*.pdf" \) -print
./example.txt
./EXAMPLE.txt
./file.txt
./new.txt
./text.pdf
- 查找指定路径下,包括指定字符的文件,然后输出
[root@server4 backup]# find /root/backup/ -path "*/little*" -print
/root/backup/littlelawson
/root/backup/little.txt
[root@server4 backup]# ll
total 0
-rw-r--r--. 1 root root 0 Aug 10 19:01 example.txt
-rw-r--r--. 1 root root 0 Aug 10 19:01 EXAMPLE.txt
-rw-r--r--. 1 root root 0 Aug 10 19:01 file.txt
drwxr-xr-x. 2 root root 6 Aug 10 19:07 littlelawson
-rw-r--r--. 1 root root 0 Aug 10 19:07 little.txt
-rw-r--r--. 1 root root 0 Aug 10 19:02 new.txt
-rw-r--r--. 1 root root 0 Aug 10 19:02 some.jpg
-rw-r--r--. 1 root root 0 Aug 10 19:02 text.pdf
- 查找不是以
.txt
结尾的文件或者是文件夹【使用!
】,并输出
[root@server4 backup]# find . ! -name "*.txt" -print
. #为什么这里会输出.号
./some.jpg
./text.pdf
./littlelawson
- 限制最大查找深度(使用
-maxdepth
)【这里的1就是指当前目录】
[root@server4 backup]# find . -maxdepth 1 -name "f*" -print
./file.txt
- 限制最小查找深度(使用
-mindepth
)【 当前是第1层,子目录是第2层】
[root@server4 backup]# find . -mindepth 2 -name "f*" -print
./littlelawson/file.test
[root@server4 backup]# ll
total 0
-rw-r--r--. 1 root root 0 Aug 10 19:01 example.txt
-rw-r--r--. 1 root root 0 Aug 10 19:01 EXAMPLE.txt
-rw-r--r--. 1 root root 0 Aug 10 19:01 file.txt
drwxr-xr-x. 2 root root 23 Aug 10 20:05 littlelawson
-rw-r--r--. 1 root root 0 Aug 10 19:07 little.txt
-rw-r--r--. 1 root root 0 Aug 10 19:02 new.txt
-rw-r--r--. 1 root root 0 Aug 10 19:02 some.jpg
-rw-r--r--. 1 root root 0 Aug 10 19:02 text.pdf
Note
-maxdepth 与 -mindepth参数的位置与find的效率有关。比如说,先查找符合name条件的文件,这样找的文件还需要再经过深度测试。这样就会产生很多低效的操作。相反,如果是先搜索深度,再比较name就会高效很多。
By using -type, we can specify to the find command that it should only match files having a specified type.
list only directories including descendants
find . -type d -print
list only regular files as follows
find . -type f -print
list only symbolic links as follows
find . -type l -print
File type Type argument
Regular file f
Symbolic link l
Directory d
Character special
device c
Block device b
Socket s
FIFO p
- 根据访问、修改、创建时间查找文件
Print all the files that were accessed within the last seven days as follows:[最近7天内访问的数据]
$ find . -type f -atime -7 -print
Print all the files that are having access time exactly seven-days old as follows:[恰好为7天时访问的数据]
$ find . -type f -atime 7 -print
Print all the files that have an access time older than seven days as follows:[7天之前访问的数据]
$ find . -type f -atime +7 -print
[root@server4 backup]# find . -type f -amin +1 -print
./example.txt
./EXAMPLE.txt
./file.txt
./new.txt
./some.jpg
./text.pdf
./littlelawson/file.test
./little.txt
[root@server4 backup]# find . -type f -amin +100 -print
[root@server4 backup]# find . -type f -amin +10 -print
./example.txt
./EXAMPLE.txt
./file.txt
./new.txt
./some.jpg
./text.pdf
./littlelawson/file.test
./little.txt
- 根据文件大小查找数据
based on the file sizes of the files, a search can be performed as follows:
find . -type f -size +2k
find . -type f -size -2k
find . -type f -size 2k
- 其它参数
-delete 参数,将符合搜索结果的文件执行删除操作
-perm 参数,查找符合条件【based on the file permissions and ownership】的文件
[root@server4 backup]# ll
total 0
-rw-r--r--. 1 root root 0 Aug 10 19:01 example.txt
-rw-r--r--. 1 root root 0 Aug 10 19:01 EXAMPLE.txt
-rw-r--r--. 1 root root 0 Aug 10 19:01 file.txt
drwxr-xr-x. 2 root root 23 Aug 10 20:05 littlelawson
-rw-r--r--. 1 root root 0 Aug 10 19:07 little.txt
-rw-r--r--. 1 root root 0 Aug 10 19:02 new.txt
-rw-r--r--. 1 root root 0 Aug 10 19:02 some.jpg
-rw-r--r--. 1 root root 0 Aug 10 19:02 text.pdf
如下这条命令是找出文件类型为file(普通文件),用户是root,然后执行(-exec)操作(将其放到littlelawson文件夹中)。
[root@server4 backup]# find . -type f -user root -exec chown littlelawson {} \;
[root@server4 backup]# ll
total 0
-rw-r--r--. 1 littlelawson root 0 Aug 10 19:01 example.txt
-rw-r--r--. 1 littlelawson root 0 Aug 10 19:01 EXAMPLE.txt
-rw-r--r--. 1 littlelawson root 0 Aug 10 19:01 file.txt
drwxr-xr-x. 2 root root 23 Aug 10 20:05 littlelawson
-rw-r--r--. 1 littlelawson root 0 Aug 10 19:07 little.txt
-rw-r--r--. 1 littlelawson root 0 Aug 10 19:02 new.txt
-rw-r--r--. 1 littlelawson root 0 Aug 10 19:02 some.jpg
-rw-r--r--. 1 littlelawson root 0 Aug 10 19:02 text.pdf