Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
add comment

3 Answers

up vote 3 down vote accepted

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.

share|improve this answer
    
it's preferred with grep as names are highlighted ^_^ –  wisdom Apr 19 '12 at 20:25
    
@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 :-) . –  Daniel Andersson May 11 '12 at 16:30
add comment

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.

share|improve this answer
add comment

The currently accepted answer from @Dennis will consume a vast amount of CPU and/or IO if there are a lot of files under /media/X since find will recursively list everything under the defined path unless some limiting flags are used. In the average case this may not matter, but deep recursion followed by grepping an excessively long list is certainly not optimal.

You will generally get faster results using the locate command (e.g. from the mlocate package). For example:

$ locate virtualbox/README.Debian
/usr/share/doc/virtualbox/README.Debian.gz
/usr/share/doc/virtualbox/README.Debian.html

The main limitation of this approach is that the various locate packages generally will not index private home directories under ecryptfs, which may be a concern for Ubuntu users. Since the OP is using Ubuntu, I'd recommend using locate for system files, and find with some sensible limiting flags for anything stored within an ecryptfs mount.

Lastly, it's worth mentioning that using find without an absolute path as the starting point may not do what the OP wants anyway. The original question was how to return a filename along with its path, so unless a relative path is acceptable, locate is again more likely to return the proper result.

share|improve this answer
    
I 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. –  Eroen Apr 19 '12 at 22:24
    
Thanks 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 ! –  wisdom Apr 19 '12 at 22:51
    
@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. –  CodeGnome Apr 20 '12 at 20:15
    
@CodeGnome that's great ! Wow....but still can't find files in my /media/ even it's included ! why ? I do mlocate -b 'fedora' but can't find it in my /media/ –  wisdom Apr 20 '12 at 21:02
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.