linux文件查找详解

文件查找

介绍:在文件系统上查找符合条件的文件。

文件查找:

  • 非实时查找(数据库查找):locate
  • 实时查找:find

1、locate

  • locate 查询系统上预建的文件索引数据库 /var/lib/mlocate/mlocate.db
  • 索引的构建是在系统较为空闲时自动进行(周期性任务),执行updatedb可以更新数据库
  • 索引构建过程需要遍历整个根文件系统,很消耗资源
  • locate和updatedb命令来自于mlocate包

工作特点:

  • 查找速度快
  • 模糊查找
  • 非实时查找
  • 搜索的是文件的全路径,不仅仅是文件名
  • 可能只搜索用户具备读取和执行权限的目录

格式:

locate [OPTION]...[PATTERN]...

常用选项:

-i 不区分大小写的搜索
-n N 只列举前N个匹配项目
-r 使用基本正则表达式

范例:

#搜索名称或路径中包含"conf"的文件
locate conf

#使用Regex来搜索以".conf"结尾的文件
locate -r '\.conf$'

范例:locatedb创建数据库

dnf -y install mlocate
locate conf
updatedb
ll /var/lib/mlocate/mlocate.db
locate -n 3 conf

范例:文件新创建和删除,无法马上更新locate数据库

touch test.log
locate test.log
updatedb
locate test.log
touch test2.log
locate test2.log
rm -f test2.log
locate test2.log
[root@centos8 ~]#locate -n 10 -ir '\.CONF$'
/boot/loader/entries/5b85fc7444b240a992c42ce2a9f65db5-0-rescue.conf
/boot/loader/entries/5b85fc7444b240a992c42ce2a9f65db5-4.18.0-147.el8.x86_64.conf
/etc/autofs.conf
/etc/autofs_ldap_auth.conf
/etc/dracut.conf
/etc/fuse.conf
/etc/host.conf
/etc/idmapd.conf
/etc/kdump.conf
/etc/krb5.conf

2.find

find是实时查找工具,通过遍历指定路径完成文件查找

工作特点:

  • 查找速度略慢
  • 实时查找
  • 精确查找
  • 查找条件丰富
  • 可能只搜索用户具备的读取和执行权限目录

格式:

find [OPTION]…[查找路径] [查找条件] [处理动作]

查找路径:指定具体目标路径,默认为当前目录

查找条件:指定的查找标准,可以文件名、大小、类型、权限等标准进行;默认为找出指定路径下的所有文件。

处理动作:对符合条件的文件做操作,默认输出到屏幕

2.1 指定搜索目录层级

-maxdepth level 最大搜索目录深度,指定目录下的文件为第1级
-mindepth level 最小搜索目录深度

范例:
find /etc -maxdepth 2 -mindepth 2

2.2 对每个目录先处理目录内的文件,再处理目录本身

-depth
-d

范例:
tree /data/test
/data/test
├── f1.txt
├── f2.txt
└── test2
   └── test3
       ├── f3.txt
       └── f4.txt
4 directories, 2 files

[root@centos8 data]#find /data/test
/data/test
/data/test/f1.txt
/data/test/f2.txt
/data/test/test2
/data/test/test2/test3
/data/test/test2/test3/f3.txt
/data/test/test2/test3/f4.txt

[root@centos8 data]#find /data/test -depth
/data/test/f1.txt
/data/test/f2.txt
/data/test/test2/test3/f3.txt
/data/test/test2/test3/f4.txt
/data/test/test2/test3
/data/test/test2
/data/test

2.3 根据文件名和inode查找

-name "文件名称"        #支持使用glob,如:*,?,[],[^],通配符要加双引号引起来
-iname "文件名称"       #不区分字母大小写
-inum n                          #按inode号查找
-samefile name               #相同inode号的文件
-links n                             #链接数为n的文件
-regex "PATTERN"             #以PATTERN匹配整个文件路径,而非文件名称
范例:
find -name snow.png
find -iname snow.png
find / -name ".txt"
find /var -name "log*"

find -regex ".*\.txt$"
./scripts/b.txt
./f1.txt

2.4 根据属主、属组查找

-user USERNAME #查找属主为指定用户(UID)的文件
-group GRPNAME #查找属组为指定组(GID)的文件
-uid UserID #查找属主为指定的UID号的文件
-gid GroupID #查找属组为指定的GID号的文件
-nouser #查找没有属主的文件
-nogroup #查找没有属组的文件

2.5 根据文件类型查找

-type TYPE
TYPE可以是以下形式:
f: 普通文件
d: 目录文件
l: 符号链接文件
s:套接字文件
b: 块设备文件
c: 字符设备文件
p: 管道文件

范例:
#查看/home的目录
find /home –type d -ls

2.6 空文件或目录

-empty
范例:
find /app -type d -empty

2.7 组合条件

与:-a ,默认多个条件是与关系
或:-o
非:-not   !

范例:
[root@centos8 ~]#find /etc/ -type d -o -type l |wc -l
307
[root@centos8 ~]#find /etc/ -type d -o -type l -ls |wc -l
101
[root@centos8 ~]#find /etc/ \( -type d -o -type l \)   -ls |wc -l
307
德 摩根定律:
(非 A) 或 (非 B) = 非(A 且 B)
(非 A) 且 (非 B) = 非(A 或 B)
示例:
!A -a !B = !(A -o B)
!A -o !B = !(A -a B)

[root@centos8 data]#find ! \( -type d -a -empty \)| wc -l
56
[root@centos8 data]#find ! -type d -o ! -empty |wc -l
56
[mage@centos8 data]$find !  -user wang   ! -user mage
.
./script40
./script40/backup
./script40/backup/args.sh
./script40/backup/chook_rabbit.sh
./script40/backup/disk_check.sh
./script40/backup/ping.sh
./script40/backup/systeminfo.sh
./script40/backup/test_read.sh
./script40/backup/test.sh
./script40/if_bmi.sh
./script40/case_yesorno.sh
./script40/case_yesorno2.sh
./script40/b.txt
./f1.txt
./test
./test/f1.txt.link
./f1.txtlink
./test2

[root@centos8 home]#find ! \( -user wang -o -user mage \)
.
./xiaoming
./xiaoming/.bash_logout
./xiaoming/.bash_profile
./xiaoming/.bashrc
[root@centos8 home]#f
#找出/tmp目录下,属主不是root,且文件名不以f开头的文件
find /tmp \( -not -user root -a -not -name 'f*' \) -ls
find /tmp -not \( -user root -o -name 'f*' \) –ls

2.8 排除目录

#查找/etc/下,除/etc/security目录的其它所有.conf后缀的文件
find /etc -path '/etc/security' -a -prune -o -name "*.conf"
#查找/etc/下,除/etc/security和/etc/systemd,/etc/dbus-1三个目录的所有.conf后缀的文件
find /etc \( -path "/etc/security" -o -path "/etc/systemd"  -o -path "/etc/dbus-
1" \) -a -prune -o -name "*.conf"
#排除/proc和/sys目录
find / \( -path "/sys" -o -path "/proc" \) -a -prune -o -type f -a -mmin -1

2.9 根据文件大小来查找

shell下如何创建指定大小的临时文件?
/dev/zero 0输入设备文件,跟/dev/null一样的伪文件,它产生连续不断的null流(二进制的零流,不是ascii码),写入它的输出文件。作用是可以用它来创建一个指定大小的null文件也称为临时文件。
命令如下:

dd if=/dev/zero of=f1.img bs=1M count=100 //说明:创建一个名为f1.im的100M大小文件

-size [+|-] #UNIT #常用单位:k, M, G,c(byte),注意大小写敏感
 #UNIT: #表示(#-1, #],如:6k 表示(5k,6k]
-#UNIT #表示[0,#-1],如:-6k 表示[0,5k]
+#UNIT #表示(#,∞),如:+6k 表示(6k,∞)

解释说明:#UNIT参数可以看做一个坐标轴

例如 -6K 6K +6K 代表一个+有理数的取值范围。。
-6K 代表[0K ~ 5K ]  前半部 包含0k 和 5K
6K  代表(5K-6K)    中部  不含5K、6K
+6K  代表[6K ~ ∞)   后部 包含6K 到无穷大

范例: 查找根目录下 大于10g的文件
find /  -size +10G
[root@centos8 ~]#find / -size +10G
/proc/kcore
find: '/proc/25229/task/25229/fd/6': No such file or directory
find: '/proc/25229/task/25229/fdinfo/6': No such file or directory
find: '/proc/25229/fd/5': No such file or directory
find: '/proc/25229/fdinfo/5': No such file or directory
[root@centos8 ~]#ll -h /proc/kcore
-r-------- 1 root root 128T Dec 14  2020 /proc/kcore
[root@centos8 ~]#du -sh /proc/kcore
0 /proc/kcore

2.10 根据时间戳

#以"天"为单位
-atime [+|-]#
#表示[#,#+1)
+# #表示[#+1,∞]
-# #表示[0,#)
-mtime
-ctime
#以"分钟"为单位
-amin
-mmin
-cmin

linux文件查找详解

2.11 根据权限查找

-perm [/|-]MODE
MODE  #精确权限匹配

或者: /MODE #任何一类(u,g,o)对象的权限中只要能一位匹配即可,或关系,+ 从CentOS 7开始淘汰
并且: -MODE #每一类对象都必须同时拥有指定权限,与关系
0 表示不关注
说明:
find -perm 755 会匹配权限模式恰好是755的文件
只要当任意人有写权限时,find -perm /222就会匹配
只有当每个人都有写权限时,find -perm -222才会匹配
只有当其它人(other)有写权限时,find -perm -002才会匹配

2.12 正则表达式

-regextype type
   Changes the regular expression syntax understood by -regex and -iregex tests
which occur later on the command line.   Currently-implemented types are
emacs (this is the default), posix-awk, posix-basic, posix-egrep and posixextended.

-regex pattern
   File name matches regular expression pattern. This is a match on the whole
path, not a search.   For example, to match a file named ./fubar3', you can
use the regular expression .*bar.' or .*b.*3', but not .*r3'. The regular
expressions understood by find are by default Emacs Regular Expressions, but
this can be changed with the -regextype option.

范例:

find /you/find/dir -regextype posix-extended -regex "regex"

2.13 处理动作

-print:默认的处理动作,显示至屏幕
-ls:类似于对查找到的文件执行"ls -dils"命令格式输出
-fls file:查找到的所有文件的长格式信息保存至指定文件中,相当于 -ls > file
-delete:删除查找到的文件,慎用!
-ok COMMAND {} \; 对查找到的每个文件执行由COMMAND指定的命令,对于每个文件执行命令之前,都会
交互式要求用户确认
-exec COMMAND {} \; 对查找到的每个文件执行由COMMAND指定的命令
{}: 用于引用查找到的文件名称自身
范例:
#备份配置文件,添加.orig这个扩展名
find  -name  ".conf"  -exec  cp {} {}.orig \;
#提示删除存在时间超过3天以上的joe的临时文件
find /tmp -ctime +3 -user joe -ok rm {} \;
#在主目录中寻找可被其它用户写入的文件
find ~ -perm -002  -exec chmod o-w {} \;
#查找/data下的权限为644,后缀为sh的普通文件,增加执行权限
find /data –type f -perm 644  -name "*.sh" –exec chmod 755 {} \;

3.参数替换 xargs

一句话来说,xargs 用来接收标准输入或者前面指令的结果 动态输入给后面参数或者输出。也可作为传参

详解:

由于很多命令不支持管道|来传递参数, xargs用于产生某个命令的参数,xargs 可以读入 stdin 的数

据,并且以空格符或回车符将 stdin 的数据分隔成为参数

另外,许多命令不能接受过多参数,命令执行可能会失败,xargs 可以解决

注意:文件名或者是其他意义的名词内含有空格符的情况

find 经常和 xargs 命令进行组合,形式如下:

find | xargs COMMAND

范例:

#显示10个数字
[root@centos8 ~]#seq 10 | xargs
1 2 3 4 5 6 7 8 9 10
#删除当前目录下的大量文件
ls | xargs   rm
#
find  -name "*.sh" | xargs ls -Sl
[root@centos8 data]#echo {1..10} |xargs
1 2 3 4 5 6 7 8 9 10
[root@centos8 data]#echo {1..10} |xargs -n1
123456789
10
[root@centos8 data]#echo {1..10} |xargs -n2
1 2 3 4 5 6 7 8 9 10
#批量创建和删除用户
echo user{1..10} |xargs -n1 useradd
echo user{1..100} | xargs -n1 userdel -r

#这个命令是错误的
find /sbin/ -perm /700 | ls -l      
#查找有特殊权限的文件,并排序
find /bin/ -perm /7000 | xargs ls -Sl  
#此命令和上面有何区别?
find /bin/ -perm -7000 | xargs ls -Sl  
#以字符nul分隔
find -type f -name "*.txt" -print0 | xargs -0 rm
#并发执行多个进程
seq 100 |xargs -i -P10 wget -P /data   http://10.0.0.8/{}.html

Xargs 的缺点:

假如:touch 几个文件, touch a b;touch ‘a b’;

此时当前目录下就是产生3个文件,如下:ls

a b a b

find -type f | xargs rm //此时是无法进行删除操作的

原因为:默认空格是作为xargs的分隔符存在的,a b文件中含有空格,故报错!

那么以上问题如何解决呢?

可以使用ASCII中的0字符来作为切割符号:print0 ==>null

find -type f -print0 |xargs -0 rm

下载bilibili视频

#并行下载bilibili视频
yum install python3-pip -y
pip3 install you-get
seq 10 | xargs -i -P3 you-get https://www.bilibili.com/video/BV1HZ4y1p7Bf?p={}

以上范例依赖Python环境,需要先安装好Python环境。

练习

1、查找/var目录下属主为root,且属组为mail的所有文件
2、查找/var目录下不属于root、lp、gdm的所有文件
3、查找/var目录下最近一周内其内容修改过,同时属主不为root,也不是postfix的文件
4、查找当前系统上没有属主或属组,且最近一个周内曾被访问过的文件
5、查找/etc目录下大于1M且类型为普通文件的所有文件
6、查找/etc目录下所有用户都没有写权限的文件
7、查找/etc目录下至少有一类用户没有执行权限的文件
8、查找/etc/init.d目录下,所有用户都有执行权限,且其它用户有写权限的文件

答案:
1、find /var -user root -a -group mail
2、find /var/ !\( -user root -o -user lp -o -user -gdm \)
3、find /var -mtime -7 -a -nouser root -a -nouser postfix

find /var/ -mtime -7 -a -not \( -user root -o -user postfix \)
4、find /. -nouser -o -nogroup -atime -7
5、find /etc -size +1M -a -type f
6、find /etc  -not -perm /222 -a -not -type d -ls
7、find /etc ! -perm -111 -ls
8、find /etc/init.d -perm /113  -ls

linux文件查找详解

Original: https://www.cnblogs.com/cn-leoblog/p/15707078.html
Author: Leo’sblog
Title: linux文件查找详解

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/712300/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球