1

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?

3 Answers 3

4

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.

2
  • it's preferred with grep as names are highlighted ^_^
    – wisdom
    Apr 19, 2012 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 :-) . May 11, 2012 at 16:30
5

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.

0

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.

4
  • 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, 2012 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, 2012 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, 2012 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, 2012 at 21:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .