Solved

Checking for "orphan" files

Posted on 2006-10-27
4
251 Views
Last Modified: 2010-04-20
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
Comment
Question by:hankknight
  • 3
4 Comments
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
What if there are multiple files by same name in different directories? Do the files contain absolute paths?
0
 
LVL 16

Author Comment

by:hankknight
Comment Utility
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
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
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
 
LVL 45

Accepted Solution

by:
sunnycoder earned 500 total points
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now