Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'd like to do the following:

  1. Get the number of files in a given directory that match a given pattern, for example:

    ExtractBackup_{date}.tar.gz

  2. If that number is 2 or higher, delete the oldest file which matches that pattern.

How do I go about doing this using a Korn Shell (.ksh) script?

share|improve this question

migrated from stackoverflow.com Jun 28 '11 at 22:09

This question came from our site for professional and enthusiast programmers.

3 Answers 3

up vote 2 down vote accepted

There's no direct way to count files matching a pattern, but you can do it in two easy steps: generate the list of files, and take the length of the list. Assuming the date is in YYYYMMDD form (note that this clobbers the positional parameters):

set ExtractBackup_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].tar.gz
if [ -e "$1" ]; then count=$#; else count=0; fi

In ksh93, you can make the count easier by making the list be empty if no file matches. Portably, a pattern that doesn't match any file is replaced by a list containing one word which is the pattern itself; ksh93 has a construct to have the pattern expand to an empty list instead. Ksh has arrays, which means you don't need to clobber the positional parameters.

backups=(~(N:ExtractBackup_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].tar.gz))
count=${#backups[@]}

If the date is in YYYYMMDD form, then the oldest file is the first in the list.

set ExtractBackup_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].tar.gz
if [ $# -ge 2 ]; then rm "$1"; fi
share|improve this answer
    
This is what I was looking for. Thanks! My date was in YYYYMMDDhhmmss format so I had to tweak it but only slightly. –  Ben Jun 29 '11 at 14:42

Ok, run this script with the pattern as the argument and if more than one files matches it will delete the oldest, for example:

$ ksh dtof.sh ?? # this will delete the oldest two-character file

The script:

for i in "$@"; do
  if [ ! "$oldest" ]; then
    oldest="$i"
    continue
  fi
  if [ "$i" -ot "$oldest" ]; then
    oldest="$i"
    reap="$i"
  else
    reap="$oldest"
  fi
done
[ "$reap" ] && rm "$reap"
share|improve this answer

Don't know about ksh so I'll give you some tools that you can put together.

Find all files in current folder that match a certain pattern, and return the count

find . -maxdepth 1 -type f -name '*.tgz' | wc -l

To select the oldest file, select the first entry from the output below

ls -t1p | grep -v /

Good luck :-)

share|improve this answer
    
Parsing the output of ls is generally not recommended, though it works here since the file names are tame. find isn't the right tool as recursion into directories isn't wanted. –  Gilles Jun 28 '11 at 22:36
    
How about rounding out this answer by showing how to read the first command into a variable, then making sure the variable is >= 2 before running a delete on all but the latest? –  Caleb Jun 28 '11 at 22:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.