Home >System Tutorial >LINUX >7 Powerful Uses of Find Command in Linux

7 Powerful Uses of Find Command in Linux

WBOY
WBOYforward
2024-02-15 08:30:03458browse

Okay, I can try to make a deep pseudo-original version of this paragraph. This is my modified version:

In Linux, the find command is one of the operations that back-end developers must master, unless you are using Windows Server.

It is also a common topic in technical interviews. Here is a real question:

If you have a directory called logs on your Linux server, how do you delete log files in it that are last accessed more than a year ago? 🤔

This situation is very common, but surprisingly, not every developer can clearly write this command in the interview.

Answer: First, we need to use the cd command to enter the corresponding directory. Then, the command is as follows:

linuxmi@linuxmi:~/www.linuxmi.com$ find . -type f -atime +365 -exec rm -rf {} \;
Linux 中 find 命令的 7 个强大用途

If you don’t fully understand the above commands, don’t worry. This article will introduce 7 practical uses of the find command so that you can eventually master it. If you already know this, reading this article will be a good refresher.

1. Find files based on file names or regular expressions

Let's start with the simplest usage. To search for files by a specific name, use the following command:

linuxmi@linuxmi:~/www.linuxmi.com$ find . -name linuxmi.sh
Linux 中 find 命令的 7 个强大用途

The . symbol in the above code indicates the current path. If we want to search for files in another path, just specify:

linuxmi@linuxmi:~/www.linuxmi.com$ find /home/linuxmi/linuxmi.com -name linuxmi.png
Linux 中 find 命令的 7 个强大用途

How to find all images in png format? Use regular expressions:

find /home/linuxmi/linuxmi.com -name "*.png"

By default, the find command searches regular files, but it is better to specify the type to make everything clearer:

find /home/linuxmi/linuxmi.com  -type f -name "*.png"

2. Find different types of files

In addition to searching for ordinary files, we can also search for other types of files by specifying the -type option.

For example directory:

find . -type d -name "linuxmi*"

or symbolic link:

find . -type l -name "linuxmi*"

3. Search files by specific timestamp

To search files by a specific timestamp, we need to understand the 3 different timestamps in Linux systems:

Access timestamp (atime): The time when the file was last read. Modification timestamp (mtime): The time when the file content was last modified. Change timestamp (ctime): A file’s metadata, such as when it was last changed such as ownership, location, file type, and permission settings.

So, just like the interview question mentioned at the beginning, to search for files that are a time more than a year old, we can write the following command:

linuxmi@linuxmi:~$ find . -type f -atime +365
Linux 中 find 命令的 7 个强大用途

If we need to find files whose mtime is exactly 5 days ago, don't include the sign as it means "greater than".

linuxmi@linuxmi:~$ find . -type f -mtime 5
Linux 中 find 命令的 7 个强大用途

Obviously, the sign means "greater than" and the - sign means "less than". Therefore, we can search for files with a ctime between 5 and 10 days:

linuxmi@linuxmi:~$ find . -type f -ctime +5 -ctime -10
Linux 中 find 命令的 7 个强大用途

4. Search files by file size

The

-size option enables us to find files by a specific size. We can specify its unit of measurement using the following convention:

  • b:512字节块(默认)
  • c:字节
  • w:两字节单词
  • k:千字节
  • M:兆字节
  • G:千兆字节

与按时间戳查找文件类似,+号表示“大于”,-号表示“小于”。例如,要查找大小在10兆字节和1千兆字节之间的文件:

find . -type f -size +10M -size -1G

5、按权限查找文件

适当控制文件的权限是Linux管理员的重要任务。find命令的-perm选项可以帮助我们按特定权限搜索文件:

find . -type f -perm 777

例如,上述命令将搜索所有具有777权限的文件,这意味着文件对其所有者、组和所有用户具有读、写和执行权限。

6、按文件所有者查找文件

这个任务很简单。我们只需要在-user选项中指定一个用户名。例如,以下命令将找到所有属于 linuxmi 的文件:

find -type f -user linuxmi

7、在查找文件后执行命令

在大多数情况下,我们希望在找到所需文件后执行一些后续操作,例如删除它们、检查它们的详细信息等等。-exec命令使所有这些操作变得更加简单。

现在,为了理解如何使用它,让我们回到之前提到的面试问题:

find . -type f -atime +365 -exec rm -rf {} ;

上述命令中-exec选项后面是rm -rf,用于删除文件。{}是找到的结果的占位符。

注意:占位符{}非常重要,特别是如果你想要删除文件。因为如果你不使用它,命令将对所有文件执行,而不仅仅是通过find命令找到的文件。

为了尝试一下,在终端上执行以下两个命令并检查它们的结果有什么不同:

一个使用了占位符:

find . -type f -atime +5 -exec ls {} ;

另一个没有使用:

find . -type f -atime +5 -exec ls ;

跟在-exec选项后面的命令必须以分号结束。正如我们所知,转义字符用于取消单个字符的特殊含义。在Linux中,反斜杠\被用作转义字符。因此,我们将其用于分号字符。

总结

阅读完find命令的7个用途后,之前提到的面试问题现在看起来非常简单了。现在你能直接写出答案并清楚地解释吗?

find . -type f -atime +365 -exec rm -rf {};

感谢阅读。如果你喜欢,请关注Linux迷 www.linuxmi.com 以享受更多精彩文章。 🙂

The above is the detailed content of 7 Powerful Uses of Find Command in Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lxlinux.net. If there is any infringement, please contact admin@php.cn delete