Solved

Checking for "orphan" files

Posted on 2006-10-27
4
270 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
ID: 17818753
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
ID: 17819558
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
ID: 17819649
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
ID: 17819662
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

775 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