Solved

Shell script to compare and delete files

Posted on 1998-06-26
4
200 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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

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" …
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 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…
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…

744 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

12 Experts available now in Live!

Get 1:1 Help Now