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?
You can use the command
find for these purposes.
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 aren’t really the right tools for that; you want the
find /media/X -name '*filename*'
This also lets you look for other conditions such as by age.
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.