0

Possible Duplicate:
Delete all but the most recent X files in bash

The scenerio is as follows:

Two tar files will be created in one directory per day, but I need only the latest two files, so how to delete the other files automatically each day?

Is i possible to write this script using pure shell commands, and not with high level language such as perl, python or ruby...

This issue is a bit similar to FTP - Only want to keep latest 10 files - delete LRU and how to delete all files except the latest three in a folder

but mine also needs to test if a tar file is corrupt

If newer tar file is corrupt, I would not keep it, but reserve the older ones, so what the script should be like?

Community
  • 1
  • 1
hugemeow
  • 7,777
  • 13
  • 50
  • 63

1 Answers1

0

Obviously this is solvable using a two pass approach.

  1. Detect and remove corrupt tar files.
  2. Remove all but the latest N files as described in the referenced question.

The first pass could be performed with something along

for t in *.tar; do
    if program_that_checks_tar_file_for_integrity $t; then
        : # OK
    else
        rm $t
    fi
done
Jens
  • 69,818
  • 15
  • 125
  • 179