Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find file in directory from command line

In editors/ides such as eclipse and textmate, there are shortcuts to quickly find a particular file in a project directory.

Is there a similar tool to do full path completion on filenames within a directory (recursively), in bash or other 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 =/

like image 352
whoisbenli Avatar asked Mar 18 '09 02:03

whoisbenli


People also ask

How do I find files inside a folder in command prompt?

You can use the DIR command by itself (just type “dir” at the Command Prompt) to list the files and folders in the current directory.

Can you search for a file in CMD?

Searching for files and folders using the command line is very easy. Follow the instructions below: In the command prompt, type dir "search term*" /s but replace the words “search term” with the file name or a part of the name you remember.


5 Answers

find /root/directory/to/search -name 'filename.*'
# Directory is optional (defaults to cwd)

Standard UNIX globbing is supported. See man find for more information.

If you're using Vim, you can use:

:e **/filename.cpp

Or :tabn or any Vim command which accepts a filename.

like image 195
strager Avatar answered Oct 04 '22 12:10

strager


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).

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:

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:

grep ERROR $(find . -type f -name 'btree*.c')
vi $(find . -type f -name 'btree*.c')

to do this.

like image 27
paxdiablo Avatar answered Oct 04 '22 14:10

paxdiablo


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:

# 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 "/")

like image 38
Andy White Avatar answered Oct 04 '22 14:10

Andy White


I use ls -R, piped to grep like this:

$ ls -R | grep -i "pattern"

where -R means recursively list all the files, and -i means case-insensitive. Finally, the patter could be something like this: "std*.h" or "^io" (anything that starts with "io" in the file name)

like image 23
BlueDiary9 Avatar answered Oct 04 '22 13:10

BlueDiary9


I use this script to 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, here is another link on Stack Overflow that may help.

like image 38
SnapShot Avatar answered Oct 04 '22 13:10

SnapShot