Locate directories containing files
From LinuxServerTech
You can use find, of course, to locate files. However, if you only want a listing of directories which contain files (or file patterns), it is a little more difficult. The following command locates all php files (for simplicity, *.php), and returns only the TOP directory names which contain them.
Note: this is a special purpose command that specifically locates all directories under a ISPConfig installation containing php files (a quick and dirty approximation to locates sites using PHP).
for dir in `find /home/www/ -type f -name "*.php" | cut -d \/ -f 4 | uniq | grep web` \ do ls -ablp | grep $dir \ done \ | grep lrwx | cut -d \> -f 1 | cut -d \: -f 2 | cut -d ' ' -f 2
What it does:
- find /home/www/ -type f -name "*.php" | cut -d \/ -f 4 | uniq | grep web
Locate all *.php files (note, use -iname for case insensitive), then removes the fourth field (/home/web/fourth field), only returns the unique entries, then looks for web in the name (passing only the lines that contain this, directories in this case are of the form /home/www/web### where ### is a number).
- do ls -ablp | grep $dir
for each of these entries, perform an ls -ablph (they are all symbolic links)
- grep lrwx
Get only the entries that are links (the rwx might not be the same on your machine)
- cut -d \> -f 1
Get the second entry for each row, using a > for the delimiter. The lines returned by ls -ablph are in the form file information -> target for symbolic links
- cut -d \: -f 2
I don't remember what this does. Appearantly, there are at least three colons in the output from the above, and we are grabbing the last one.
- cut -d ' ' -f 2
Grab the third thing using spaces to separate.
