Make script delete certain files

Hi,

In a directory, say

~/dir

is a lot of files and subdirectories. Some of the files are named

res1.om
res2.om
...
res65.om

The amount of files varies. I need a .csh script that deletes all res*.om files except the one with the highest number.

Any suggestions?

Thanks in advance :slight_smile:

csh is not suited for scripting:
Top Ten Reasons not to use the C shell
C shell problems
Csh Programming Considered Harmful

Assuming that the filenames are all like your example and do not contain spaces or other pathological characters:

rm $(
    printf "%s\n" res*.om |
    sort -t~ -k2n |
    awk 'NR > 1 { printf "%s ", last } { last = $0 }'
 )

doesn't work. Error says:

Illigal variable name.

The file names are as I wrote originally.

There are no variables used in that script.

Note that it is not a csh script; run it in a POSIX shell. On mnost systems, /bin/sh is a POSIX shell; on Solaris, use ksh or bash or (I think) /usr/pkg4/bin/sh.

Ok thank you!

ls -1 res*.om|sort -rn -k1.4|sed -n '2,$p'|xargs rm -f

wow that one really works! thanks a lot!