bash 从命令行查找目录中的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/656741/
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
Find file in directory from command line
提问by whoisbenli
In editors/ides such as eclipse and textmate, there are shortcuts to quickly find a particular file in a project directory.
在 eclipse 和 textmate 等编辑器/ide 中,有快捷方式可以快速找到项目目录中的特定文件。
Is there a similar tool to do full path completion on filenames within a directory (recursively), in bash or other shell?
是否有类似的工具可以在 bash 或其他 shell 中(递归地)对目录中的文件名进行完整路径补全?
I have projects with alot of directories, and deep ones at that (sigh, java). Hitting tab in the shell only cycles thru files in the immediate directory, thats not enough =/
我有很多目录的项目,还有很深的目录(叹气,java)。在 shell 中点击 tab 只会循环通过直接目录中的文件,这还不够=/
回答by strager
find /root/directory/to/search -name 'filename.*'
# Directory is optional (defaults to cwd)
Standard UNIX globbing is supported. See man find
for more information.
支持标准 UNIX 通配符。有关man find
更多信息,请参阅。
If you're using Vim, you can use:
如果你使用 Vim,你可以使用:
:e **/filename.cpp
Or :tabn
or any Vim command which accepts a filename.
或者:tabn
任何接受文件名的 Vim 命令。
回答by paxdiablo
If you're looking to do something with a list of files, you can use find combined with the bash $()
construct (better than backticks since it's allowed to nest).
如果您想对文件列表执行某些操作,您可以将 find 与 bash$()
构造结合使用(比反引号更好,因为它允许嵌套)。
for example, say you're at the top level of your project directory and you want a list of all C files starting with "btree". The command:
例如,假设您位于项目目录的顶层,并且想要一个所有以“btree”开头的 C 文件的列表。命令:
find . -type f -name 'btree*.c'
will return a list of them. But this doesn't really help with doing something with them.
将返回它们的列表。但这并不能真正帮助与他们做些什么。
So, let's further assume you want to search all those file for the string "ERROR" or edit them all. You can execute one of:
因此,让我们进一步假设您要在所有这些文件中搜索字符串“ERROR”或全部编辑它们。您可以执行以下操作之一:
grep ERROR $(find . -type f -name 'btree*.c')
vi $(find . -type f -name 'btree*.c')
to do this.
去做这个。
回答by Andy White
When I was in the UNIX world (using tcsh (sigh...)), I used to have all sorts of "find" aliases/scripts setup for searching for files. I think the default "find" syntax is a little clunky, so I used to have aliases/scripts to pipe "find . -print" into grep, which allows you to use regular expressions for searching:
当我在 UNIX 世界(使用 tcsh(叹气...))时,我曾经有各种“查找”别名/脚本设置来搜索文件。我认为默认的“find”语法有点笨拙,所以我曾经使用别名/脚本将“find . -print”传递给grep,它允许您使用正则表达式进行搜索:
# finds all .java files starting in current directory
find . -print | grep '\.java'
#finds all .java files whose name contains "Message"
find . -print | grep '.*Message.*\.java'
Of course, the above examples can be done with plain-old find, but if you have a more specific search, grep can help quite a bit. This works pretty well, unless "find . -print" has too many directories to recurse through... then it gets pretty slow. (for example, you wouldn't want to do this starting in root "/")
当然,上面的例子可以用普通的 find 来完成,但如果你有更具体的搜索,grep 可以提供很多帮助。这工作得很好,除非“find . -print”有太多目录需要递归......然后它会变得很慢。(例如,您不想从根“/”开始执行此操作)
回答by SnapShot
I use this scriptto quickly find files across directories in a project. I have found it works great and takes advantage of Vim's autocomplete by opening up and closing an new buffer for the search. It also smartly completes as much as possible for you so you can usually just type a character or two and open the file across any directory in your project. I started using it specifically because of a Java project and it has saved me a lot of time. You just build the cache once when you start your editing session by typing :FC (directory names). You can also just use . to get the current directory and all subdirectories. After that you just type :FF (or FS to open up a new split) and it will open up a new buffer to select the file you want. After you select the file the temp buffer closes and you are inside the requested file and can start editing. In addition, hereis another link on Stack Overflow that may help.
我用这个脚本快速查找项目中跨目录的文件。我发现它很好用,并且通过打开和关闭一个新的搜索缓冲区来利用 Vim 的自动完成功能。它还尽可能为您完成尽可能多的工作,因此您通常只需键入一两个字符即可在项目中的任何目录中打开文件。我开始使用它是因为一个 Java 项目,它为我节省了很多时间。当您通过键入 :FC(目录名称)开始编辑会话时,您只需构建一次缓存。您也可以只使用 . 获取当前目录和所有子目录。之后,您只需键入 :FF (或 FS 以打开新的拆分),它将打开一个新缓冲区以选择您想要的文件。选择文件后,临时缓冲区关闭,您位于请求的文件中并可以开始编辑。这是堆栈溢出的另一个链接,可能会有所帮助。
回答by Marquistador
locate <file_pattern>
locate <file_pattern>
*** find
will certainly work, and can target specific directories. However, this command is slower than the locate
command. On a Linux OS, each morning a database is constructed that contains a list of all directory and files, and the locate
command efficiently searches this database, so if you want to do a search for files that weren't created today, this would be the fastest way to accomplish such a task.
***find
肯定会工作,并且可以针对特定目录。但是,此命令比locate
命令慢。在 Linux 操作系统上,每天早上都会构建一个包含所有目录和文件列表的数据库,并且该locate
命令可以有效地搜索该数据库,因此如果您想搜索今天没有创建的文件,这将是完成此类任务的最快方法。
回答by Stefan Kendall
http://content.hccfl.edu/pollock/Unix/FindCmd.htm
http://content.hccfl.edu/pollock/Unix/FindCmd.htm
The linux/unix "find" command.
linux/unix“查找”命令。
回答by Jonathan Leffler
Yes, bash
has filename completion mechanisms. I don't use them myself (too lazy to learn, and I don't find it necessary often enough to make it urgent), but the basic mechanism is to type the first few characters, and then a tab; this will extend the name as far as it can (perhaps not at all) as long as the name is unambiguous. There are a boatload of Emacs-style commands related to completion in the good ol' man page.
是的,bash
有文件名补全机制。我自己不使用它们(懒得学习,而且我觉得没有必要经常使用以使其紧急),但基本机制是键入前几个字符,然后是制表符;只要名称是明确的,这将尽可能地扩展名称(也许根本不会)。在 good ol' 手册页中有大量与完成相关的 Emacs 风格的命令。