Linux 如何在 CentOS 命令行中搜索文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21046354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:49:34  来源:igfitidea点击:

How to search for a file in the CentOS command line

linux

提问by Dan

I am using CentOS minimal version and I am trying to locate a file, but I have no idea how to search all of the server for the file. I am sure there is a command out there to do it, can anyone help me?

我正在使用 CentOS 最小版本并且我正在尝试定位一个文件,但我不知道如何在所有服务器中搜索该文件。我确定那里有一个命令可以做到这一点,有人可以帮助我吗?

采纳答案by Dennis Kerrisk

Try this command:

试试这个命令:

find / -name file.look

回答by Martin Dinov

CentOS is Linux, so as in just about all other Unix/Linux systems, you have the findcommand. To search for files within the current directory:

CentOS 是 Linux,因此在几乎所有其他 Unix/Linux 系统中,您都拥有该find命令。要在当前目录中搜索文件:

find -name "filename"

You can also have wildcards inside the quotes, and not just a strict filename. You can also explicitly specify a directory to start searching from as the first argument to find:

您还可以在引号内使用通配符,而不仅仅是严格的文件名。您还可以明确指定一个目录作为查找的第一个参数开始搜索:

find / -name "filename"

will look for "filename" or all the files that match the regex expression in between the quotes, starting from the root directory. You can also use single quotes instead of double quotes, but in most cases you don't need either one, so the above commands will work without any quotes as well. Also, for example, if you're searching for java files and you know they are somewhere in your /home/username, do:

将从根目录开始查找“文件名”或所有与引号之间的正则表达式匹配的文件。您也可以使用单引号代替双引号,但在大多数情况下,您不需要任何一个,因此上述命令也可以在没有任何引号的情况下运行。另外,例如,如果您正在搜索 java 文件并且您知道它们位于您的 /home/username 中,请执行以下操作:

find /home/username -name *.java

There are many more options to the find command and you should do a:

find 命令还有更多选项,您应该执行以下操作:

man find

to learn more about it.

了解更多信息。

One more thing: if you start searching from / and are not root or are not sudo running the command, you might get warnings that you don't have permission to read certain directories. To ignore/remove those, do:

还有一件事:如果您从 / 开始搜索并且不是 root 用户或者不是 sudo 运行命令,您可能会收到警告,提示您无权读取某些目录。要忽略/删除这些,请执行以下操作:

find / -name 'filename' 2>/dev/null

That just redirects the stderr to /dev/null.

这只是将 stderr 重定向到 /dev/null。