Retain Only specific number of file in Directory

I want to retain specific number of backup files in a directory.for example i want to retain only two latest backup file in backup directory. If number of backup files is greater than this policy that it will delete oldest file.Please Tell me whether this is possible or not.

Possible. Sort the directory by date-time ("ls -1t") and remove the first two lines ("tail -n +3") and then remove every file in the remaining list.

If you're feeling lucky:

ls -1t | tail -n +3 | xargs rm -f 

Just make sure there are no non-backup files in the folder.

Great.it works....Thanks it solve my problem....:slight_smile: