views:

33

answers:

1

Hey guys,

I'm trying to write a shell script under linux, which lists all folders (recursively) with a certain name and no symlink pointing to it.

For example, I have:

/home/htdocs/cust1/typo3_src-4.2.11
/home/htdocs/cust2/typo3_src-4.2.12
/home/htdocs/cust3/typo3_src-4.2.12

Now I want to go through all subdirectories of /home/htdocs and find those folders typo3_*, that are not pointed to from somewhere.

Should be possible with a shellscript or a command, but I have no idea how.

Thanks for you help

Stefan

+1  A: 

I think none of the common file systems store if there are symlinks pointing to this file in the file node, so you would have to scan all other files to see if it is a symlink to this one. If you don't limit your depth of search to a certain level, this might take a very long time. If you want to perform that search in /home/htdocs, for example, it would work something like this:

# find specified folders:
find /home/htdocs -name 'typo3_*' -type d | while read folder; do
    # list all symlinks pointing to $folder
    find -L /home/htdocs -samefile "$folder"|grep -v "$folder\$"
done
soulmerge
This will fail if any directories have spaces in their names. You should pipe the first `find` into a `while read` loop. Also, under certain unusual circumstances, the `grep -v` *might* falsely eliminate directories since there aren't any anchors in the pattern.
Dennis Williamson
@Dennis Williamson: Thanks for the feedback, updated answer
soulmerge