File search command find in Linux

齐天大圣
Release: 2020-06-04 13:17:48
Original
91 people have browsed it

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
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

Negative option!

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

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 {} \;
Copy after login

根据用户名、组来查找

与使用者或群组名称有关
-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
Copy after login

根据文件大小查找

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

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

根据文件权限查找

  • -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;
Copy after login

动作执行

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

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

其他

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

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

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

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!

Related labels:
1
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!