您知道全新安装的Linux中有多少文件吗?如果您使用 PopOS!以Linux 发行版(Linux distribution)为例,有超过 31,000 个文件。那是在您开始创建任何文档、存储音乐、下载PDF(PDFs)或组织图片之前。
因此,在需要时在Linux中找到正确的文件或文件夹成为一项挑战。在本文中,您将学习如何使用Linux FIND命令,我们将尽可能地为您提供所有示例。
Linux FIND 命令语法(Linux FIND Command Syntax)
语法是指单词或命令的组合方式。就像(Just)一个普通的句子可以通过改变单词变得无意义一样,如果没有以正确的语法使用命令,它们可能会失败。
查找 [路径] [条件] [操作](find [path] [conditions] [actions])
这是什么意思:
find – 在(find )Linux中启动Find实用程序
路径(path )- 在哪里看
条件(conditions )- 要应用于搜索的参数
行动(actions )——你想对结果做什么
使用所有三个的简单示例如下所示:
寻找 。-name 文件-sample.rtf -print(find . -name file-sample.rtf -print)
如您所料,这将找到文件名file-sample.rtf。
句点 ( . ) 路径告诉 find 查看当前目录和其中的任何目录。
-name条件告诉 find 获取具有该特定名称的文件。
-print操作告诉 FIND在屏幕上显示结果。
句点和 -print 是 find 命令的默认值。所以如果你不使用它们,它仍然会做同样的事情。所以,find -name file-sample.rtf会给你同样的结果。
Linux 在另一个目录中查找(Linux FIND In Another Directory)
您可以在与您所在的目录不同的目录中搜索。只需在(Just)FIND之后插入目录的路径即可。如果您位于根目录并且您知道该文件位于home/user目录中的某个位置,您将使用:
find home/user -name file-sample.rtf
它仍然是递归搜索,因此它将遍历user下的每个目录。
Linux FIND 搜索多个目录(Linux FIND Search Multiple Directories)
如果您想一次在多个目录中搜索,只需在命令中列出它们,用空格分隔即可。
find /lib /var /bin -name file-sample.rtf
无递归或限制递归的 Linux FIND(Linux FIND with No Recursion or Limiting Recursion)
如果您在根级别使用上述FIND命令,它将查看系统上的每个目录。因此,如果您只想坚持当前目录,请使用-maxdepth选项。-maxdepth 后面的数字告诉 Find 在停止之前要走多深。
使用-maxdepth 1仅表示此目录。
查找-名称文件-sample.rtf -maxdepth 1(find -name file-sample.rtf -maxdepth 1)
使用-maxdepth 2或更大的数字意味着更深的层次。
查找 -maxdepth 5 -name 文件-sample.rtf(find -maxdepth 5 -name file-sample.rtf)
Linux FIND 通配符示例(Linux FIND Wildcard Example)
FIND命令使用星号 ( * *作为通配符。将它用于您不确定的名称的任何部分。它可以在名称中多次使用。如果没有文件类型作为文件名的一部分,结果也将包括匹配的目录。
find home/user -name file*sample*
Linux FIND 按类型示例(Linux FIND by Type Example)
要仅搜索文件或目录,请使用 -type 选项和适当的描述符。有一些,但文件和目录是最常见的:
f – 文件
d - 目录
b – 块设备
c – 字符设备
l – 符号链接
s——插座
find home/user -name file*sample* -type d
Linux FIND 不区分大小写示例(Linux FIND Case Insensitive Example)
与Windows不同,Linux关心字母是大写还是小写。因此,如果您希望它同时搜索File-Sample.rtf和 file-sample.rtf,请使用-iname选项。
find home/user -iname File-Sample.rtf
Linux FIND 几个文件示例(Linux FIND Several Files Example)
假设您要查找文件的 .rtf 和 .html 版本。这可以使用-o(或)运算符在一个命令中完成。在某些发行版中,您可能需要将名称放在括号内,例如( -name file-sample.rtf -o -name file-sample.html )。
find home/user -name file-sample.rtf -o -name file-sample.html
Linux 查找名称不匹配的文件(Linux FIND Files That Don’t Match a Name)
也许您知道有一个文件的.html版本,但如果有其他文件,您就不知道了。您可以使用-not选项将.html版本从搜索中过滤掉。
find home/user -name file-sample* -not -name *.html
Linux FIND 无错误结果(Linux FIND Without Error Results)
在没有递归的查找示例中,请注意它列出了它无法搜索的每个目录以及正确的结果。这很烦人。让我们阻止它显示所有那些“权限(Permission)被拒绝”的目录。将它与另一个Linux 终端命令(Linux terminal command)grep 结合使用。您还可以使用Find和 grep 来查找其中包含特定单词的文件(find files with specific words in them)。
find -maxdepth 5 -name file-sample.rtf 2>&1 | grep -v “Permission denied”
让我们分解2>&1。
2 – 表示标准错误输出的缩写 stderr 。
1 – 代表标准输出的缩写stdout
> - 表示将左侧的任何输出重定向到右侧的任何输出。
& – 表示放在一起。
所以2>&1 表示取标准错误并重定向它们,然后将它们与标准输出一起放在一个输出中。
现在让我们看看| grep -v “Permission denied”。
| (称为管道)——告诉Linux将其左侧的任何结果提供给其右侧的任何结果。它被提供给 grep 命令。
grep – 是一个文本搜索实用程序。
-v - 告诉 grep 搜索与 -v 左侧的文本不匹配的任何内容。在这种情况下,它告诉 grep 只查找不包含文本或字符串“ Permission denied”的任何内容。因此 grep 只会向您显示您正在寻找的结果以及任何与“权限(Permission)被拒绝”不匹配的错误。
Linux FIND by Permissions 示例(Linux FIND by Permissions Example)
要使用好这个,你需要学习 Linux 权限(learn Linux permissions)。
示例文件的权限均为 664,除了一个权限为 775 的文件。使用-perm选项查找。
find Documents/ -name file-sample* -type f -perm 775
Linux 按大小查找示例(Linux FIND by Size Example)
按大小查找文件对于让那些巨大的文件填满你的硬盘很方便。使用 -size 选项、所需大小和以下后缀之一。如果不使用后缀,则 -size 默认为b。要查找等于或大于特定大小的文件,请将加号 (+) 放在大小前面。
M – 兆字节
G – 千兆字节
k – 千字节
b – 块(512 字节 – 默认)
c——字节
w - 字(两个字节一起)
find -size +500k
Linux按所有者查找(Linux FIND by Owner)
有两种方法可以按所有者查找文件。一种是所有者的用户名,另一种是用户的组。要按用户名查找,请使用-user选项,后跟用户名。要按用户组查找,请使用-group后跟组名..
查找(find -user groupname )-user 组名或查找 -user 用户名(find -user username)
Linux FIND Files by Last Modified Example
要查找在过去 X 天中修改或编辑的文件,请使用-mtime后跟一个数字。在数字前面加上减号 ( - ) 会发现在之前的那几天内发生了任何变化。加号 ( + ) 表示现在之前的那几天内。
find -name “file-sample*” -mtime +5 (greater than 5 days ago)
find -name “file-sample*” -mtime -5 (less than 5 days ago)
要以分钟为单位查找上次修改时间,请使用选项 -mmin 后跟分钟数。使用 + 和 - 就像上面一样。
find -name “file-sample*” -mmin -5
find -name “file-sample*” -mmin +5
Linux FIND Files by Last Accessed TIME 示例(Linux FIND Files by Last Accessed TIme Example)
用于根据上次打开时间查找文件的选项是-atime(天)和-amin(分钟)。后面跟着天数或分钟数返回并使用 + 和 - 符号表示大于和小于。
find -name “file-sample*” -atime -5
find -name “file-sample* -amin -5
将 FIND 与其他 Linux 命令结合使用(Combine FIND with Other Linux Commands)
上面有一个将 find 与 grep 命令一起使用的示例,您可以将它与许多其他命令一起使用。您可以看到使用 find 和其他命令可能非常强大并且可以节省大量时间。想象一下(Imagine)必须删除一堆特定类型的文件。无需在文件资源管理器中四处搜索,只需制作正确的命令,即可在几秒钟内完成。你现在将如何使用 find 命令?
Linux FIND Command With Examples
Do you know how many filеs аre іn a fresh install of Linux? If you use the PopOS! Linux distribution as an example, there are over 31,000 files. That’s before you start creating any documents, storing music, downloading PDFs, or organizing pictures.
Because of this, finding the right file or folder in Linux when you need it becomes a challenge. In this article, you’ll learn how to use the Linux FIND command, and we’re going to give you all the examples we can.
Linux FIND Command Syntax
Syntax refers to how words, or commands, are put together. Just as a normal sentence can become nonsense by just shuffling the words, commands can fail if they’re not used in the proper syntax.
find [path] [conditions] [actions]
Here’s what that means:
find – initiates the Find utility in Linux
path – where to look
conditions – arguments you want to apply to the search
actions – what you want to do with the results
A simple example using all three looks like:
find . -name file-sample.rtf -print
As you guessed, this will find the file name file-sample.rtf.
The period (.) path tells find to look in the current directory and any directories inside it.
The -name condition tells find to get the file with that specific name.
The -print action tells FIND to show the results on the screen.
The period and -print are defaults for the find command. So it will still do the same thing if you don’t use them. So, find -name file-sample.rtf will give you the same results.
Linux FIND In Another Directory
You can search in a directory different from the one you’re in. Just insert the path to the directory after FIND. If you’re at the root and you know the file is somewhere in the home/user directory you would use:
find home/user -name file-sample.rtf
It’s still a recursive search, so it will go through every directory under user.
Linux FIND Search Multiple Directories
If you wanted to search in several directories at once, just list them in the command, separated by a space.
find /lib /var /bin -name file-sample.rtf
Linux FIND with No Recursion or Limiting Recursion
If you used the FIND command above at the root level, it would look through every directory on the system. So if you want to stick to just the current directory, use the -maxdepth option. The number after -maxdepth tells Find how deep to go before stopping.
Using -maxdepth 1 means just this directory.
find -name file-sample.rtf -maxdepth 1
Using -maxdepth 2 or greater number means to go that many levels deeper.
find -maxdepth 5 -name file-sample.rtf
Linux FIND Wildcard Example
The FIND command uses the asterisk (*) as a wildcard. Use it for any part of the name that you’re unsure of. It can be used more than once in the name. Without the file type as part of the file name, results will also include directories that match.
find home/user -name file*sample*
Linux FIND by Type Example
To only search for a file or a directory, use the -type option and the appropriate descriptor. There’s a few, but the file and directory ones are most common:
f – file
d – directory
b – block device
c – character device
l – symbolic link
s – socket
find home/user -name file*sample* -type d
Linux FIND Case Insensitive Example
Unlike Windows, Linux cares about whether a letter is capital or lowercase. So if you want it to search for both File-Sample.rtf and file-sample.rtf, use the -iname option.
find home/user -iname File-Sample.rtf
Linux FIND Several Files Example
Let’s say you wanted to find the .rtf and .html versions of a file. That can be done in one command using the -o (or) operator. In some distros, you may need to put the names inside of brackets, like ( -name file-sample.rtf -o -name file-sample.html ).
find home/user -name file-sample.rtf -o -name file-sample.html
Linux FIND Files That Don’t Match a Name
Perhaps you know there’s the .html version of a file, but not if there are others. You could filter the .html version out of the search using the -not option.
find home/user -name file-sample* -not -name *.html
Linux FIND Without Error Results
In the find with no recursion example, notice that it listed every directory that it couldn’t search in and the correct result. That’s annoying. Let’s stop it from showing all those “Permission denied” directories. Combine it with another Linux terminal command, grep. You can also use Find with grep to find files with specific words in them.
find -maxdepth 5 -name file-sample.rtf 2>&1 | grep -v “Permission denied”
Let’s break down 2>&1.
2 – represents stderr which is short for standard errors output.
1 – represents stdout which is short for standard output
> – means to redirect whatever output is to the left of it to whatever is to the right of it.
& – means to put together.
So 2>&1 means take the standard errors and redirect them, and then put them together with the standard output into one output.
Now lets look at | grep -v “Permission denied”.
| (called a pipe) – tells Linux to feed the results of whatever is to the left of it to whatever is to its right. It’s being fed to the grep command.
grep – is a text search utility.
-v – tells grep to search for anything that doesn’t match text to the left of the -v. In this case, it’s telling grep to only find anything that doesn’t contain the text or string, “Permission denied.” So grep will only show you the results you’re looking for and any errors that don’t match “Permission denied.”
Linux FIND by Permissions Example
To use this well, you need to learn Linux permissions.
The example files all have the permissions 664, except one with the permissions 775. Use the -perm option to find it.
find Documents/ -name file-sample* -type f -perm 775
Linux FIND by Size Example
Finding files by size is handy for getting those huge files filling up your hard drive. Use the -size option, the size desired, and one of the following suffixes. If no suffix is used, -size defaults to b. To find files equal to and larger than a certain size, put the plus-sign (+) in front of the size.
M – Megabytes
G – Gigabytes
k – Kilobytes
b – blocks (512 bytes – default)
c – bytes
w – words (two bytes together)
find -size +500k
Linux FIND by Owner
There are two ways to find files by owner. One is by an owner’s user name, and the other is by the user’s group. To find by username, use the -user option, followed by the username. To find by user group, use -group followed by the group name..
find -user groupname or find -user username
Linux FIND Files by Last Modified Example
To find files that were modified, or edited, in the last X number of days, use -mtime followed by a number. Putting a minus sign (–) in front of the number will find anything altered within that many days before now. A plus sign (+) means within that many days before now.
find -name “file-sample*” -mtime +5 (greater than 5 days ago)
find -name “file-sample*” -mtime -5 (less than 5 days ago)
To find by last modified in minutes, use the option -mmin followed by the number of minutes. Use the + and – like above.
find -name “file-sample*” -mmin -5
find -name “file-sample*” -mmin +5
Linux FIND Files by Last Accessed TIme Example
The option used to find files based on when they were last opened is -atime for days and -amin for minutes. Follow it with the number of days or minutes to go back and use the + and – sign as greater than and less than.
find -name “file-sample*” -atime -5
find -name “file-sample* -amin -5
Combine FIND with Other Linux Commands
There’s one example above of using find with the grep command, and you can use it with many others. You can see that using find and other commands can be very powerful and a huge timesaver. Imagine having to delete a bunch of a particular type of file. Instead of searching around in the file explorer, just craft the right command, and it’s done in seconds. How will you use the find command now?