Linux命令 - find

find命令用于在指定路径范围中利用表达式递归寻找文件。

Reference: Find Command in Linux (Find Files and Directories) | Linuxize.

find命令介绍:

The find command is one of the most powerful tools in the Linux system administrators arsenal. It searches for files and directories in a directory hierarchy based on a user given expression and can perform user-specified action on each matched file.

You can use the find command to search for files and directories based on their permissions, type, date, ownership, size, and more. It can also be combined with other tools such as grep or sed .

Syntax

1
find [options] [path...] [expression]
  • The options attribute controls the treatment of the symbolic links, debugging options, and optimization method. [options]可取的选项有:[-H] [-L] [-P] [-D debugopts] [-Olevel]
  • The path... attribute defines the starting directory or directories where find will search the files.
  • The expression attribute is made up of options, search patterns, and actions separated by operators.

[expression]中可选择"-type",它的取值范围:

  • b - block (buffered) special
  • c - character (unbuffered) special
  • d - directory
  • p - named pipe (FIFO)
  • f - regular file
  • l - symbolic link; this is never true if the -L option or the -follow option is in effect, unless the symbolic link is broken. If you want to search for symbolic links when -L is in effect, use -xtype.
  • s - socket
  • D - door (Solaris)

比如: “-type d”, “-type f”

举例

1
2
3
4
5
6
7
find /root -name readme.txt	# 在/root目录及其子目录下搜索"readme.txt"
[root@localhost ~]# find /root -name readme.txt
/root/violet/readme.txt

find /root -type d -name vio* #在/root目录下搜索包含"vio"的文件夹
[root@localhost ~]# find /root -type d -name vio*
/root/violet

-maxdepth <num> - 指定查找文件的目录深度(递归深度)

1
find <path> -maxdepth 1 -type f -name "filename_experssion"

指定查找文件的大小

1
find <path> -type f -size <file_size>

<file_size>后缀选取范围:

You can use the following suffixes to specify the file size:

  • b: 512-byte blocks (default)
  • c: bytes
  • w: two-byte words
  • k: Kilobytes
  • M: Megabytes
  • G: Gigabytes

在<file_size>中可以加入正号(‘+’)和负号(‘-’),分别表示大于和小于。

1
2
find . -type f -size -1M
find . -type f -size +1M

“-1M"表示小于1MB的文件,”+1M"表示大于1MB的文件。对于要搜索文件大小在1MB和2MB之间的,可以这样搜索

1
2
find . -type f -size +1M -size 2M
find . -type f -size +1M -size -2M

按照时间查找

1
2
3
4
# +10: 10天以前的数据
# 10: 过去第10天的数据
# -10: 最近10天的数据
$ find / -type f -mtime -10

-perm - 根据文件权限查找数据

1
2
3
$ find [path] -type f -perm [权限数字]
# 比如
$ find / -maxdepth 1 -type f -perm 644

find用于批量操作

配置xargs命令使用

xargs - 用于把输入的内容按行进行分割

通过以下例子理解xargs命令的作用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$ touch r{0..6}.txt
$ find -type f -name "r?.txt"
./r1.txt
./r0.txt
./r3.txt
./r6.txt
./r2.txt
./r4.txt
./r5.txt
$ find -type f -name "r?.txt" | xargs # 通过管道符'|'将'|'前面的命令的结果作为xargs的参数
./r1.txt ./r0.txt ./r3.txt ./r6.txt ./r2.txt ./r4.txt ./r5.txt
# 可以看到xargs让前面find命令输出每个结果显示在一行
# 加上'-n2'可以让find命令每2个结果显示在一行
$ find -type f -name "r?.txt" | xargs -n2
./r1.txt ./r0.txt
./r3.txt ./r6.txt
./r2.txt ./r4.txt
./r5.txt
$ find -type f -name "r?.txt" | xargs -n3
./r1.txt ./r0.txt ./r3.txt
./r6.txt ./r2.txt ./r4.txt
./r5.txt

批量查找并删除

1
2
3
4
$ find ~/ -type f -name "r?.txt" -delete
$ find ~/ -type f -name "r?.txt" -exec rm -rf {} \;
$ find ~/ -maxdepth 1 -type f -name "r?.txt" | xargs rm -f
$ rm -rf $(find ~/ -type f -name "r?.txt")

批量查找并复制

1
2
3
$ find ~/ -type f -name "r?.txt" -exec cp {} ~/violet \;
$ find ~/ -maxdepth 1 -type f -name "r?.txt"|xargs -i cp {} ~/violet
$ cp $(find ~/ -type f -name "r?.txt") ~/violet

举个例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@localhost ~]# find ~/ -maxdepth 1 -type f -name "r?.txt"
/root/r1.txt
/root/r3.txt
/root/r2.txt
/root/r4.txt
/root/r5.txt
[root@localhost ~]# find ~/ -maxdepth 1 -type f -name "r?.txt"|xargs -i cp {} ~/violet
[root@localhost ~]# ll violet
total 5
-rw-r--r-- 1 root root 80 Jan 12 20:21 r1.txt
-rw-r--r-- 1 root root 72 Jan 12 20:21 r2.txt
-rw-r--r-- 1 root root 0 Jan 12 20:21 r3.txt
-rw-r--r-- 1 root root 0 Jan 12 20:21 r4.txt
-rw-r--r-- 1 root root 0 Jan 12 20:21 r5.txt
[root@localhost ~]# find ~/ -maxdepth 1 -type f -name "r?.txt"|xargs -i rm -f {}
[root@localhost ~]# ll r?.txt
ls: cannot access r?.txt: No such file or directory

"find ~/ -maxdepth 1 -type f -name “r?.txt”|xargs -i cp {} ~/violet"命令的图解如下:

批量查找并删除的其他方法

1
2
3
$ find ~/ -maxdepth 1 -type f -name "r?.txt" -delete
$ find ~/ -maxdepth 1 -type f -name "r?.txt" -exec cp {}
$ cp $(find ~/ -maxdepth 1 -type f -name "r?.txt") ~/violet

"find ~/ -maxdepth 1 -type f -name “r?.txt” -exec cp {}"可以这样解读

1
2
3
4
5
6
7
find ~/ -maxdepth 1 -type f -name "r?.txt" -exec cp {} ~/violet \;
find ~/ -maxdepth 1 -type f -name "r?.txt" =
/root/r1.txt
/root/r2.txt
/root/r3.txt
/root/r4.txt
/root/r5.txt

辨析几个配合xargs命令的区别

1
2
3
$ find / -type f -name "*.txt" | xargs cp /violet
$ find / -type f -name "*.txt" | xargs -i cp {} /violet
$ find / -type f -name "*.txt" | xargs cp -t /violet

假设"find / -type f -name “*.txt”"的输出结果为

1
2
3
/root/r1.txt
/root/r2.txt
/root/r3.txt

那么"find / -type f -name “*.txt” | xargs cp /violet"等价于

1
cp /root/r1.txt /root/r2.txt /root/r3.txt /violet

而"find / -type f -name “*.txt” | xargs -i cp {} /violet"等价于

1
cp /root/r1.txt /violet; cp /root/r2.txt /violet; cp /root/r3.txt /violet

“{}“可以理解为一个具有"行处理能力"的占位符,即一次操作(如cp)只允许输入一行参数进行占位。“cp {} /violet”,第一次允许”/root/r1.txt"进入{},由此"cp {} /violet”->“cp /root/r1.txt /violet”。

"find / -type f -name “*.txt” | xargs cp -t /violet"中

-t - cp命令选项中的’-t’代表–target-directory=DIRECTORY。因此-t复制的目标目录。

What does “{} ;” mean in the find command? - Ask Ubuntu
https://askubuntu.com/questions/339015/what-does-mean-in-the-find-command