Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Shell script to compare and delete files

Posted on 1998-06-26
4
Medium Priority
?
210 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

688 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