Linux find命令的使用
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 asgrep
orsed
.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) specialc
- character (unbuffered) speciald
- directoryp
- named pipe (FIFO)f
- regular filel
- 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
- socketD
- door (Solaris)比如: “-type d”, “-type f”
举例
1 | find /root -name readme.txt # 在/root目录及其子目录下搜索"readme.txt" |
-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
: bytesw
: two-byte wordsk
: KilobytesM
: MegabytesG
: Gigabytes
在<file_size>中可以加入正号(‘+’)和负号(‘-’),分别表示大于和小于。
1 | find . -type f -size -1M |
“-1M"表示小于1MB的文件,”+1M"表示大于1MB的文件。对于要搜索文件大小在1MB和2MB之间的,可以这样搜索
1 | find . -type f -size +1M -size 2M |
按照时间查找
1 | +10: 10天以前的数据 |
-perm
- 根据文件权限查找数据
1 | find [path] -type f -perm [权限数字] |
find用于批量操作
配置xargs命令使用
xargs - 用于把输入的内容按行进行分割
通过以下例子理解xargs命令的作用
1 | touch r{0..6}.txt |
批量查找并删除
1 | find ~/ -type f -name "r?.txt" -delete |
批量查找并复制
1 | find ~/ -type f -name "r?.txt" -exec cp {} ~/violet \; |
举个例子
1 | [root@localhost ~]# find ~/ -maxdepth 1 -type f -name "r?.txt" |
"find ~/ -maxdepth 1 -type f -name “r?.txt”|xargs -i cp {} ~/violet"命令的图解如下:
批量查找并删除的其他方法
1 | find ~/ -maxdepth 1 -type f -name "r?.txt" -delete |
"find ~/ -maxdepth 1 -type f -name “r?.txt” -exec cp {}"可以这样解读
1 | find ~/ -maxdepth 1 -type f -name "r?.txt" -exec cp {} ~/violet \; |
辨析几个配合xargs命令的区别
1 | find / -type f -name "*.txt" | xargs cp /violet |
假设"find / -type f -name “*.txt”"的输出结果为
1 | /root/r1.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