Linux file search tool find

1. Common software for file search tools

locate:

The locate command is actually another way of writing find -name, but it is much faster than the latter because it does not search a specific directory, but a database /var/lib/locatedb, which contains all local file information . The Linux system automatically creates this database and automatically updates it once a day, so the latest changed files cannot be found using the locate command. To avoid this situation, you can use the updatedb command to manually update the database before using locate.

find:

The find command is used to find files in the specified directory, and by traversing the specified target directory, find files that meet the specified attributes in real time. Can be combined with regular expressions (REGular EXPressio) to match patterns.

2. Common parameters and usage of software

locate:

Use the format: locate [OPTION]… PATTERN…

Common parameters:

-i: ignore case

wKiom1bgPWGTjPsyAABDnEiqYOI752.png

Example:

Filter files starting with sh under /etc/

# locate /etc/sh
/etc/shadow
/etc/shadow-
/etc/shells

find:
find [OPTIONS] [search path] [find condition] [processing action]

Search path: the default is the current path;

Search condition: The default is all files under the specified path;

Processing action: The default is to print to the screen;

1. Find the path

The default is the current path, you can specify any path in the system, but it is not recommended to use /

2. Search conditions

(1) Basic search conditions

a, user, user group and file name search

-name "filename": The filename supports globbing

glob: file name wildcard; quick reference to multiple files; overall file name matching degree detection;

Metacharacters: A matching pattern can be written based on metacharacters;

*: matches any character of any length;

?: matches any single character;

[ ]: matches any single character in the specified set;

[az], [AZ]: Characters are not case-sensitive;

[0-9]

[a-z0-9]

[[:upper:]]: all uppercase letters;

[[:lower:]]: all lowercase letters;

[[:digit:]]: all digits;

[[:alpha:]]: all letters;

[[:alnum:]]:所有字母和数字;

[[:space:]]:空白字符;

[[:punct:]]:标点符号;

[^ ]:匹配指定集合外的任意单个字符;

实例:

# find /etc -name "passwd*"

-iname “文件名” 不区分大小写

实例:

# find /tmp -iname "*yum*"
/tmp/yum.log
/tmp/YUM.LOG
# find /tmp -name "*yum*"
/tmp/yum.log

-user USERNAME:根据属主查找

-group USERNAME:根据属组查找

实例:

# find /var -user root -o -group mail

-uid UID:根据指定UID查找

-gid GID:根据指定GID查找

实例:

# find /tmp -uid 3323 -ls
2346806    0 -rw-rw-r--   1 mageedu  mageedu         0 Mar 10 09:36 /tmp/mageedu
2346812    0 -rw-rw-r--   1 mageedu  mageedu         0 Mar 10 09:36 /tmp/testfile

-nouser:查找没有属主的文件

-nogroup:查找没有属组的文件需

-nouser与-nogroup参数主要用在当公司员工离职后,虽然账户删除,但是他所有权限的文件还存在,并且以UID的形式来标识属主与属组,既当有uid有这个用户相同时就会可以查看此文件,所以需要删除此类文件。

实例:

# find /tmp -nouser -nogroup -ls
2346806    0 -rw-rw-r--   1 3323     3323            0 Mar 10 09:36 /tmp/mageedu
2346812    0 -rw-rw-r--   1 3323     3323            0 Mar 10 09:36 /tmp/testfile

b、文件类型查找

-type TYPE: 根据文件类型查找

f: 普通文件

d: 目录文件

l: 符号链接文件

b: 块设备

c: 字符设备文件

p: 命令名管道文件

s: 套接字文

实例:

# find /tmp -type d -ls

c、文件大小

-size [+|-]#UNIT:

常用单位有:k, M, G

#UNIT: #-1 < x <= #

2k指1k-2k

-#UNIT:x <= #-1

-2k:指0k-1k

+#UNIT: x > #

+2k:指2k到正无穷

实例:

# find /etc/ -type f -size +1

d、时间戳查找

以“天”为单位

-atime [+|-]#

-mtime [+|-]#

-ctime [+|-]#

#: 例如:今天为3月10日,3就是3月7日到3月8日(注意天是24小时制)

-#: 例如:今天为3月10日,-3就是三天内指,3月7日的现在时间到现在的时间。

+#: 例如:今天为3月10日,+3就是三天前之前指,3月7日前

以“分钟”为单位

-amin [+|-]#

-mmin

-cmin

实例:

# find /etc -not -user root -not -user hadoop -mtime -7
# find / -nouser -nogroup -atime -3

e、权限查找

根据权限查找:

-perm [+|-]MODE

MODE: 与MODE精确匹配

+MODE:任何一类用户的权限只要能包含对其指定的任何一位权限即可;

-MODE:每一类用户的权限都包含对其指定的所有权限;

#这里是指包含,例如:-444 指在属主、属组、其他用户中只要包含读权限就可以。

实例:

# find /etc/init.d/ -perm -113  #此处指所有用户都有执行权限,其他用户有写权限的文件或目录
# find /etc/ -type f -not -perm +222 #此处指所有用户都没有写权限

(2)组合查找条件

组合查找条件:

与:-a, 查找条件1 -a 查找条件2 -a …

所有条件必须同时满足

或:-o, 查找条件1 -o 查找条件2 -o …

满足其中一个条件即可

非:-not, !

-not 查找条件

实例:

# find / \( -nouser -o -nogroup \) -a -atime -3
# find /etc/ -not \( -user root -o -user hadoop \) -a -mtime -7

3、处理动作

-print: 默认动作,打印至屏幕;

-ls: 显示找到的文件的详细属性;

-exec COMMAND {} \;

-ok COMMAND {} \;

#exec与ok的区别:ok会提供交互式,让你确认。而exec则不需要;

实例:

# find /tmp -iname "*yum*" -type f -ls
   134    0 -rw-r--r--   1 root     root            0 Mar 10 09:32 /tmp/yum.log
   137    0 -rw-r--r--   1 root     root            0 Mar 10 09:32 /tmp/YUM.LOG

# find /tmp -iname "*yum*" -type f -exec ls -l {} \;
-rw-r--r-- 1 root root 0 Mar 10 09:32 /tmp/yum.log
-rw-r--r-- 1 root root 0 Mar 10 09:32 /tmp/YUM.LOG

# find /tmp -iname "*yum*" -type f -exec echo {} >>/tmp/test.file \;

# find /tmp -iname "*yum*" -type f -ok echo {} >>/tmp/test.file \;
< echo ... /tmp/yum.log > ? y
< echo ... /tmp/YUM.LOG > ? y

 

 

http://www.techug.com/linux-find

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326638414&siteId=291194637