Link to home
Start Free TrialLog in
Avatar of hankknight
hankknightFlag for Canada

asked on

Checking for "orphan" files

Hello,

I would like to "clean up" a directory AND its sub-directories by deleting ALL scripts and images that are not called
by any other script or webpage within that directory or its sub-directories.

So I want a list of ALL files in my directory (excluding any file that starts with the text 'index') where the file name is not included as text in any other file in that directory or its sub-directories.

How can this be done?

Thanks!
Avatar of sunnycoder
sunnycoder
Flag of India image

What if there are multiple files by same name in different directories? Do the files contain absolute paths?
Avatar of hankknight

ASKER

Thanks for the good questions.  I hadn't throughly thought this through before you asked them.

     >>Do the files contain absolute paths?

            Not always, sometimes the path is relative, sometimes it is absolute and sometimes
            the path name is called from a variable.

      >>What if there are multiple files by same name in different directories?

            That is a good question and it makes a good point.  

To be safe, I will leave ALL files called "image.gif" in ALL subdirectories if ANY script contains the text "image.gif" at least once. In this case, better safe than sorry.

find /top/dir -type f  > filename.txt

cat filename.txt | while read filename
do
      fname=`echo $filename | sed 's:.*/\(.*\)$:\1:'`
      ret=`grep -R "$fname" /top/dir`
      if [ -z "$ret" ]
      then
             echo "File $fname was not found"
      else
             echo "File $fname was found"
      fi
done

Make sure ot works as expected on test data. When you are sure, replace echo command in if then part with rm -f $filename
ASKER CERTIFIED SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial