Solved

Checking for "orphan" files

Posted on 2006-10-27
4
260 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
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.

910 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

25 Experts available now in Live!

Get 1:1 Help Now