Search for files and print their full path
10,977
Solution 1
You can use the command find
for these purposes.
Try
find /media/X | grep filename
You can achieve the same results without grep
(as @geekosaur points out), but find
's syntax can be hassle if you're already used to grep
.
Solution 2
ls
and grep
aren't really the right tools for that; you want the find
command.
find /media/X -name '*filename*'
This also lets you look for other conditions such as by age.
Related videos on Youtube
Author by
wisdom
Updated on September 18, 2022Comments
-
wisdom over 1 year
ls -R /media/X | grep filename
lets me search for file names, but it only prints the file name and not the directory it resides in. How can I print the file name and its directory? -
wisdom about 12 yearsit's preferred with
grep
as names are highlighted ^_^ -
Eroen about 12 yearsI just want to add that the amount of time it takes to traverse a directory tree in this way depends heavily on the underlying filesystem.
-
wisdom about 12 yearsThanks alot for your great answer as it's fastest !...but how about locating under /media/ path ? how do I do that ? seems I can't !
-
CodeGnome about 12 years@wisdom mlocate will prune /media by default. If you want to index removable media, you can edit PRUNEPATHS in
/etc/updatedb.conf
to suit yourself. If you remove the entry for /media, it will be included in the index the next time updatedb is run. Also note that your locate results may be out of date, since the index is only as current as the last invocation of updatedb, which is why /media is excluded by default. -
Daniel Andersson about 12 years@wisdom: Still,
find /media/X -name '*filename*' | grep filename
would be quicker (how much depends on how many files there are) and highlight the output. And you will get the warm fuzzy feeling of using the right tools for the job :-) .