Solved

Shell script to compare and delete files

Posted on 1998-06-26
4
206 Views
Last Modified: 2010-04-20
I have two trees under the directories

/usr

and

/bak/usr

I would like to use a command or a short bash script that will
delete everything under /bak/usr which exists and has the same
date and relative path as a file under /usr. How would I do this?

Thanks in advance,
Nicholas Waltham
0
Comment
Question by:nwaltham
  • 2
  • 2
4 Comments
 
LVL 2

Accepted Solution

by:
mlev earned 70 total points
ID: 1637429
I think this should do:
(I assumed nothing in /bak/usr would be newer than in /usr)

#!/bin/sh

find /usr -depth |
while read file
do
  bakfile="/bak$file"
  if [ -e "$bakfile" -a ! "$file" -nt "$bakfile" ]
  then
    if [ -d "$bakfile" ]
    then
      rmdir "$bakfile" 2>/dev/null
    else
      rm -f "$bakfile"
    fi
  fi
done

0
 

Author Comment

by:nwaltham
ID: 1637430
Hello - thanks for your script. Its possible /bak/usr could have newer files, since its from a different system. I guess the modification to the script would only need to be slight...
0
 
LVL 2

Expert Comment

by:mlev
ID: 1637431
You can make the 7th line read
  if [ -e "$bakfile" -a ! "$file" -nt "$bakfile" -a ! "$file" -ot "$bakfile" ]
but then identical directories won't be removed, since their date will change as their contents are removed. To solve this:

#!/bin/sh

find /usr -depth |
while read file
do
  bakfile="/bak$file"
  if [ -e "$bakfile" -a ! "$file" -nt "$bakfile" -a ! "$file" -ot "$bakfile" ]
  then
    echo "$bakfile"
  fi
done |
while read bakfile
do
  if [ -d "$bakfile" ]
  then
    rmdir "$bakfile" 2>/dev/null
  else
    rm -f "$bakfile"
  fi
done

Please note that even this way, in some cases directories still won't be removed e.g. because their date changed last time you ran this script. Maybe you should think of a different criterion for directories than date (e.g. empty and exist in both /bak/usr and /usr)

0
 

Author Comment

by:nwaltham
ID: 1637432
Thanks for your help!
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Suggested Solutions

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.…
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…
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.:

839 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