如何在 Linux 中使用 find
find

> 
> 使用正确的参数，`find` 命令是在你的系统上找到数据的强大而灵活的方式。
> 
> 
> 


![](/data/attachment/album/201805/16/083712gelll7esoold7o7l.png)


在[最近的一篇文章](/article-9585-1.html)中，Lewis Cowles 介绍了 `find` 命令。


`find` 是日常工具箱中功能更强大、更灵活的命令行工具之一，因此值得花费更多的时间。


最简单的，`find` 跟上路径寻找一些东西。例如：



```
find /

```

它将找到（并打印出）系统中的每个文件。而且由于一切都是文件，你会得到很多需要整理的输出。这可能不能帮助你找到你要找的东西。你可以改变路径参数来缩小范围，但它不会比使用 `ls` 命令更有帮助。所以你需要考虑你想要找的东西。


也许你想在主目录中找到所有的 JPEG 文件。 `-name` 参数允许你将结果限制为与给定模式匹配的文件。



```
find ~ -name '*jpg'

```

可是等等！如果它们中的一些是大写的扩展名会怎么样？`-iname` 就像 `-name`，但是不区分大小写。



```
find ~ -iname '*jpg'

```

很好！但是 8.3 名称方案是如此的老。一些图片可能是 .jpeg 扩展名。幸运的是，我们可以将模式用“或”（表示为 `-o`）来组合。



```
find ~ ( -iname 'jpeg' -o -iname 'jpg' )

```

我们正在接近目标。但是如果你有一些以 jpg 结尾的目录呢？ （为什么你要命名一个 `bucketofjpg` 而不是 `pictures` 的目录就超出了本文的范围。）我们使用 `-type` 参数修改我们的命令来查找文件。



```
find ~ \( -iname '*jpeg' -o -iname '*jpg' \) -type f

```

或者，也许你想找到那些命名奇怪的目录，以便稍后重命名它们：



```
find ~ \( -iname '*jpeg' -o -iname '*jpg' \) -type d

```

你最近拍了很多照片，所以让我们把它缩小到上周更改的文件。



```
find ~ \( -iname '*jpeg' -o -iname '*jpg' \) -type f -mtime -7

```

你可以根据文件状态更改时间 （`ctime`）、修改时间 （`mtime`） 或访问时间 （`atime`） 来执行时间过滤。 这些是在几天内，所以如果你想要更细粒度的控制，你可以表示为在几分钟内（分别是 `cmin`、`mmin` 和 `amin`）。 除非你确切地知道你想要的时间，否则你可能会在 `+` （大于）或 `-` （小于）的后面加上数字。


但也许你不关心你的照片。也许你的磁盘空间不够用，所以你想在 `log` 目录下找到所有巨大的（让我们定义为“大于 1GB”）文件：



```
find /var/log -size +1G

```

或者，也许你想在 `/data` 中找到 bcotton 拥有的所有文件：



```
find /data -owner bcotton

```

你还可以根据权限查找文件。也许你想在你的主目录中找到对所有人可读的文件，以确保你不会过度分享。



```
find ~ -perm -o=r

```

这篇文章只说了 `find` 能做什么的表面。将测试条件与布尔逻辑相结合可以为你提供难以置信的灵活性，以便准确找到要查找的文件。并且像 `-exec` 或 `-delete` 这样的参数，你可以让 `find` 对它发现的内容采取行动。你有任何最喜欢的 `find` 表达式么？在评论中分享它们！




---


via: <https://opensource.com/article/18/4/how-use-find-linux>


作者：[Ben Cotton](https://opensource.com/users/bcotton) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
In [a recent Opensource.com article](https://opensource.com/article/18/4/how-find-files-linux), Lewis Cowles introduced the `find`

command.

`find`

is one of the more powerful and flexible command-line programs in the daily toolbox, so it's worth spending a little more time on it.

At a minimum, `find`

takes a path to find things. For example:

```
``````
find /
```


will find (and print) every file on the system. And since everything is a file, you will get a lot of output to sort through. This probably doesn't help you find what you're looking for. You can change the path argument to narrow things down a bit, but it's still not really any more helpful than using the `ls`

command. So you need to think about what you're trying to locate.

Perhaps you want to find all the JPEG files in your home directory. The `-name`

argument allows you to restrict your results to files that match the given pattern.

```
``````
find ~ -name '*jpg'
```


But wait! What if some of them have an uppercase extension? `-iname`

is like `-name`

, but it is case-insensitive.

```
``````
find ~ -iname '*jpg'
```


Great! But the 8.3 name scheme is so 1985. Some of the pictures might have a .jpeg extension. Fortunately, we can combine patterns with an "or," represented by `-o`

.

```
````find ~ ( -iname 'jpeg' -o -iname 'jpg' )`


We're getting closer. But what if you have some directories that end in jpg? (Why you named a directory `bucketofjpg`

instead of `pictures`

is beyond me.) We can modify our command with the `-type`

argument to look only for files.

```
``````
find ~ \( -iname '*jpeg' -o -iname '*jpg' \) -type f
```


Or maybe you'd like to find those oddly named directories so you can rename them later:

```
``````
find ~ \( -iname '*jpeg' -o -iname '*jpg' \) -type d
```


It turns out you've been taking a lot of pictures lately, so let's narrow this down to files that have changed in the last week.

```
``````
find ~ \( -iname '*jpeg' -o -iname '*jpg' \) -type f -mtime -7
```



You can do time filters based on file status change time (`ctime`

), modification time (`mtime`

), or access time (`atime`

). These are in days, so if you want finer-grained control, you can express it in minutes instead (`cmin`

, `mmin`

, and `amin`

, respectively). Unless you know exactly the time you want, you'll probably prefix the number with `+`

(more than) or `–`

(less than).

But maybe you don't care about your pictures. Maybe you're running out of disk space, so you want to find all the gigantic (let's define that as "greater than 1 gigabyte") files in the `log`

directory:

```
``````
find /var/log -size +1G
```


Or maybe you want to find all the files owned by bcotton in `/data`

:

```
``````
find /data -owner bcotton
```


You can also look for files based on permissions. Perhaps you want to find all the world-readable files in your home directory to make sure you're not oversharing.

```
``````
find ~ -perm -o=r
```


This post only scratches the surface of what `find`

can do. Combining tests with Boolean logic can give you incredible flexibility to find exactly the files you're looking for. And with arguments like `-exec`

or `-delete`

, you can have `find`

take action on what it... finds. Have any favorite `find`

expressions? Share them in the comments!

## 12 Comments