【国产化】Linux 命令速学之 find 命令

createh54个月前 (01-11)技术教程25

1. find 简介

find 命令用于查找文件和目录,支持按名称、大小、修改时间、内容等进行搜索,是 Linux 命令行工具包的一个关键组件。KylinUOSCentOS Linux 版本通用。


2. 语法

find [path] [options] [expression]

常用参数如下表:

Command Options

Description

-name pattern

按文件名查找,区分大小写

-iname pattern

按文件名查找,不区分大小写

-type type

按文件类型查找

-xtype type

The same as -type unless the file is a symbolic link.

-size [+/-]n

按文件大小查找, +n finds larger files, -n finds smaller files. nmeasures size in characters.

-mtime n

按文件修改时间查找. n represents the number of days ago.

-maxdepth level

指定最大查找目录深度

-mindepth level

指定最小查找目录深度

-empty

查找空文件和目录

-print

显示到屏幕,默认处理动作

-delete

删除满足条件的文件

-exec command {} \;

对找到的每个文件执行命令

-execdir command {} \;

Executes a command on each file found, from the directory containing the matched file.


3. 常用命令示例

3.1. 按名称查找

[root@uos /]# find ~ -name readme.txt
/root/readme.txt

-name 是区分大小写的,如果需要忽略大小写则用 -iname

除了直接使用完整的名字,还可以使用匹配模式:

[root@uos /]# find /data -name *.txt
/data/demo1.txt
/data/demo6.txt


3.2. 按权限查找

find -perm [/|-] MODE
  • MODE:精确匹配(u,g,o)三类权限
  • /MODE:“或”关系,三类对象只要有一类对象中的三个权限位匹配一位即可,比如/444 表示三个只要有一 个“有”读权限
  • -MODE:“与”关系,三类对象分别有对应权限,是否还有其他权限不在意,比如-444,三个都要“有”读权限但是否还有写或者执行权限则不关心

需要注意的是 “/” 或者 “-” 后面要匹配的权限为0时,表示不关心该对象的权限,如440,表示所属组有无权限都不关心。

[root@centos data]# find /data -perm 664
/data/demo3.json
/data/demo1.txt


3.3. 按类型查找

find -type type

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)
  • 查看目录结构,查看所有目录
# 查看目录结构,查看所有目录
[root@centos data]# find /data -type d
/data
/data/dir1
/data/dir1/dir3
/data/dir1/dir3/dir4
/data/dir2
  • 查看块设备文件
# 查看块设备文件
[root@centos /]# find /dev -type b
/dev/dm-1
/dev/dm-0
/dev/sr0
/dev/sda2
/dev/sda1
/dev/sda


3.4. 按时间查找

以天为单位(time):
    -atime [+|-]N:访问时间
           +: 表示(N+1)天之前被访问过;
           -: 表示 N 天之内被访问过;
           无符号:表示(N +1)> x >= N 天的时间段被访问过;    
    -mtime [+|-]N: 修改时间
    -ctime [+|-]N: 创建时间

以分钟为单位(min):
    -amin [+|-]N: 访问时间
    -mmin [+|-]N: 修改时间
    -cmin [+|-]N: 创建时间
# 查找近 7 天修改的文件
find /path/to/search -mtime -7

# 查找 3 天内修改的文件,查找深度为 2 层
[root@centos /]# find -maxdepth 2 -type f -mtime -3
./boot/initramfs-3.10.0-1160.108.1.el7.x86_64kdump.img
./proc/fb
...
./data/demo1.txt
./data/demo2.txt


3.5. 按大小查找

单位为c/k/M/G,常用于查找大文件。

# 查找大于 100k 的文件,(100k, )
find -size +100k
# 查找小于 99k 的文件, [0, 99k)
find -size -100K
# 查找99k ~ 100k的文件,[99k, 100k]
find -size 100k

[root@centos /]# find /usr -size +50M
/usr/bin/containerd
/usr/bin/dockerd
/usr/lib/locale/locale-archive

/proc/kcore 这是一个虚拟文件,由 virtual procfs filesystem 提供,并不是一个真实的文件。实际上整个 /proc 都是虚拟的,里面的文件并不实际占用真实的磁盘空间。Linux 内核会设置 kcore 的大小为系统所能支持的最大内存大小,约为 128TB

[root@centos /]# find -size +1G
./proc/kcore
[root@centos /]# ls -lh /proc/kcore
-r--------. 1 root root 128T Feb 20 03:10 /proc/kcore


3.6. 查找空文件和空目录

[root@centos data]# find /data -empty
/data/demo5.dat
/data/dir1/eg1.json
/data/dir2


3.7. 查找并交互删除

[root@centos /]# ls /data
demo1.txt  demo2.md  demo3.json  demo4.xml  demo5.dat  demo6.txt
[root@centos /]# find /data -name *.txt -exec rm -i {} \;
rm: remove regular empty file ‘/data/demo6.txt’? y
rm: remove regular empty file ‘/data/demo1.txt’? y
[root@centos /]# ls /data
demo2.md  demo3.json  demo4.xml  demo5.dat


3.8. 统计代码行

[root@centos data]# find -name "*.java" -exec cat {} + | wc -l
14


3.9. 从多个文件中查找指定文本

# 显示当前路径下的txt文件中包含 hello 的行内容
[root@centos data]# find -type f -name "*.txt" -exec grep "hello" {} \;
hello, lucky boy!
hello linux
this is a sample of hello line.


3.10. 查找包含指定文本的文件

[root@centos data]# find -type f -name "*.txt" -exec grep -l "hello" {} \;
./dir1/eg2.txt
./demo1.txt
./demo2.txt


3.11. 多条件查询

可以使用如下操作符:

  • -and, -a:与
  • -or, -o:或
  • -not!:非
# 查找普通文件和符号链接文件
[root@centos data]# find ./ -type f -o -type l

# 在当前目录查找 txt 和 md 文件
[root@centos data]# find ./ -name "*.txt" -o -name "*.md"

# 查找文件名带 info 的符号链接文件
[root@centos data]# find ./ -name "*info" -a -type l

# 查找非 json 文件
[root@centos data]# find ./ ! -name "*.json"

# 查找 10 分钟以内生成并且名字以 log_ 开头的的 .log 文件
[root@centos data]# find . -amin -10 -a -name "log_*.log"

相关文章

Java & Python 康威生命游戏 - 命令行版

制作背景高二的时候看霍金的《大设计》最后几页的时候看到里面提到了康威生命游戏,介绍了它的规则,感觉很有意思,但是在草稿纸上一点一点画,推演,实在是太麻烦了,于是我便想是否可以通过编程的方式实现康威生命...