Coder Perfect

On the linux command line, how can I discover a file/directory that could be anywhere? [closed]

Problem

In an ideal world, I’d be able to use anything like

find [file or directory name]

to return a list of paths that match the filenames/directories. Regrettably, it appears that this only checks the current directory and not the full folder.

I’ve also tried locate and which, but none of these seem to discover the file, despite the fact that I know it’s someplace on the computer.

Asked by johncorser

Solution #1

“Unfortunately, it appears that this merely checks the current directory rather than the full folder.” You’re probably referring to the fact that it doesn’t look in subdirectories. Use find -name “filename” to fix this.

If the file you’re looking for isn’t in your current working directory, you can search your entire computer using this method.

find / -name "filename"

This also works with search / -name “*.pdf” and similar commands. I prefer to pipe that through a grep statement as well (as it highlights the results on my machine), so I end up with something like

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

This or a similar strategy just aids me in quickly locating the filename and determining whether it is, in fact, the file I am looking for.

Answered by Russell Uhl

Solution #2

You can route stderr to nowhere to avoid permission errors (and other issues).

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

Answered by user52028778

Solution #3

If you need to find something nested in a directory, use the following command:

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

Or connected dirs:

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

Answered by Andrew

Solution #4

The find command will take a long time; instead, use the locate command, which searches for file names (and paths) in an indexed database (updated by command updatedb).

With a single command, the result will display right away:

locate {file-name-or-path}

If the command is not found, you must first install the mlocate package and perform the updatedb command to prepare the search database.

More information may be found at https://medium.com/@thucnc/the-fastest-way-to-find-files-by-filename-mlocate-locate-commands-55bf40b297ab.

Answered by thucnguyen

Solution #5

I hope this comment aids you in determining your local and server file paths using terminal.

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

Alternatively, if you simply want to view your current location, run.

 pwd "filename"

Answered by Shabeer K

Post is based on https://stackoverflow.com/questions/24655436/how-can-i-find-a-file-directory-that-could-be-anywhere-on-linux-command-line