How can I find a file/directory that could be anywhere on linux command line? [closed]
Asked Answered
A

5

105

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.

Altorelievo answered 9/7, 2014 at 13:46 Comment(4)
Not sure what the issue is, as find -name "filename" finds files recursively in the current working directory.Nodose
Sorry if it's not clear, the file may not be in the current working directory. It could be anywhere on the computerAltorelievo
find /<directory mount point/part> -name <filename>Hereunto
Does this answer your question? finding a file on linux?Varico
M
165

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

Maraschino answered 9/7, 2014 at 13:57 Comment(5)
Thanks. Out of curiosity, will this work for partial filenames?Altorelievo
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 caseMaraschino
@Altorelievo you should also be able to use some sort of RE to find what you needMaraschino
Good to know! I didn't realize I could use regular expressions with find!Altorelievo
it is surprisingly slow. I have a scratch linux machine on EC2, and it can't find file in a minute.Meng
V
28

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

find / -name "something" 2>/dev/null
Voluptuous answered 16/8, 2018 at 6:36 Comment(1)
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 linuxTrip
S
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

Snooker answered 18/5, 2016 at 3:46 Comment(2)
have used both the find and locate solutions, and i can confirm locate is much fasterMonroy
find didn't output anything and took a long time to execute. locate gave out the file's location instantly.Agostino
V
8

If need to find nested in some dirs:

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

Or connected dirs:

find / -type d -wholename "*foo/bar"
Vilify answered 19/10, 2014 at 14:24 Comment(1)
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"Pettish
T
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"
Theocritus answered 25/3, 2020 at 12:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.