Fifth day punch card (find usage)

Linux system find command is very useful and convenient when searching for files. It can find files based on different criteria, such as permissions, owner, modification date / time, file size, and so on. In this article, we will learn how to use the find command and the options it provides to locate the file.

In the vast majority of Linux distributions, you can directly use the find command without any installation. If you want to be particularly efficient in the command line linux system, so find one that you must master command.

The basic syntax of the find command is as follows:

$ find [path] [option] [expression]

First, the basic usage

1. List all files in the current directory and subdirectories

This command will list all files in the current directory and subdirectories.

?
1
2
3
4
5
6
$ find
.
. /abc .txt
. /subdir
. /subdir/how .php
. /cool .php

This command is the same command effects

?
1
2
$ find .
$ find . -print

2. Find a special directory or path

The following command looks for files in the current directory test folder, default lists all files.

?
1
2
3
4
5
6
$ find . /test
. /test
. /test/abc .txt
. /test/subdir
. /test/subdir/how .php
. /test/cool .php

The following command is used to find the specified file name.

?
1
2
$ find . /test -name "abc.txt"
. /test/abc .txt

You can also use wildcards

?
1
2
3
$ find . /test -name "*.php"
. /test/subdir/how .php
. /test/cool .php

Please note that all of the folder will be recursively search. So, this is a find the specified file extensions for a very powerful way.

If we try to search / folder, which is the root directory, it will search the entire file system, including equipment and network storage devices mounted. So use with caution. Of course, you can always terminate the command by pressing Ctrl + C.

Note: When a specified folder (such as the example in the "./test" folder), ignoring the end of the slash is not a problem. However, if the folder is a link to other locations (symlink), you have to write at the end of the slash to make the find command to work (find ./test/).

Ignore case

When looking for a file name, ignoring case often very useful. To ignore case, only you need to use iname option instead of name options.

?
1
2
3
$ find . /test -iname "*.Php"
. /test/subdir/how .php
. /test/cool .php

Always use double quotes or single quotes to surround the matching pattern (file name parameter), which is very useful. Failure to do so can sometimes work, and sometimes may produce strange results.

3. Restrict directory lookup depth

find command default recursive search the entire directory tree, which is very time-consuming and resource. Fortunately, you can specify the directory to find the depth manually. For example, we just want to find a subdirectory within two layers, you can be specified by maxdepth option.

?
1
2
3
4
5
6
$ find . /test -maxdepth 2 -name "*.php"
. /test/subdir/how .php
. /test/cool .php
 
$ find . /test -maxdepth 1 -name *.php
. /test/cool .php

The second example is specified maxdepth to 1, indicating that at most only find subdirectories within a layer, that is, to find only the current folder.

When we want to find in the current directory, rather than searching the entire directory tree, this option will be particularly useful.

Maxdepth with similar options, there is an option called the mindepth, as the name suggests, it will at least reach the first level of subdirectories N before starting to find files.

4. Reverse Lookup

In addition to satisfying the condition Find the file, we can also find all files that do not satisfy the conditions. When we know which files to exclude in the lookup time, this option will be able to play.

?
1
2
3
4
$ find . /test -not -name "*.php"
. /test
. /test/abc .txt
. /test/subdir

In the above example, we found all the extension is not php files and folders. We can also use an exclamation mark! Instead -not.

?
1
find . /test ! -name "*.php"

The combined plurality of search conditions

We can simultaneously use multiple search criteria to specify the file name and exclude certain files.

?
1
2
3
$ find . /test -name 'abc*' ! -name '*.php'
. /test/abc .txt
. /test/abc

The above command finds all files that start with abc and without the .php extension. This example demonstrates the find command comes to find expression and how strong yes.

OR operator

When we use multiple search criteria, find command will combine them by the AND operator, that is to say, only to meet all the conditions of the files will be listed. However, if we need to find based on the OR operator, you can add -o switch.

?
1
2
3
4
5
$ find -name '*.php' -o -name '*.txt'
. /abc .txt
. /subdir/how .php
. /abc .php
. /cool .php

The above command finds all files ending with .php or .txt ending.

6. find only a file or directory

Sometimes we just want to find the corresponding file or by a corresponding directory name, we can easily implement this requirement.

?
1
2
3
$ find . /test -name abc*
. /test/abc .txt
. /test/abc

Find only files

?
1
2
$ find . /test - type f -name "abc*"
. /test/abc .txt

Find only directories

?
1
2
$ find . /test - type d -name "abc*"
. /test/abc

Very useful and convenient!

7. At the same time look at more than one directory

如果你想要在两个不同的目录内进行查找,命令非常简单。

?
1
2
3
$ find . /test . /dir2 - type f -name "abc*"
. /test/abc .txt
. /dir2/abcdefg .txt

检查一下,它确实列出了来自给定的两个目录的文件。

8. 查找隐藏文件

在Linux系统中,隐藏文件的名字以英文的句号开头,即 . 。所以要列出隐藏文件,只需加上简单的文件名过滤条件就行了。

$ find ~ -type f -name ".*"

二、基于文件权限和属性的查找

9. 查找指定权限的文件

通过指定 perm 选项,我们可以查找具有特定权限的文件。下面的示例中查找了所有具有 0664 权限的文件。

?
1
2
3
4
5
$ find . - type f -perm 0664
. /abc .txt
. /subdir/how .php
. /abc .php
. /cool .php

我们可以用这个命令来查找带有错误权限的文件,这些文件可能会产生安全问题。

可以结合 反向查找 来进行权限检查。

?
1
2
3
4
5
$ find . - type f ! -perm 0777
. /abc .txt
. /subdir/how .php
. /abc .php
. /cool .php

10. 查找具有 SGID/SUID 属性的文件

下面的命令查找所有具有 644 权限和 SGID 属性的文件。

# find / -perm 2644

我们同样可以使用 1664 来查找设置了 粘滞位 (sticky bit)的文件。

# find / -perm 1644

perm 选项除了接受数值型参数外,同样接受 chmod 命令中的模式串。在下面的查找中,我们用另一种语法来代替数字。

?
1
2
3
4
5
6
7
8
$ find / -maxdepth 2 -perm /u =s 2> /dev/null
/bin/mount
/bin/su
/bin/ping6
/bin/fusermount
/bin/ping
/bin/umount
/sbin/mount .ecryptfs_private

注意:由于权限不足,某些目录会拒接访问。命令中的 2>/dev/null 正是用于清除输出中的错误访问结果。

11. 查找只读文件

?
1
2
3
4
5
6
7
$ find /etc -maxdepth 1 -perm /u =r
/etc
/etc/thunderbird
/etc/brltty
/etc/dkms
/etc/phpmyadmin
... output truncated ...

12. 查找可执行文件

?
1
2
3
4
5
6
7
$ find /bin -maxdepth 2 -perm /a =x
/bin
/bin/preseed_command
/bin/mount
/bin/zfgrep
/bin/tempfile
... output truncated ...

三、基于文件拥有者和用户组的查找

13. 查找属于特定用户的文件

查找当前目录下,属于 bob 的文件。

?
1
2
3
4
5
6
7
$ find . -user bob
.
. /abc .txt
. /abc
. /subdir
. /subdir/how .php
. /abc .php

在指定所属用户的同时,我们同样可以指定文件名。

$ find . -user bob -name '*.php'

很容易看出,我们可以通过增加过滤条件来缩小查找文件的范围。

14. 查找属于特定用户组的文件

# find /var/www -group developer

四、基于日期和时间的查找

除了上面介绍的查找条件外,另外一个非常棒的查找条件就是文件的修改和访问时间(日期)。当我们想要找出哪些文件在某段时间内被修改的时候,这个查找条件将会非常方便。我们来看几个例子。

15. 查找过去的第 N 天被修改过的文件

# find / -mtime 50

16. 查找过去的 N 天内被访问过的文件

# find / -atime -50

17. 查找某段时间范围内被修改过内容的文件

# find / -mtime +50 -mtime -100

18. 查找过去的 N 分钟内状态发生改变的文件

$ find /home/bob -cmin -60

19. 查找过去的 1 小时内被修改过内容的文件

# find / -mmin -60

20. 查找过去的 1 小时内被访问过的文件

# find / -amin -60

五、基于文件大小的查找

21. 查找指定大小的文件

$ find / -size 50M

22. 查找大小在一定范围内的文件

$ find / -size +50M -size -100M

23. 查找最大和最小的文件

我们可以将 find 命令与 ls 和 sort命令结合,从而找出最大或最小的文件。

下面的命令使用了 sort 命令的 -r 选项,也就是从大到小降序排列。经过 head 命令的过滤之后,会显示当前目录和子目录下最大的5个文件。命令的执行过程需要一段时间,查找的速度取决于文件的总数。

$ find . -type f -exec ls -s {} \; | sort -n -r | head -n 5

同样,我们可以去掉 sort 命令的 -r 选项来进行升序排列,从而显示出最小的5个文件。

$ find . -type f -exec ls -s {} \; | sort -n | head -n 5

24. 查找空文件和空目录

查找空文件:

# find /tmp -type f -empty

查找空目录:

$ find ~/ -type d -empty

非常简单!

六、高级操作

find 命令不仅可以通过特定条件来查找文件,还可以对查找到的文件使用任意linux命令进行操作。下面给出两个例子。

25. 使用 ls 命令列出文件信息

我们使用 find 命令找到文件后,只能看到文件路径。如果想进一步查看文件信息,可以结合 ls 命令来实现。

?
1
2
3
4
5
6
7
8
$ find . - exec ls -ld {} \;
drwxrwxr-x 4 enlightened enlightened 4096 Aug 11 19:01 .
-rw-rw-r-- 1 enlightened enlightened 0 Aug 11 16:25 . /abc .txt
drwxrwxr-x 2 enlightened enlightened 4096 Aug 11 16:48 . /abc
drwxrwxr-x 2 enlightened enlightened 4096 Aug 11 16:26 . /subdir
-rw-rw-r-- 1 enlightened enlightened 0 Aug 11 16:26 . /subdir/how .php
-rw-rw-r-- 1 enlightened enlightened 29 Aug 11 19:13 . /abc .php
-rw-rw-r-- 1 enlightened enlightened 0 Aug 11 16:25 . /cool .php

26. 删除找到的文件

下面的命令会删除 tmp 目录下扩展名为 .txt 的文件。

$ find /tmp -type f -name "*.txt" -exec rm -f {} \;

我们同样可以删除目录,只要把 -type 后面的 f 改为 d ,并且在 rm 命令后面加上 -r 即可。

$ find /tmp -type d -name "dirToRemove" -exec rm -r -f {} \;

本文由 Wray 翻译,略有删改。考虑到原文有个别错误,以及为便于读者理解,故采用意译的方式。不足之处请大家指出,谢谢。

Guess you like

Origin www.cnblogs.com/bokerick/p/11207834.html