I need some help with the logic and syntax for a shell script (ksh) that will search a directory and look for similar files and save only the 5 versions. The version number is in the file name.
However, the files are of varying name lengths and may have 1 or many files, with no limit to the number of files.
the input file
From you question, I presume that the file name uses the date in the format ddmmyyyy, and that makes it a little tricky. Better to use yyyymmdd if you can, so the most significant digits are first. If you are stuck with that, can you be sure that the files (once complete) are not edited?
If so, you could:-
ls -1rt *.txt|tail -5
If the file timestamps cannot be guaranteed, you could change the date format in the file name, then it is a much easier sort:-
ls -1 *.txt | sort -n +0.2
This format of sort may have been superseded by now, so check your manual pages for sort to be sure.
I hope that this helps,
Robin
Liverpool/Blackburn
---------- Post updated at 02:34 PM ---------- Previous update was at 02:32 PM ----------