Solved

Need borne shell to delete all but last two backups

Posted on 2012-04-11
3
395 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:cschene
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:cschene
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
oracle query help 36 67
Problem logging tar errors 11 35
mobaxterm not able to change directory 28 48
Xymon customize http timeout 2 27
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.…
If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
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.:
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…

758 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

20 Experts available now in Live!

Get 1:1 Help Now