7

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.

Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
  • 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`. – lurker Jan 29 '14 at 17:47

5 Answers5

12

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
Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
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.

John Y
  • 14,123
  • 2
  • 48
  • 72
misja111
  • 329
  • 2
  • 11
  • 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? – Dave Heq May 09 '17 at 20:08
  • 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! – Thomas Praxl Jan 11 '18 at 03:12
  • 1
    A `which find` on Git Bash gives me `/usr/bin/find` instead. – Andy Jul 15 '19 at 19:16
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'
that other guy
  • 116,971
  • 11
  • 170
  • 194
2

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.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • Wow, this answer *completely* lost out to [the currently top-voted one](https://stackoverflow.com/a/21438636/95852) 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. – John Y Mar 04 '22 at 16:29
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.

hoijui
  • 3,615
  • 2
  • 33
  • 41
pyshcoguy
  • 11
  • 2