remove old backup files

# find /home/shantanu -name 'my_stops*' | xargs ls -lt | head -2

The command mentioned above will list the latest 2 files having my_stops in it's name. I want to keep these 2 files. But I want to delete all other files starting with "my_stops" from the current directory.

Try this:

 
find /home/shantanu -name 'my_stops*' | xargs ls -lt | tail +3 | awk '{print $9}' | xargs rm -f

Note: This is untested since it involves deletion. Please test it thoroughly before deleting!

HTH, :cool:

Regards,

Praveen

Does your command really list lastest 2 files?

Try the below command and see if the order of listing is based on time.
find /home/shantanu -type f | xargs ls -lt | pg

I agree with "pt14". The construct doesn't ignore the correct files.

Try this construct which should do if there are no non-files called "mystops*":

ls -1td mystops* 2>/dev/null | sed -n '3,$ p' | while read FILENAME
do
     echo rm "${FILENAME}"
done

Check carefully before removing the "echo".