File Lookup in Linux systems
Keyword File Finder | Find | Locate
This article mainly describes the file lookup of the two commands--find and locate, as well as the compressed packaging commands--compress, GZIP,BZIP2,XZ and so on. Questions or suggestions can be sent to email [email protected]
Locate
The locate command is by querying a pre-built file index database on a Linux system, which, to be exact, is the default location of the database/var/lib/mlocate/ Mlocate.db, this database is usually updated periodically by the system, but can also be manually updated by the UpdateDB command, but it may take a long time period, especially if the file system occupancy rate is high.
Features of the Locate command lookup:
查找速度快模糊查找非实时查找
command format and common options:
locate [OPTION]... PATTERN... -h, --help 查看帮助文档 -i 忽略大小写搜索 -n N 最多显示N个匹配项目 -r, --regexp 支持进行基本正则表达式匹配,默认支持glob统配符 --regex 支持使用扩展正则表达式 -c 统计搜索到匹配的条目,但不输出具体条目
Find command
Find command Lookup has the following characteristics relative to the locate command
查询速度较快精确查找实时查找
Grammar:
[-H] [-L] [-P] [-D debugopts] [-Olevel] [path...][expression][-H] [-L] [-P] 三选项用来控制处理符号链接文件[-D] 控制dubug[-O] 查询,设置优化等级
The above 5 parameters are basically not used, if needed, the location must be before the path [path]
[OPTION]... [查找路径] [查找条件] [处理动作]
Find a path: Specify the directory you want to find, and if it is the current directory, use "." Replace
Find criteria: Specify the lookup criteria, which can be file name, size, permissions, etc.
Handling actions: Working with eligible files, default output to screen
The following details the search criteria:
Search by file name
-name, 直接查找文件名,支持glob风格查找-iname, 不区分大小写,支持glob风格查找-regex, 支持正则表达式查找文件名-iregex, 不区分大小写,支持正则表达式查找文件名注意:如果对glob不了解,可以用man 7 glob查看
Find based on file dependencies
-user, 根据属主查找-group, 根据属组查找-uid 根据属主id查找-gid 根据属组id查找-nouser 判断是否属于无属主文件-nogroup 判断是否属于无属组文件
Find by File type
-type
Common file types are
f 普通文件d 目录文件l 符号链接文件b 块文件c 字符设备文件p 管道文件s 套接字文件
Find by File size
-size[+|-] UNIT常用单位:k, M, G
Find by timestamp
以“天”为单位:-atime[+|-]-mtime-ctime以“分钟”为单位:-amin-mmin-cmin
Find based on permissions
modemode: 精确权限匹配-mode/mode: 任何一类用户的权限满足即可+mode: 已废弃这种用法
Handling actions
-print 输出至标准输出,默认的动作-ls 类似于对查找的文件执行"ls -l"命令,输出文件的详细信息-delete 删除查找到的文件-fls file 把查找到的所有文件的长格式信息保存到指定文件-ok CMD {} \; 执行CMD,执行前询问-exec CMD {} \; 执行CMD
Combination Test
The default between each condition is to connect with, and if you need to use another combination test, you must add
-a 与-o 或-not, ! 非
De Morgan Law
(非 P) 或 (非 Q) = 非(P 且 Q)(非 P) 且 (非 Q) = 非(P 或 Q)
Practice
1. Find the main root of the/var directory, and belong to all files or directories of mail;
# find /var -user root -group mail
2. Find all files or directories that do not belong to root, LP, or GDM in the/usr directory;
# find /usr -not -user root -a -not -user lp -a -not -user gdm# find /usr -not \( -user root -o -user lp -o -user gdm \)
3, Find/etc directory in the most Monday weeks of its content has been modified, and is not the root of the master, nor is it postfix files or directories;
# find /etc -mtime -7 -a -not -user root -a -not -user postfix# find /etc/ -mtime -7 -a -not \( -user root -o -user postfix \)
4. Find files or directories on the current system that are not owned by the master or group and have been visited in the last week;
# find / \( -nouser -o -nogroup \) -a -atime -7# find / \( -atime -7 -nouser -o -nogroup \) -ls
5. Find all files that are larger than 1M and type ordinary files in/etc directory;
# find /etc -size +1M -type f
6, look for all the users in/etc directory do not have permission to write files;
find /etc -not -perm 222 -type f -ls
File locating and locating in Linux system