▼locate [OPTION] PATTERN非实时搜索
1. Locate
[OPTION]
-i `不区分大小写`
-n NUM `显示前NUM行`
-r `使用正则表达式`
`Locate命令搜索的数据库`
[root@CentOS7 ~]$ll /var/lib/mlocate/mlocate.db
-rw-r-----. 1 root slocate 2831206 May 5 19:39 /var/lib/mlocate/mlocate.db
`立即更新Locate数据库`
[root@CentOS7 ~]$updatedb
`示例`
[root@CentOS7 script]$locate -r '\.jpg$'
/usr/lib64/pygtk/2.0/demos/images/background.jpg
/usr/share/backgrounds/day.jpg
/usr/share/backgrounds/default.jpg
▼Find [OPTION] [/路径] [条件] [动作]实时搜索文件
[OPTION]
-ls `显示属性`
●条件
1. 文件属性条件
命令 |
用途 |
-regex PATTERN |
正则表达式,完整路径 |
``maxdepth NUM` |
最大目录深度 |
``mindepth NUM` |
最小目录深度 |
-depth |
先文件后目录 |
-name FILE |
查文件名,支持glob |
iname FILE |
查文件名,不区分大小写 |
samefile name |
相同inode号的文件 |
links NUM |
链接数为NUM的文件 |
2. 属主/组条件
命令 |
用途 |
-user USERNAME |
指定用户UID的文件 |
-group GROUPNAME |
指定组GID的文件 |
-uid USERID |
指定UID号的文件 |
-gid GROUPID |
指定GID号的文件 |
-nouser |
没有属主的文件 |
nogroup |
没有属组的文件 |
3. 文件类型条件
命令 |
用途 |
-type TYPE |
根据文件类型查找 |
f |
普通文件 |
d |
目录 |
l |
符号链接文件 |
s |
套接字文件 |
b |
块设备文件 |
c |
字符设备文件 |
p |
管道文件 |
-empty |
空文件 |
-type d -empty |
空目录 |
4. 逻辑条件
命令 |
用途 |
-a |
与 |
-o |
或 |
-not 或 ! |
非 |
`示例`
[root@CentOS7 script]$find -not \( -user wang -o -user tom \)
5. 文件大小查找条件
命令 |
用途 |
-size [±]SIZE |
单位k , M , G , c(byte) |
SIZE |
( SIZE-1 , SIZE ], 6k 表示为(5k,6k] |
-SIZE |
[ 0 , SIZE-1 ]-6k 表示 [0,5k] |
+SIZE |
( SIZE , ∞ )+6k 表示 (6k,∞) |
示例 : find -size [±]SIZE
6. 文件时间戳条件
分类
命令 |
用途 |
-atime |
访问时间 |
-mtime |
修改时间 |
-ctime |
状态修改时间 |
以分钟为单位 |
|
-amin |
访问时间 |
-mmin |
修改时间 |
cmin |
状态修改时间 |
命令格式
命令 |
用途 |
-atime [±]TIMES |
单位:天 |
TIMES |
[ TIMES,TIMES+1 ) |
-TIMES |
[ TIMES+1,∞ ) |
+TIMES |
[ 0,TIMES ) |
示例 : find -atime [±]TIMES
7. 权限查找条件
分类
命令 |
用途 |
-perm [+|-]MODE |
权限查找 |
MODE |
精确权限匹配 |
+MODE |
任意一类权限匹配即可(CentOS7 /MODE ) |
-MODE |
每一类对象都必需同时拥有指定权限,0 表示不关注(MODE中的0 ) |
详解
`示例文件`
96 0 -rwxrwxrwx 1 root root 0 May 5 21:56 ./f1
`find -perm MODE : 精确匹配权限为`755`的文件`
[root@CentOS7 dir1]$find -perm 755 -ls
[root@CentOS7 dir1]
`find -perm -MODE : 匹配权限大于或等于`755`的文件`
[root@CentOS7 dir1]$find -perm -755 -ls
96 0 -rwxrwxrwx 1 root root 0 May 5 21:56 ./f1
`find -perm /MODE : 匹配权限为`u=7或g=5或o=5`的文件`
[root@CentOS7 dir1]$find -perm /755 -ls
96 0 -rwxrwxrwx 1 root root 0 May 5 21:56 ./f1
`特殊权限:SUID,SGID,Sticky`
[root@CentOS7 dir1]$find -perm /7755
●处理动作
命令 |
用途 |
-print |
默认 |
-ls |
对找到的文件执行ls -l |
-delete |
删除查找到的文件 |
fls FILE |
所有文件长格式保存到FILE |
|
|
-ok COMMAND {} \; |
对查找到的内容执行COMMAND |
|
-ok 头 |
|
COMMAND 命令 |
|
{} 对查找到的内容的引用 |
|
\; 结束 |
|
|
-exec COMMAND {} \; |
对查找到的内容执行COMMAND |
find 示例
1. 查找 /root 下后缀为txt的文件
`find /root -name "*.txt"`
2. 查找属主为wang但不是root组的文件
`find -user wang -not -group root`
4. 查找属主不是wang也不是chen的文件
`find -not \( -user wang -o -user chen \)`
5. /tmp 下查找属主不是wang也不是chen的文件并长格式显示
`find /tmp -not \(-user root -o -name "f*" \) -ls`
6. /etc 下排除 /etc/sane.d 查找后缀conf的文件
`find /etc -path '/etc/sane.d' -a -prune -o -name "*.conf" `
7. /etc下排除/etc/sane.d和/etc/fonts查找后缀conf的文件
`find /etc \( -path "/etc/sane.d" -o -path "/etc/fonts" \) -a -prune -o -name "*.conf" `
8. 查找后缀conf的文件并复制到当前文件夹下加.orig后缀
`find -name "*.conf" -exec cp {} {}.orig \;`
9.查找修改状态时间在[0,3)天的文件并删除
`find /tmp -ctime +3 -user wang -ok rm {} \;`
10.查找other权限>2的文件并去写权限
`find ~ -perm -002 -exec chmod o-w {} \;`
11./data查找权限为644且后缀为.sh的普通文件并修改权限为755
`find /data -type f -perm 644 -name "*.sh" -exec chmod 755 {} \;`
12.查找/home下的目录
`find /home -type d -ls`
xargs 参数替换
xargs用于产生某个命令的参数,xargs可以读入stdin的数据,并且以空格符或回车符将stdin的数据分隔成arguments
`在文件很多的情况下,使用rm无法直接删除`
ls f* |xargs rm
find /sbin/ -perm +700 |ls -l
find /bin/ -perm /7000 | xargs ls -l
find /bin/ -perm -7000 | xargs ls -l
`问题`
[root@CentOS7 dir1]$find -name "*.txt" | xargs rm
rm: cannot remove ‘./f’: No such file or directory
rm: cannot remove ‘1.txt’: No such file or directory
`使用\0输出并分隔`
[root@CentOS7 dir1]$find -name "*.txt" -print0 |xargs -0 rm
[root@CentOS7 dir1]$
练习