Solved

Need borne shell to delete all but last two backups

Posted on 2012-04-11
3
408 Views
Last Modified: 2012-04-12
I wrote this shell scrip whose intent is to delete all but the last two files that match a particular pattern. I am attempting to set the variable $backupcnt to the number of files that match the pattern in line 4.

The script fails on line 4  with: "line 4: backupcnt: command not found" and on the while loop test: " line 6: [: -ge: unary operator expected"


#!/bin/bash
set -v -x
cd /opt/backup
backupcnt = $(ls -t -1 | grep $1_`hostname` | wc -l)

while [ $backupcnt -ge 2 ]
do
        ls -t -1 | grep $1_`hostname` > temp.txt
        echo cat temp.txt
        cat temp.txt
        echo rm `cat temp.txt | tail -1`
        rm `cat temp.txt | tail -1`
        backupcnt = $(ls -t -1 | grep $1_`hostname` | wc -l)
done
0
Comment
Question by:Christopher Schene
[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
3 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 37836100
backupcnt=$(ls -t -1 | grep $1_`hostname` | wc -l)
0
 

Author Closing Comment

by:Christopher Schene
ID: 37836217
Perfect! Works great! Thanks for the fast response.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 37836389
You can simplfy the whole script to

#!/bin/bash
ls -t $1_$(hostname) | sed 'N;$!P;$!D;$d' | xargs rm

Open in new window

0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

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…
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
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 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…

752 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