00. 目录
01. 命令概述
find 查找目录和文件
Linux下find命令在目录结构中搜索文件,并执行指定的操作。Linux下find命令提供了相当多的查找条件,功能很强大。由于find具有强大的功能,所以它的选项也很多,其中大部分选项都值得我们花时间来了解一下。即使系统中含有网络文件系统( NFS),find命令在该文件系统中同样有效,只你具有相应的权限。 在运行一个非常消耗资源的find命令时,很多人都倾向于把它放在后台执行,因为遍历一个大的文件系统可能会花费很长的时间(这里是指30G字节以上的文件系统)。
02. 命令格式
find [路径] [选项] [参数] [输出形式]
03. 常用选项
命令参数:
pathname: find命令所查找的目录路径。例如用.来表示当前目录,用/来表示系统根目录。
-print: find命令将匹配的文件输出到标准输出。
-exec: find命令对匹配的文件执行该参数所给出的shell命令。相应命令的形式为'command' { } \;,注意{ }和\;之间的空格。
-ok: 和-exec的作用相同,只不过以一种更为安全的模式来执行该参数所给出的shell命令,在执行每一个命令之前,都会给出提示,让用户来确定是否执行。
命令选项:
-name 按照文件名查找文件。
-perm 按照文件权限来查找文件。
-prune 使用这一选项可以使find命令不在当前指定的目录中查找,如果同时使用-depth选项,那么-prune将被find命令忽略。
-user 按照文件属主来查找文件。
-group 按照文件所属的组来查找文件。
-mtime -n +n 按照文件的更改时间来查找文件, - n表示文件更改时间距现在n天以内,+ n表示文件更改时间距现在n天以前。find命令还有-atime和-ctime 选项,但它们都和-m time选项。
-nogroup 查找无有效所属组的文件,即该文件所属的组在/etc/groups中不存在。
-nouser 查找无有效属主的文件,即该文件的属主在/etc/passwd中不存在。
-newer file1 ! file2 查找更改时间比文件file1新但比文件file2旧的文件。
-type 查找某一类型的文件,诸如:
b - 块设备文件。
d - 目录。
c - 字符设备文件。
p - 管道文件。
l - 符号链接文件。
f - 普通文件。
-size n:[c] 查找文件长度为n块的文件,带有c时表示文件长度以字节计。-depth:在查找文件时,首先查找当前目录中的文件,然后再在其子目录中查找。
-fstype:查找位于某一类型文件系统中的文件,这些文件系统类型通常可以在配置文件/etc/fstab中找到,该配置文件中包含了本系统中有关文件系统的信息。
-mount:在查找文件时不跨越文件系统mount点。
-follow:如果find命令遇到符号链接文件,就跟踪至链接所指向的文件。
-cpio:对匹配的文件使用cpio命令,将这些文件备份到磁带设备中。
另外,下面三个的区别:
-amin n 查找系统中最后N分钟访问的文件
-atime n 查找系统中最后n*24小时访问的文件
-cmin n 查找系统中最后N分钟被改变文件状态的文件
-ctime n 查找系统中最后n*24小时被改变文件状态的文件
-mmin n 查找系统中最后N分钟被改变文件数据的文件
-mtime n 查找系统中最后n*24小时被改变文件数据的文件
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
04. 参考示例
4.1 列出当前目录及子目录下所有文件和文件夹
[deng@localhost test]$ find
.
./test2
./test
./file
- 1
- 2
- 3
- 4
- 5
或者
[deng@localhost test]$ find .
.
./test2
./test
./file
[deng@localhost test]$
- 1
- 2
- 3
- 4
- 5
- 6
4.2 在/home
目录下查找以.txt结尾的文件名
[deng@localhost test]$ find /home -name "*.txt"
/home/deng/.cache/tracker/db-version.txt
/home/deng/.cache/tracker/db-locale.txt
/home/deng/.cache/tracker/parser-sha1.txt
/home/deng/.cache/tracker/locale-for-miner-user-guides.txt
/home/deng/.cache/tracker/locale-for-miner-apps.txt
- 1
- 2
- 3
- 4
- 5
- 6
4.3 在/home
目录下查找以.txt结尾的文件名,忽略大小写
[deng@localhost test]$ find /home -iname "*.txt"
/home/deng/.cache/tracker/db-version.txt
/home/deng/.cache/tracker/db-locale.txt
/home/deng/.cache/tracker/parser-sha1.txt
- 1
- 2
- 3
- 4
4.4 当前目录及子目录下查找所有以.txt和.pdf结尾的文件
[deng@localhost test]$ find ./ -name "*.txt" -o -name "*.pdf"
- 1
4.5 匹配文件路径或者文件
[root@localhost ~]# find /usr -path "*local"
/usr/bin/aclocal
/usr/bin/abrt-action-analyze-ccpp-local
/usr/share/doc/automake-1.13.4/README.aclocal
/usr/share/doc/postfix-2.10.1/examples/qmail-local
/usr/share/aclocal
/usr/libexec/postfix/local
/usr/local
/usr/src/kernels/3.10.0-862.el7.x86_64/include/config/x86/local
[root@localhost ~]#
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
4.6 基于正则表达式匹配文件路径
[root@localhost ~]# find ./ -regex ".*\(\.txt\|\.pdf\)$"
./.cache/tracker/db-version.txt
./.cache/tracker/db-locale.txt
./.cache/tracker/parser-sha1.txt
- 1
- 2
- 3
- 4
4.7 基于正则表达式匹配文件路径,忽略大小写
[root@localhost ~]# find ./ -iregex ".*\(\.txt\|\.pdf\)$"
./.cache/tracker/db-version.txt
./.cache/tracker/db-locale.txt
./.cache/tracker/parser-sha1.txt
./.cache/tracker/locale-for-miner-user-guides.txt
./.cache/tracker/locale-for-miner-apps.txt
- 1
- 2
- 3
- 4
- 5
- 6
4.8 找出当前目录下不是以.txt结尾的文件
[root@localhost ~]# find ./ ! -name "*.txt"
- 1
4.9 根据类型查找对应的文件
类型参数列表:
- f 普通文件
- l 符号连接
- d 目录
- c 字符设备
- b 块设备
- s 套接字
- p Fifo
查找当前目录下所有的普通文件
[root@localhost ~]# find ./ -type f
- 1
4.10 向下最大深度限制为3
[root@localhost share]# find ./ -maxdepth 3 -type f
- 1
4.11 搜索出深度距离当前目录至少2个子目录的所有文件
[root@localhost share]# find ./ -mindepth 2 -type f
./5th/1client/ItcastLog.cpp
./5th/1client/ItcastLog.h
./5th/1client/TcpServer.cpp
- 1
- 2
- 3
- 4
4.12 根据文件时间戳进行搜索
UNIX/Linux文件系统每个文件都有三种时间戳:
- 访问时间(-atime/天,-amin/分钟):用户最近一次访问时间。
- 修改时间(-mtime/天,-mmin/分钟):文件内容最后一次修改时间。
- 变化时间(-ctime/天,-cmin/分钟):文件属性(例如权限等)最后一次修改时间。
搜索最近七天内被访问过的所有文件
[deng@localhost share]$ find ./ -type f -atime -7
./5th/1client/ItcastLog.cpp
./5th/1client/ItcastLog.h
- 1
- 2
- 3
搜索恰好在七天前被访问过的所有文件
[deng@localhost share]$ find ./ -type f -atime 7
- 1
搜索超过七天内被访问过的所有文件
[deng@localhost share]$ find ./ -type f -atime +7
- 1
搜索访问时间超过10分钟的所有文件
[deng@localhost share]$ find ./ -type f -amin +10
- 1
找出比login.log修改时间更长的所有文件
[deng@localhost share]$ find ./ -type f -newer login.log
- 1
4.13 根据文件大小进行匹配
find ./ -type f -size 文件大小单元
文件大小单位
- b —— 块(512字节)
- c —— 字节
- w —— 字(2字节)
- k —— 千字节
- M —— 兆字节
- G —— 吉字节
搜索大于10KB的文件
[root@localhost /]# find / -type f -size +10k
- 1
搜索小于10KB的文件
[root@localhost /]# find / -type f -size -10k
- 1
搜索等于10KB的文件
[root@localhost /]# find / -type f -size 10k
- 1
4.14 删除当前目录下所有.txt文件
[root@localhost /]# find . -type f -name "*.txt" -delete
- 1
4.15 根据文件权限进行匹配
当前目录下搜索出权限为777的文件
[root@localhost /]# find ./ -type f -perm 777
- 1
找出当前目录下权限不是644的txt文件
[root@localhost /]# find ./ -type f -name "*.txt" ! -perm 644
- 1
找出当前目录用户deng拥有的所有文件
[root@localhost /]# find ./ -type f -user deng
- 1
找出当前目录用户组deng拥有的所有文件
[root@localhost /]# find ./ -type f -group deng
- 1
05. 附录
系统属性部件syspara_lite负责提供获取与设置操作系统相关的系统属性,包括默认系统属性、OEM厂商系统属性和自定义系统属性。为满足OpenHarmony产品兼容性规范,产品解决方案需要实现获取设备信息的接口,如:产品名、品牌名、厂家名等,同时提供设置/读取系统属性的接口。在《移植案例与原理 - startup子系统之syspara_lite系统属性部件》中,我们介绍了移植开发板时如何适配系统属性部件syspara_lite,并介绍了相关的运行机制原理。本文主要介绍如何使用syspara_lite系统属性部件的接口,然后详细介绍下syspara_lite系统属性部件的各个接口。
1、系统参数syspara_lite部件使用例子
下面是系统参数syspara_lite部件使用例子,来自https://gitee.com/openharmony/startup_syspara_lite。⑴处获取设备类型,⑵处获取厂商名称,⑶处获取品牌名称。其他系统属性接口调用的例子类似,详细的接口说明下文会提供。
⑴ char* value1 = GetDeviceType();
printf("Device type =%s\n", value1);
free(value1);
⑵ char* value2 = GetManufacture();
printf("Manufacture =%s\n", value2);
free(value2);
⑶ char* value3 = GetBrand();
printf("GetBrand =%s\n", value3);
free(value3);
class="hljs-button signin active" data-title="登录复制" data-report-click="{"spm":"1001.2101.3001.4334"}">
评论记录:
回复评论: