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

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

I have so many movies in my hard drive and I have stored them in different folders based on the movie genre. Now, I want to keep only particular number of movie files in a directory, and delete everything else. More importantly, I want to delete only the oldest files. This way I can maintain a constant number of files in each folder. Since I have so many files scattered across many folders, it is quite time consuming process to go to each folder, search for the oldest files and manually delete them one by one. While looking for an easy way to do this, I found the following solution. Read on. It’s not that difficult.

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

Let us say, you wanted to find and delete the oldest file if there are more than 10 files in a directory. How would you do? It’s very simple.

Take the following directory as an example. Let us check how many files are in this directory.

$ find ostechnix/ -type f | wc -l

Or cd into that directory and run:

$ ls | wc -l

Sample output:


As you see in the above example, the directory ostechnix contains 13 files. I don’t want 13 files in this directory. I want to delete all oldest files and keep only 10 files.

Now, let us find and delete oldest file(s) in this directory if it contains more than 10 files. To do so, go to that directory:

$ cd ostechnix

And, run the following command:

$ ls -1t | tail -n +11 | xargs rm


$ ls -1t | tail -n +11 | xargs rm -f


  • ls : List directory contents.
  • -1t : 1(Number one) indicates that the output of ls should be one file per line. t indicates sort contents by modification time, newest first.
  • tail : Output the last part of files.
  • -n +11 : output the last NUM lines, instead of the last 10; or use -n +NUM to output starting with line NUM
  • xargs : Build and execute command lines from standard input.
  • rm -f : Remove files or directories. f indicates ignore nonexistent files and arguments, never prompt. It means that this command won’t display any error messages if there are less than 10 files.
  • | – It is a pipeline. It is generally a sequence of one or more commands separated by one of the control operators or |&.

So, the above command will delete the oldest files if there are more than 10 files in the current working directory. To verify how many files are in the directory after deleting the oldest file(s), just run:

$ ls | wc -l

Also Read: How To Find And Delete Files Older Than X Days In Linux

And, that’s all. If you find our guides helpful, please share them on your social, professional networks and support OSTechNix. We will be posting more useful and interesting stuffs everyday. Keep visiting!


Thanks for stopping by!

Help us to help you:

Have a Good day!!

You may also like...

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.