Shell script to housekeep lower version files lpt$vpn.xxx

I have the following files (& new files added daily in cron job) in a folder /opt/avscan
-rwxr-xr-x   1 root     root         60M Jul 12 20:50 /opt/uvscan/lpt$vpn.787
-rwxr-xr-x   1 root     root         60M Jul 10 20:50 /opt/uvscan/lpt$vpn.783
-rwxr-xr-x   1 root     root         60M Jul 13 23:57 /opt/uvscan/lpt$vpn.789
-rwxr-xr-x   1 root     root         60M Jul  9 21:49 /opt/uvscan/lpt$vpn.781
-rwxr-xr-x   1 root     root         60M Jul 14 21:52 /opt/uvscan/lpt$vpn.791
-rwxr-xr-x   1 root     root         60M Jun 30 06:17 /opt/uvscan/lpt$vpn.761
-rwxr-xr-x   1 root     root         60M Jul 11 20:49 /opt/uvscan/lpt$vpn.785

I would like to add into the very same Bash Shell script (I suppose I don't
have to share the existing script) that pulls down lpt$vpn.xxxx (can grow
to  lpt$vpn.1001 & higher) that will delete /housekeep away lower version
files.  In this case, we'll leave behind only lpt*.791

I suppose we can use the ctime or atime as a way of deciding which file(s)
to delete:
find /opt/avscan -ctime ... | sort based on date/timing
& then "rm `sorted lpt* list of files | grep -v `tail -1` "   or something like that


Most appreciate if anyone can provide the precise codes & a tested script
sunhuxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

woolmilkporcCommented:
We don't need "find", "ls" can sort by modification time, too. "-t" displays the newest file first.
"tail +2" removes the newest file from the list ("+n" means "distance from the beginning")

Please note the single quotation marks. They're required to protect '$' from being interpreted by the shell.

echo rm $(ls -t '/opt/uvscan/lpt$vpn.'* | tail +2)

I prefixed the command with "echo" so you can see what it would do.
Remove this echo to actually run the command.

It might be that there are more files than "rm" can handle in one go. In this case use "xargs":

ls -t '/opt/uvscan/lpt$vpn.'* | tail +2 | xargs echo rm

"xargs" splits the input into chunks suitable for the command line.
See above for the use of "echo".
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sunhuxAuthor Commented:
Thanks very much, I'll test it out in 30-32 hours' time :
if there's no further clarifications will close this thread
0
sunhuxAuthor Commented:
One query: the script given should work in Bash, Korn or Bourne shells, right?
0
woolmilkporcCommented:
Bash and ksh - no problem.

With "Bourne Shell", do you mean the (very) traditional old bsh?

If so, you will have to run my first suggestion there this way:

echo rm `ls -t '/opt/uvscan/lpt$vpn.'* | tail +2`

My second version should work as posted.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.