Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to do the following:

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


  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.

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.


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
  if [ "$i" -ot "$oldest" ]; then
[ "$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


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.