Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Shell script to compare and delete files

Posted on 1998-06-26
4
Medium Priority
?
211 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 280 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
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.:
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses
Course of the Month12 days, 7 hours left to enroll

971 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