Finding Files in a Git Bash Terminal
Asked Answered
I

5

8

I am currently using Git Bash to navigate file directories and edit files. I want to know if there's a command to search the current directory and all directories in it for a file name.

Indentation answered 29/1, 2014 at 17:41 Comment(1)
git and bash are two totally different things. bash is the Unix (or Linux) shell. git is a software source control program. As others have answered, there's a Unix/Linux command find which does what you want, and it's independent of git and works in any available shell, including bash.Backscratcher
M
13

Yes, it comes with the find utility. To recursively search for a file named "somefile.txt" starting from the current working directory, the following should work:

find . -name somefile.txt
Maidservant answered 29/1, 2014 at 17:45 Comment(3)
see answer by @misja111 comment if using windows: Use /bin/find not just find.Frizette
On git for Win 2.28.0 $ find . -name "something" does anyhow not work recursively. Can anybody confirm or clarify?Calesta
@Calesta Your find might be pointing to the Windows version. See misja111's answer.Biquadrate
A
7

If you are using Git Bash on Windows, then it might well be that find will point to the Find tool that comes with Windows and not to the one provided by Git Bash. The Windows Find tool searches for text within a file or files, similar to Unix's grep.

You can verify by executing which find. If it points to your Windows folder, it's not the Unix tool.

To be sure that you are using the Unix find from the Git Bash shell, type /bin/find. Now the -name parameter will work as shown in other answers.

Amitosis answered 16/11, 2016 at 13:36 Comment(3)
I'm using git for Windows and find . -name somefile.txt does work. For instance, I try find . -name *.js and it finds all .js files recursively. It took a while initially, because, I don't know, Windows?Olivares
I had lots of trouble with a failing AppVeyor test that used find in the git bash. Thousand thanks for your answer! It saved my day. bin/find for the win!Phenyl
A which find on Git Bash gives me /usr/bin/find instead.Etan
D
6

If you want to find e.g. all java file names containing the word 'Test', recursively from the current directory, you can use

git ls-files '*Test*.java' 

To search for all files whose contents includes containing the word "FIXME", you can use

git grep 'FIXME'
Dyane answered 29/1, 2014 at 17:53 Comment(0)
H
3

Git Bash is a bash shell underneath, and as such all standard Unix utilities will be available. The standard find utility will work fine:

$ find . -name filename.java 

will find filename.java in your directory/subdirectories. Note that you have to escape wildcarding, otherwise the shell itself will interpret this e.g.

$ find . -name \*.java 

will give you all the .java files

find is powerful, but can be complex to use. Check out a tutorial here.

Hammurabi answered 29/1, 2014 at 17:45 Comment(1)
Wow, this answer completely lost out to the currently top-voted one because it was apparently 15 seconds too slow. I gotta give this one an upvote (its first one?!) because it provides a useful superset of the information in that other answer.Mckale
A
0

You can simply run this command on the git shell:

find / -name 'filename-you-want-search-regex-allowed'

This will search all the files below / across file system.

Antimicrobial answered 21/12, 2014 at 14:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.