File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Linux / UNIX and the fly likes Incremental filename script Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "Incremental filename script" Watch "Incremental filename script" New topic

Incremental filename script

Kev D'Arcy
Ranch Hand

Joined: Jul 26, 2001
Posts: 75
Hi all,

I currently have a backup cron job running every hour to create a backup
of my code repository. The only issue at the moment is that the backup file
gets over-written each time the job runs.

Is there a way to script the appending of a number onto the file name so
that I can keep say the most recent 50 copies of the file? eg:

And when the number hits (say) 50 it'll return to 1?

Many thanks,

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17417

I assume that either you're not using a version control system or else that you're really paranoid about your VCS server breaking down. If you're not using some sort of VCS, such as CVS, Subversion or even Visual SourceSafe, consider doing so. I backup my VCS's at 4:00 am every morning as a cron job, but don't keep versions of them - VCS systems are inherently cumulative. About once a week, the backup copies get burned out to DVD as long-term archives. I keep a couple of generations just in case the master VCS gets trashed, the online backup gets trashed and the latest DVD turns out to be unreadable.

I'm pretty paranoid myself.

An IDE is no substitute for an Intelligent Developer.
Kev D'Arcy
Ranch Hand

Joined: Jul 26, 2001
Posts: 75

I'm pretty paranoid too! We (will) have a nightly backup job to back up the repository, but I want my own backup jobs to run as well (very paranoid). It's more to do with being able to restore the repository quickly to another server (i.e. myself) rather than have to wait for a full restore by our server admin. I think I've managed to sort it out thought.

Brian Wright

Joined: Dec 12, 2005
Posts: 11
Here's a very basic way to do what you're looking for, I think. Note that there are a horde of ways to do this, I was just looking for something fairly easy to follow:

LATEST=$(ls -1t backup*.zip | head -1 | cut -d. -f1 | sed "s+backup++g")

if [[ ${LATEST} -eq 50 ]]

NEW=$(( ${LATEST} + 1 ))


Hope this gives you something to play with!
I agree. Here's the link:
subject: Incremental filename script
It's not a secret anymore!