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

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

By sk
Published: Last Updated on 22.6K views

I have 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 in a directory in Linux

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 named ostechnix as an example. Let us check how many files are in this directory using command:

$ find ostechnix/ -type f | wc -l

Or cd into that directory and run:

$ ls | wc -l

Sample output:

33
Count files in a directory in Linux using ls command
Count files in a directory

As you see in the above example, the directory ostechnix contains 33 files. I don't want 33 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

Or,

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

Where,

  • 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

Update:

If the filenames contains spaces, the above command will not work. Because, the xargs command takes white space characters (tabs, spaces, new lines) as delimiters. In that case, you can narrow it down only for the new line characters ('\n') with -d option like below:

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

Hope this helps.

You May Also Like

8 comments

JP September 2, 2019 - 5:47 am

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

Reply
sk September 16, 2019 - 4:10 pm

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.

Reply
Kevin Baker December 8, 2019 - 1:29 pm

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

Reply
Heron March 1, 2021 - 11:40 pm

everyday I have a backup of some files. I need to delete files that are older then 30 days just if there are more then 20 files on that directory.
this will prevent deletion in case the backup has an issue and stop working.
I guess it will be require a condition IF right?

Reply
hannes September 16, 2021 - 2:42 pm

Instead of `tail -n +11` I think you could also use `head -10`

Reply
sid December 16, 2021 - 1:30 am

How to run this command from different directory but no the pwd

Reply
sk December 16, 2021 - 3:44 pm

Could you please elaborate the question?

Reply
Marek February 9, 2022 - 10:25 pm

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

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

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

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More