Home  >  Article  >  Operation and Maintenance  >  File search command find in Linux

File search command find in Linux

齐天大圣
齐天大圣Original
2020-06-04 09:53:03153browse

The find command is one of my favorite commands. It can easily find the files I want to find. It can support too many methods to find, such as file name, file size, file type, etc. wait. Next, let’s take a look.

Syntax: find [search path] [options] [action]

Don’t have too many options in the find command. Today I will only talk about some frequently used options.

Search by file name

The option to search by file name is -name, which supports simple regular search.

For example, I know that locale.conf is saved in the /etc directory, but I have forgotten the specific path, then

# find /etc/ -name locale.conf/etc/locale.conf

I can find out the specific path of the file.

Now I want to know how many php files there are in a certain directory, then I can use the following command to complete it.

# find default -name *.php | wc -l122

Or option -o

Here is another option, -o, which means or, generally the default is between find options They all mean "and". Let's look at an example to find the total number of php or js files in a certain directory.

# find default -name *.php -o -name *.js | wc -l225

Search based on file type and directory depth

You need to use the -type option to find file types. Commonly used types include f (file), d(directory). Another option is introduced, -maxdepth indicates the maximum number of recursive directories.

# find ./  -maxdepth 1  -type d././default./default.bak

Negative option!

# find ./  -maxdepth 1 ! -type f././default./default.bak

Time-related lookup

Time-related options: There are -atime, -ctime and -mtime , using -mtime description
-mtime n n as a number, meaning files whose contents were changed "within one day" n days ago;
-mtime n List the file names whose contents were changed n days ago (excluding n days itself)
-mtime -n List the file names of files whose contents have been changed within n days (including n days themselves)
- newer file file is an existing file and lists the file names that are newer than file

  这个选项很有作用,比如进行数据定时备份时,只保留最近7天的数据,超过7天的自动删除就会用到该选项。注意+n表示n天之前,-n表示n天之内。

# find $bakdir -name "*.sql.bz2" -type f -mtime +7 -exec rm -rf {} \;

根据用户名、组来查找

与使用者或群组名称有关
-uid n   n 为数字,这个数字是使用者的帐号ID,亦即UID    
-gid n  n 为数字,这个数字是群组名称的ID,亦即GID    
-user name  name 为使用者帐号名称,例如dmtsai   
-group name  name 为群组名称,例如users ;    
-nouser 寻找文件的拥有者不存在 的人!    
-nogroup  寻找文件的拥有群组不存在于/etc/group 的文件!    

  查找某目录下,所有者不是www的文件有哪些。

find /home/wwwroot/default ! -user www | wc -l

根据文件大小查找

按文件大小查找使用-size选项,比如查找大于1M的文件,那么使用-size +1M,如果查找小于1K的,那么使用-size -1K

# find /home/wwwroot/default -size +1M
# find /home/wwwroot/default -size -1k

根据文件权限查找

  • -perm mode 搜寻文件权限『刚好等于』 mode 的文件,这个mode 为类似chmod的属性值,举例来说, -rwsr-xr-x 的属性为4755 !

  • -perm -mode    搜寻文件权限『必须要全部囊括mode 的权限』的文件,举例来说,我们要搜寻-rwxr--r-- ,亦即0744 的文件,使用-perm -0744,当一个文件的权限为-rwsr-xr-x ,亦即4755 时,也会被列出来,因为-rwsr-xr-x 的属性已经囊括了-rwxr--r-- 的属性了。    

  • -perm /mode    搜寻文件权限『包含任一mode 的权限』的文件,举例来说,我们搜寻-rwxr-xr-x ,亦即-perm /755 时,但一个文件属性为-rw-------也会被列出来,因为他有-rw.... 的属性存在!    

我们知道,文件的权限一般为644,目录的权限一般为755。如果,不是等于这个权限,可能就会有点问题,那么我们来找找看,是否有这类文件。

find /home/wwwroot/default ! -perm 644 -type d -exec ls -ld {} \;
# 查找权限不是644的文件,并将其修改为644
find /home/wwwroot/default ! -perm 644 -type f | xargs -n 10 chmod 644;

动作执行

其实这个命令上面已经使用到了,使用-exec选项,然后接命令,最后要以{} ;结尾,比如

find /home/wwwroot/default ! -perm 644 -type d -exec ls -ld {} \;

其他

find还支持正则(-regex)查找文件名,还可以不区分大小写(-iregex);

使用-empty可以查找文件大小为0的文件。

# find . -empty -exec ls -l {} \;

The above is the detailed content of File search command find in Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn