在 linux 上查找文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7459526/
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-05 06:14:27  来源:igfitidea点击:

finding a file on linux?

linuxdebian

提问by Andy

I am trying to find where index.html is located on my linux server, and was wondering if there was a command to do that. Very new to linux and appreciate any help I can get.

我试图找到 index.html 在我的 linux 服务器上的位置,并想知道是否有一个命令可以做到这一点。对 linux 非常陌生,感谢我能得到的任何帮助。

采纳答案by datalost

Find from root path find / -name "index.html"

从根路径查找 find / -name "index.html"

Find from current path find . -name "index.html"

从当前路径查找 find . -name "index.html"

回答by chown

Try this (via a shell):

试试这个(通过外壳):

update db
locate index.html

Or:

或者:

find /var -iname "index.html"

Replace /var with your best guess as to the directory it is in but avoid starting from /

将 /var 替换为您对其所在目录的最佳猜测,但避免从 / 开始

回答by xdazz

find /the_path_you_want_to_find -name index.html

find /the_path_you_want_to_find -name index.html

回答by paulsm4

In general, the best way to find any file in any arbitrary location is to start a terminal window and type in the classic Unix command "find":

通常,在任意位置查找任何文件的最佳方法是启动终端窗口并输入经典的 Unix 命令“find”:

find / -name index.html -print

Since the file you're looking for is the root file in the root directory of your web server, it's probably easier to find your web server's document root. For example, look under:

由于您要查找的文件是 Web 服务器根目录中的根文件,因此找到 Web 服务器的文档根目录可能更容易。例如,请查看以下内容:

/var/www/*

/var/www/*

Or type:

或输入:

find /var/www -name index.html -print

回答by Taylor Jasko

The below line of code would do it for you.

下面的代码行将为您完成。

find / -name index.html

find / -name index.html

However, on most Linux servers, your files will be located in /var/www or in your user directory folder /home/(user) depending on how you have it set up. If you're using a control panel, most likely it'll be under your user folder.

但是,在大多数 Linux 服务器上,您的文件将位于 /var/www 或您的用户目录文件夹 /home/(user) 中,具体取决于您的设置方式。如果您使用的是控制面板,它很可能位于您的用户文件夹下。

回答by avivamg

Solution: Use unix command find

解决方案:使用unix命令find

The find utility recursively descends the directory tree for each path listed, evaluating an expression (composed of the 'primaries' and 'operands') in terms of each file in the tree.

find 实用程序针对列出的每个路径递归地降低目录树,根据树中的每个文件计算表达式(由“主要”和“操作数”组成)。

  • You can make the find action be more efficient and smart by controlling it with regular expressions queries, file types, size thresholds, depths dimensions in subtree, groups, ownership, timestamps , modification/creation date and more.
  • In addition you can use operators and combine find requests such as or/not/and etc...
  • 您可以通过使用正则表达式查询、文件类型、大小阈值、子树中的深度维度、组、所有权、时间戳、修改/创建日期等来控制查找操作,从而使查找操作更加高效和智能。
  • 此外,您可以使用运算符并组合查找请求,例如 or/not/and 等...

The Traditional Formulawould be :

传统的公式是:

find <path> -flag <valueOfFlag>

Easy Examples

简单的例子

1.Find by Name- Find all package.jsonfrom my current location subtree hierarchy.

1.按名称查找 -package.json从我当前的位置子树层次结构中查找所有内容。

find . -name "package.json"

2.Find by Name and Type- find all node_modulesdirectories from ALL file system (starting from root hierarchy )

2.按名称和类型查找 -node_modules从所有文件系统中查找所有目录(从根层次结构开始)

sudo find / -name "node_modules" -type d

Complex Examples:

复杂的例子:

More Useful examples which can demonstrate the power of flag options and operators:

更多有用的例子可以证明标志选项和运算符的威力:

3.Regex and File Type- Find all javascript controllers variation names (using regex) javascript Filesonly in my app location.

3. 正则表达式和文件类型-仅在我的应用程序位置查找所有 javascript 控制器变体名称(使用正则表达式)javascript 文件

find /user/dev/app -name "*contoller-*\.js" -type f

-typefmeans file -namerelated to regular expression to any variation of controller string and dash with .jsat the end

-typef表示-name与正则表达式相关的文件到控制器字符串的任何变体,并以破折号.js结尾

4.Depth -Find all routes patterns directoriesin app directory no more than 3 dimensions( app/../../.. only and no more deeper)

4.深度 -不超过 3 个维度的app 目录中查找所有路线模式目录(仅 app/../../.. 且不能更深)

find app -name "*route*" -type d  -maxdepth 3

-typedmeans directory -namerelated to regular expression to any variation of route string -maxdepthmaking the finder focusing on 3 subtree depth and no more <yourSearchlocation>/depth1/depth2/depth3)

-typed表示-name与正则表达式相关的目录与路由字符串的任何变体,-maxdepth使查找器专注于 3 个子树深度而不是更多<yourSearchlocation>/depth1/depth2/depth3

5.File Size , Ownership and OR Operator- Find all files with names 'sample' or 'test' under ownership of root user that greater than 1 Mega and less than 5 Mega.

5.文件大小、所有权和 OR 运算符- 在 root 用户的所有权下查找大于 1 Mega 且小于 5 Mega 的名称为“sample”或“test”的所有文件。

find . \( -name "test" -or -name "sample" \)  -user root -size +1M -size -5M

-sizethreshold representing the range between more than(+) and less than(-) -userrepresenting the file owner -oroperator filters query for both regex matches

-size阈值表示大于(+) 和小于(-)之间的范围,-user表示-or两个正则表达式匹配的文件所有者运算符过滤器查询

6.Empty Files- find all empty directories in file system

6. Empty Files- 查找文件系统中的所有空目录

find / -type d -empty

7.Time Access, Modification and creation of files- find all files that were created/modified/access in directory in 10 days

7.时间访问、修改和创建文件- 查找 10 天内在目录中创建/修改/访问的所有文件

# creation (c)
find /test -name "*.groovy" -ctime -10d
# modification (m)
find /test -name "*.java" -mtime -10d
# access (a)
find /test -name "*.js" -atime -10d

8.Modification Size Filter- find all files that were modified exactly between a week ago to 3 weeks ago and less than 500kb and present their sizes as a list

8.修改大小过滤器- 查找一周前到 3 周前刚修改过且小于 500kb 的所有文件,并将它们的大小显示为列表

find /test -name "*.java" -mtime -3w -mtime +1w -size -500k | xargs du -h