101

Ideally, I would be able to use a program like

find [file or directory name]

to report the paths with matching filenames/directories. Unfortunately this seems to only check the current directory, not the entire folder.

I've also tried locate and which, but none find the file, even though I know its on the computer somewhere.

Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
johncorser
  • 9,262
  • 17
  • 57
  • 102

5 Answers5

156

"Unfortunately this seems to only check the current directory, not the entire folder". Presumably you mean it doesn't look in subdirectories. To fix this, use find -name "filename"

If the file in question is not in the current working directory, you can search your entire machine via

find / -name "filename"

This also works with stuff like find / -name "*.pdf", etc. Sometimes I like to pipe that into a grep statement as well (since, on my machine at least, it highlights the results), so I end up with something like

find / -name "*star*wars*" | grep star

Doing this or a similar method just helps me instantly find the filename and recognize if it is in fact the file I am looking for.

Russell Uhl
  • 4,181
  • 2
  • 18
  • 28
  • Thanks. Out of curiosity, will this work for partial filenames? – johncorser Jul 09 '14 at 14:02
  • 6
    Yes, provided you use the asterisk. Note that it is case sensitive. If I have a file called STARTUP I want to find, `find / -name "*ART*"` will find it. note that `find / -name "*art*"` will NOT locate that file, since "art" is lower case, whereas "STARTUP" is upper case – Russell Uhl Jul 09 '14 at 14:10
  • 1
    @johncorser you should also be able to use some sort of RE to find what you need – Russell Uhl Jul 09 '14 at 14:11
  • Good to know! I didn't realize I could use regular expressions with find! – johncorser Jul 09 '14 at 14:22
  • it is surprisingly slow. I have a scratch linux machine on EC2, and it can't find file in a minute. – ikamen Nov 06 '19 at 08:43
25

To get rid of permission errors (and such), you can redirect stderr to nowhere

find / -name "something" 2>/dev/null
user52028778
  • 27,164
  • 3
  • 36
  • 42
  • 4
    Thankyou, this _actually_ answers the question "how do I find a file that could be anywhere". For linux noobs like me that don't understand standard folder structures and aren't interested in typing three hundred `cd` commands to find the file, and aren't interested in searching through hundreds of permission denied lines, this is the perfect answer. For my specific case I'm trying to find the sqlcmd tool within the SQL Server linux – Nick.Mc Mar 08 '21 at 01:36
9

The find command will take long time, the fastest way to search for file is using locate command, which looks for file names (and path) in a indexed database (updated by command updatedb).

The result will appear immediately with a simple command:

locate {file-name-or-path}

If the command is not found, you need to install mlocate package and run updatedb command first to prepare the search database for the first time.

More detail here: https://medium.com/@thucnc/the-fastest-way-to-find-files-by-filename-mlocate-locate-commands-55bf40b297ab

thucnguyen
  • 1,706
  • 15
  • 14
8

If need to find nested in some dirs:

find / -type f -wholename "*dirname/filename"

Or connected dirs:

find / -type d -wholename "*foo/bar"
Andrew
  • 36,676
  • 11
  • 141
  • 113
  • How do we use both files and directories in a single find command? i am trying something like this "find / -maxdepth 1 -type d,f " but I get a message on command line saying - " find: Arguments to -type should contain only one letter" – Ronnie Jan 29 '21 at 20:36
0

I hope this comment will help you to find out your local & server file path using terminal

 find "$(cd ..; pwd)" -name "filename"

Or just you want to see your Current location then run

 pwd "filename"
Shabeer K
  • 1,489
  • 16
  • 23