1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > linux查找文件夹命令_如何在Linux中使用命令行查找文件和文件夹

linux查找文件夹命令_如何在Linux中使用命令行查找文件和文件夹

时间:2022-07-06 12:17:42

相关推荐

linux查找文件夹命令_如何在Linux中使用命令行查找文件和文件夹

linux查找文件夹命令

Most peopleuse a graphical file manager to find files in Linux, such as Nautilus in Gnome, Dolphin in KDE, and Thunar in Xfce. However, there are several ways to use the command line to find files in Linux, no matter what desktop manager you use.

大多数人使用图形文件管理器在Linux中查找文件,例如Gnome中的Nautilus,KDE中的Dolphin和Xfce中的Thunar。 但是,无论使用哪种桌面管理器,都有几种方法可以使用命令行在Linux中查找文件。

使用查找命令 (Using the Find Command)

The “find” command allows you to search for files for which you know the approximate filenames. The simplest form of the command searches for files in the current directory and recursively through its subdirectories that match the supplied search criteria. You can search for files by name, owner, group, type, permissions, date, and other criteria.

“ find”命令使您可以搜索已知文件名的文件。 该命令的最简单形式是在当前目录中搜索文件,并通过与提供的搜索条件相匹配的子目录递归搜索。 您可以按名称,所有者,组,类型,权限,日期和其他条件搜索文件。

Typing the following command at the prompt lists all files found in the current directory.

在提示符下键入以下命令将列出在当前目录中找到的所有文件。

find .

The dot after “find” indicates the current directory.

“查找”后的点表示当前目录。

To find files that match a specific pattern, use the-nameargument. You can use filename metacharacters (such as*), but you should either put an escape character (\) in front of each of them or enclose them in quotes.

要查找与特定模式匹配的文件,请使用-name参数。 您可以使用文件名元字符(例如*),但应在每个字符前放置转义符(\)或将其括在引号中。

For example, if we want to find all the files that start with “pro” in the Documents directory, we would use thecd Documents/command to change to the Documents directory, and then type the following command:

例如,如果我们要在Documents目录中找到所有以“ pro”开头的文件,则可以使用cd Documents/命令切换到Documents目录,然后键入以下命令:

find . -name pro\*

All files in the current directory starting with “pro” are listed.

列出当前目录中以“ pro”开头的所有文件。

NOTE: The find command defaults to being case sensitive. If you want the search for a word or phrase to be case insensitive, use the-inameoption with the find command. It is the case insensitive version of the-namecommand.

注意:find命令默认为区分大小写。 如果要搜索的单词或短语不区分大小写,请在find命令中使用-iname选项。 它是-name命令的不区分大小写的版本。

Iffinddoesn’t locate any files matching your criteria, it produces no output.

如果find找不到符合您条件的任何文件,则不会产生任何输出。

The find command has a lot of options available for refining the search. For more information about the find command, runman findin a Terminal window and press Enter.

find命令具有许多可用于完善搜索的选项。 有关find命令的更多信息,请在“终端”窗口中运行man find并按Enter。

使用定位命令 (Using the Locate Command)

The locate command is faster than the find command because it uses a previously built database, whereas the find command searches in the real system, through all the actual directories and files. The locate command returns a list of all path names containing the specified group of characters.

find命令比find命令快,因为它使用以前建立的数据库,而find命令在真实系统中搜索所有实际目录和文件。 查找命令返回包含指定字符组的所有路径名的列表。

The database is updated periodically from cron, but you can also update it yourself at any time so you can obtain up-to-the-minute results. To do this, type the following command at the prompt:

该数据库是通过cron定期更新的,但是您也可以随时自己更新它,以便获得最新的结果。 为此,在提示符下键入以下命令:

sudo updatedb

Enter your password when prompted.

出现提示时输入密码。

The basic form of the locate command finds all the files on the file system, starting at the root, that contain all or any part of the search criteria.

locate命令的基本形式是在文件系统中查找从根开始的所有文件,其中包含全部或部分搜索条件。

locate mydata

For example, the above command found two files containing “mydata” and one file containing “data.”

例如,上述命令找到了两个包含“ mydata”的文件和一个包含“ data”的文件。

If you want to find all files or directories that contain exactly and only your search criteria, use the-boption with the locate command, as follows.

如果要查找完全包含且仅包含搜索条件的所有文件或目录,请使用-b选项和locate命令,如下所示。

locate -b ‘\mydata’

The backslash in the above command is a globbing character, which provides a way of expanding wildcard characters in a non-specific file name into a set of specific filenames. A wildcard is a symbol that can be replaced by one or more characters when the expression is evaluated. The most common wildcard symbols are the question mark (?), which stands for a single character and the asterisk (*), which stands for a contiguous string of characters. In the above example, the backslash disables the implicit replacement of “mydata” by “*mydata*” so you end up with only results containing “mydata.”

上面命令中的反斜杠是通配符,它​​提供了一种将非特定文件名中的通配符扩展为一组特定文件名的方法。 通配符是一种符号,在计算表达式时可以用一个或多个字符替换。 最常见的通配符是问号(?)代表单个字符,而星号(*)代表连续的字符串。 在上面的示例中,反斜杠禁用了用“ * mydata *”隐式替换“ mydata”,因此最终只能得到包含“ mydata”的结果。

The mlocate command is a new implementation of locate. It indexes the entire file system, but the search results only include files to which the current user has access. When you update the mlocate database, it keeps timestamp information in the database. This allows mlocate to know if the contents of a directory changed without reading the contents again and makes updates to the database faster and less demanding on your hard drive.

mlocate命令是locate的新实现。 它为整个文件系统建立索引,但是搜索结果仅包括当前用户有权访问的文件。 当您更新mlocate数据库时,它将时间戳信息保留在数据库中。 这使mlocate无需重新读取目录内容即可知道目录的内容是否已更改,并且使对数据库的更新更快,对硬盘的要求更低。

When you install mlocate, the /usr/bin/locate binary file changes to point to mlocate. To install mlocate, if it’s not already included in your Linux distribution, type the following command at the prompt.

安装mlocate时,/ usr / bin / locate二进制文件更改为指向mlocate。 要安装mlocate,如果Linux发行版中尚未包含mlocate,请在提示符下键入以下命令。

sudo apt-get install mlocate

NOTE: We will show you a command later in this article that allows you to determine where the executable for a command is located, if it exists.

注意:在本文后面,我们将向您显示命令,该命令使您可以确定命令的可执行文件所在的位置(如果存在)。

The mlocate command does not use the same database file as the standard locate command. Therefore, you may want to create the database manually by typing the following command at the prompt:

mlocate命令与标准locate命令使用的数据库文件不同。 因此,您可能需要通过在提示符下键入以下命令来手动创建数据库:

sudo /etc/cron.daily/mlocate

The mlocate command will not work until the database is created either manually or when the script is run from cron.

在手动创建数据库或从cron运行脚本之前,mlocate命令将不起作用。

For more information about either the locate or the mlocate command, typeman locateorman mlocatein a Terminal window and press Enter. The same help screen displays for both commands.

有关无论是定位还是更多信息的mlocate命令,键入man locateman mlocate在终端窗口,然后按Enter。 这两个命令显示相同的帮助屏幕。

使用哪个命令 (Using the Which Command)

The “which” command returns the absolute path of the executable that is called when a command is issued. This is useful in finding the location of an executable for creating a shortcut to the program on the desktop, on a panel, or other place in the desktop manager. For example, typing the commandwhich firefoxdisplays the results shown in the image below.

“哪个”命令返回执行命令时调用的可执行文件的绝对路径。 这对于在桌面,面板或桌面管理器中的其他位置查找用于创建程序快捷方式的可执行文件的位置很有用。 例如,键入命令which firefox显示下图所示的结果。

By default, the which command only displays the first matching executable. To display all matching executables, use the-aoption with the command:

缺省情况下,which命令仅显示第一个匹配的可执行文件。 要显示所有匹配的可执行文件,请在命令中使用-a选项:

which -a firefox

You can search for multiple executables using at once, as shown in the following image. Only the paths to executables found are displayed. In the example below, only the “ps” executable was found.

您可以一次搜索多个可执行文件,如下图所示。 仅显示找到的可执行文件的路径。 在下面的示例中,仅找到“ ps”可执行文件。

NOTE: The which command only searches the current user’s PATH variable. If you search for an executable that is only available for the root user as a normal user, no results will display.

注意:which命令仅搜索当前用户的PATH变量。 如果搜索仅对root用户具有普通用户身份的可执行文件,则不会显示任何结果。

For more information about the which command, type “man which” (without the quotes) at the command prompt in a Terminal window and press Enter.

有关which命令的更多信息,请在Terminal窗口的命令提示符下键入“ man which”(不带引号),然后按Enter。

使用Whereis命令 (Using the Whereis Command)

The whereis command is used to find out where the binary, source, and man page files for a command are located. For example, typingwhereis firefoxat the prompt displays results as shown in the following image.

whereis命令用于查找命令的二进制文件,源文件和手册页文件的位置。 例如,在提示符下键入whereis firefox将显示结果,如下图所示。

If you want only the path to the executable to display, and not the paths to the source and the man(ual) pages, use the-boption. For example, the commandwhereis -b firefoxwill display only/usr/bin/firefoxas the result. This is handy because you will most likely search for a program’s executable file more often than you would search for source and man pages for that program. You can also search for only the source files (-s) or for only the man pages (-m).

如果只想显示可执行文件的路径,而不要显示源页面和手册页的路径,请使用-b选项。 例如,命令whereis -b firefox将仅显示/usr/bin/firefox作为结果。 这很方便,因为与搜索该程序的源文件和手册页相比,搜索该程序的可执行文件的可能性更高。 您还可以仅搜索源文件(-s)或仅手册页(-m)。

For more information about the whereis command, typeman whereisin a Terminal window and press Enter.

有关whereis命令的更多信息,请在Terminal窗口中键入man whereis并按Enter。

了解Whereis命令和which命令之间的区别 (Understanding the Difference Between the Whereis Command and the Which Command)

The whereis command shows you the location for the binary, source, and man pages for a command, whereas the which command only shows you the location of the binary for the command.

whereis命令显示命令的二进制文件,源代码和手册页的位置,而where命令仅显示命令的二进制文件的位置。

The whereis command searches through a list of specific directories for the binary, source, and man files whereas the which command searches the directories listed in the current user’s PATH environment variable. For the whereis command, the list of specific directories can be found in the FILES section of the man pages for the command.

whereis命令在特定目录列表中搜索二进制文件,源文件和man文件,而where命令则搜索当前用户的PATH环境变量中列出的目录。 对于whereis命令,可以在命令手册页的FILES部分中找到特定目录的列表。

When it comes to results displayed by default, the whereis command displays everything it finds whereas the which command only displays the first executable it finds. You can change that using the-aoption, discussed earlier, for the which command.

当涉及默认显示的结果时,whereis命令显示其找到的所有内容,而which命令仅显示其找到的第一个可执行文件。 您可以使用前面讨论的-a选项针对which命令来更改它。

Because the whereis command only uses paths hard-coded into the command, you may not always find what you are looking for. If you are searching for a program you think might be installed in a directory not listed in the man pages for the whereis command, you might want to use the which command with the-aoption to find all occurrences of the command throughout the system.

因为whereis命令仅使用硬编码到命令中的路径,所以您可能并不总是找到所需的内容。 如果您正在搜索某个程序,您认为可能会将其安装在whereis命令的手册页中未列出的目录中,则可能需要将which命令与-a选项一起使用,以查找整个系统中所有出现的命令。

翻译自: /112674/how-to-find-files-and-folders-in-linux-using-the-command-line/

linux查找文件夹命令

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。