linux查找文件夹命令

Most people use 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 -name argument. 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 the cd 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 -iname option with the find command. It is the case insensitive version of the -name command.

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

If find doesn’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, run man find  in 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 -b option 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, type man locate or man mlocate  in 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 command which firefox displays 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 -a option 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, typing whereis firefox at 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 -b option. For example, the command whereis -b firefox will display only /usr/bin/firefox as 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, type man whereis in 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 -a option, 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 -a option to find all occurrences of the command throughout the system.

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

翻译自: https://www.howtogeek.com/112674/how-to-find-files-and-folders-in-linux-using-the-command-line/

linux查找文件夹命令

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

  1. python中glob模块怎么下_如何在Python中使用glob.glob模块搜索子文件夹?

    如何在Python中使用glob.glob模块搜索子文件夹? 我想在文件夹中打开一系列子文件夹,找到一些文本文件并打印一些文本文件行. 我用这个: configfiles = glob.glob('C ...

  2. java 查找链表中间元素_如何在Java中一次性查找Java中链表的中间元素

    如何在一次传递中找到LinkedList的中间元素?这是一个 Java 和非Java程序员面试时经常被问到的编程问题.这个问题类似于检查回文或计算阶乘,有时也会要求编写代码.为了回答这个问题,候选人必 ...

  3. linux 如何让.开头的文件不隐藏_如何在Mac上显示隐藏文件?苹果mac显示隐藏文件夹方法

    与任何操作系统一样,macOS会将重要文件隐藏起来,以防止意外删除它们并因此而损坏系统.但是,在某些情况下,您可能需要在Mac上显示隐藏文件,例如,浏览" 库"文件夹并清除旧日志, ...

  4. linux终端删除文件命令_如何在Linux终端中删除文件和目录

    linux终端删除文件命令 Fatmawati Achmad Zaenuri/Shutterstock.comFatmawati Achmad Zaenuri / Shutterstock.com T ...

  5. 在linux里复制文件命令_如何在Linux上使用“安装”命令复制文件

    在linux里复制文件命令 Fatmawati Achmad Zaenuri/Shutterstock.com Fatmawati Achmad Zaenuri / Shutterstock.com ...

  6. linux uniq命令_如何在Linux上使用uniq命令

    linux uniq命令 Fatmawati Achmad Zaenuri/ShutterstockFatmawati Achmad Zaenuri / Shutterstock The Linux ...

  7. linux使用find命令_如何在Linux中使用FIND

    linux使用find命令 在最近的Opensource.com文章中 ,刘易斯·考尔斯介绍了find命令. 在日常工具箱中, find是功能更强大,更灵活的命令行程序之一,因此值得花一些时间在上面. ...

  8. linux 开机自动运行命令_如何在Linux终端同时运行多个Linux命令

    在一行中运行两个或多个命令可以节省大量时间,并在Linux中提高效率.在Linux中,有三种方法可以在一行中运行多个命令: ; Command 1 ; Command 2 首先运行Command1,然 ...

  9. linux xargs命令_如何在Linux中使用xargs命令?

    linux xargs命令 The xargs command allows us to pass the output of one command as the input for another ...

最新文章

  1. python语言特点有哪些-Python是什么?Python有什么特点?
  2. Unity3D GUI中的图片尾随鼠标旋转脚本
  3. python中da_Python中字符的编码与解码
  4. 面条html5,使用 babel 全家桶模块化古老的面条代码
  5. tomcat报错“The specified JRE installation does not exist”
  6. Unity3D 5.x 简单实例 - 发射炮弹
  7. Codeforces Round #456 (Div. 2)
  8. 【2019杭电多校第八场1011=HDU6667】Roundgod and Milk Tea(贪心)
  9. 神经网络和机器学习基础入门分享
  10. 计算机辅助logo设计与实现,新手都会看的logo设计师53条规则
  11. idea发现git出现cannot run git:cannot identify version of git executable :no response
  12. 数据库SQL的高级查询
  13. 新版本微信PC端小程序打不开 一直加载 白屏等问题及解决方案
  14. 【网络】inux流量控制器TC(Traffic Control)
  15. 什么是SPU和SKU
  16. 浙江农林大学ACM集训队2019级队长退役回忆录(未实际退役版本)
  17. Android APP全局黑白化实现方案
  18. 部门来了个跳槽出来的测试开发,听说是00后,上来一顿操作给我看呆了...
  19. Cisco IP Phone 7960/7940 SCCP firmware 转换成SIP firmware过程
  20. 「Python条件结构」银行利息计算

热门文章

  1. 什么是Web应用程序
  2. 深度学习常用激活函数
  3. python findall用法_python re.findall 使用
  4. Flume-NG源码分析-整体结构及配置载入分析
  5. MongoDB自学笔记8--- 3.3 MongoDB Shell简介及简单使用
  6. 使用PyCharm运行.sh文件
  7. 西奥服务器修改楼层参数,杭州西奥OHCON8503电梯LMCB板楼层表(井道)自学习
  8. 【ES实战】ES6.X Join
  9. JavaCV音视频开发宝典:使用JavaCV读取海康平台或海康网络摄像头sdk回调录像回放视频PS码流并解析预览图像
  10. 前n个自然数的阶乘之和(单数,双数,全部)