140

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.

4
  • 2
    Not sure what the issue is, as find -name "filename" finds files recursively in the current working directory. Commented Jul 9, 2014 at 13:55
  • 1
    Sorry if it's not clear, the file may not be in the current working directory. It could be anywhere on the computer Commented Jul 9, 2014 at 13:56
  • 1
    find /<directory mount point/part> -name <filename> Commented Mar 10, 2020 at 18:44
  • Does this answer your question? finding a file on linux? Commented Oct 20, 2020 at 20:23

5 Answers 5

214

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

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks. Out of curiosity, will this work for partial filenames?
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
Can it be made case-insensitive with RegEx's /i?
You can specify case insensitive with -iname instead of -name.
@johncorser you should also be able to use some sort of RE to find what you need
Good to know! I didn't realize I could use regular expressions with find!
it is surprisingly slow. I have a scratch linux machine on EC2, and it can't find file in a minute.
43

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

find / -name "something" 2>/dev/null 

1 Comment

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
14

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

2 Comments

have used both the find and locate solutions, and i can confirm locate is much faster
find didn't output anything and took a long time to execute. locate gave out the file's location instantly.
11

To find a nested in some dirs:

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

Or connected dirs:

find / -type d -wholename "*foo/bar" 

1 Comment

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"
2

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" 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.