How can I find a file/directory that could be anywhere on linux command line?

LinuxBashFindKshLocate

Linux Problem Overview


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.

Linux Solutions


Solution 1 - Linux

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

Solution 2 - Linux

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

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

Solution 3 - Linux

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

Solution 4 - Linux

If need to find nested in some dirs:

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

Or connected dirs:

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

Solution 5 - Linux

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"

Solution 6 - Linux

If it is a command file you are looking for, the fastest and most accurate way is with

which "commandname"

That will show you the actual file being used for the command, even if you have many files with the same name on the system.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjohncorserView Question on Stackoverflow
Solution 1 - LinuxRussell UhlView Answer on Stackoverflow
Solution 2 - Linuxuser52028778View Answer on Stackoverflow
Solution 3 - LinuxthucnguyenView Answer on Stackoverflow
Solution 4 - LinuxAndrewView Answer on Stackoverflow
Solution 5 - LinuxShabeer KView Answer on Stackoverflow
Solution 6 - LinuxDouglas MilnesView Answer on Stackoverflow