Find And Delete Oldest File If There Are More Than X Files In A Directory

3 Responses

  1. JP says:

    My files use spaces. How do I make it so “xargs rm -f” doesn’t treat each space as a separate filename?

  2. sk says:

    Hey, sorry for the late reply. If your files contains spaces, use -d option like below.

    ls -1t | tail -n +11 | xargs -d ‘\n’ rm -f

    I have updated the guide now. Thanks for bringing it to my knowledge.

  3. Kevin Baker says:

    Hi! This is almost EXACTLY what I’ve been looking for, however, I need to delete all files but the last x from each sub-folder. I have a folder (BackupData) with many sub-folders (each folder is a different customers databackups), so I need the script to do a for-each sub-folder in BackupData, delete all files except most recent x

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.