Linux find command script
Posted on 2006-11-21
I have 200 users that place files in the /tmp directory. These users all have a username that starts with user1. So for example:
There are other users that also write files to this directory, so I only want to this script to run on files owned by users who's username starts with user1.
The files user1* users create all start with DBF. I need a script that will check to see if any DBF* files exist by the same user in the /tmp directory, and delete all but the most recent one. So if user123 has three files: DBF1A modified at 12:55, DBF1B modified at 12:57, and DBF1C modofied at 12:59, the script needs to delete all except the DBF1C file because it is newest.
What I have so far is this (thanks to an EE expert "dnb"):
find /tmp -name 'DBF*' -printf '%u:%f\n' | grep '^user1' | cut -d: -f2- | xargs rm
The problem is that this script will delete ALL the DBF files that it finds. How can I make it keep only the most recent version?