Solved

Checking for "orphan" files

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
wipe a usb using python 5 64
Setting up static IP - Ubuntu server 16.04 12 97
SonarQube on Linux vs Windows 3 52
Linux FTP Error 5 33
This is the error message I got (CODE) Error caused by incompatible libmp3lame 3.98-2 with ffmpeg I've googled this error message and found out sometimes it attaches this note "can be treated with downgrade libmp3lame to version 3.97 or 3.98" …
Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
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.:
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.

828 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