Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 300
  • Last Modified:

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!
0
hankknight
Asked:
hankknight
  • 3
1 Solution
 
sunnycoderCommented:
What if there are multiple files by same name in different directories? Do the files contain absolute paths?
0
 
hankknightAuthor Commented:
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.

0
 
sunnycoderCommented:
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
0
 
sunnycoderCommented:
Can combine it to one script wihout using a temp file

find /top/dir -type f  | while read filename     # for all files
do
      fname=`echo $filename | sed 's:.*/\(.*\)$:\1:'`      #get the name of the file
      ret=`grep -R "$fname" /top/dir`                            #grep for it recursively
      if [ -z "$ret" ]                                                        #not found
      then
             echo "File $fname was not found"
      else
             echo "File $fname was found"
      fi
done
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now