Linux: Delete Files Older Than X Days


This is a very simple tutorial on how to find, move and delete files older than X days. I needed this for a project where I collected some images and needed to archive and delete them after a while.

With this, you will be able with the Linux find command to find your JPG files older than 30 days and then execute rm or mv or whatever command you want on them.


Find files older then

find /path/to/files/ -type f -name '*.jpg' -mtime +7

Explanation

  • First part is the path where your files are located. Don’t use wildcard * if you have a lot of files because you will get Argument list too long error.
  • Second part -type is the file type f stands for files
  • Third part -name is limiting *,jpg files
  • Fourth part -mtime gets how many days the files older then will be listed. +7 is for files older than 7 days.

This is the simple find file command that will list all the .jpg files older than 7 days.


Delete files older then

find /path/to/files/ -type f -name '*.jpg' -mtime +15 -exec rm {} \;

Explanation

  • First part is the path where your files are located. Don’t use wildcard * if you have a lot of files because you will get Argument list too long error.
  • Second part -type is the file type f stands for files
  • Third part -name is limiting *,jpg files
  • Fourth part -mtime gets how many days the files older then will be listed. +15 is for files older than 15 days.
  • Fifth part -exec executes a command. In this case rm is the command, {} gets the filelist and \; closes the command

This will delete all the .jpg files older than 15 days.


Move files older then

find /path/to/files/ -type f -name '*.jpg' -mtime +30 -exec mv {} /path/to/archive/ \;

Explanation

  • First part is the path where your files are located. Don’t use wildcard * if you have a lot of files because you will get Argument list too long error.
  • Second part -type is the file type f stands for files
  • Third part -name is limiting *,jpg files
  • Fourth part -mtime gets how many days the files older then will be listed. +30 is for files older than 30 days.
  • Fifth part -exec executes a command. In this case mv is the command, {} gets the filelist, path where to move the files and \; closes the command

This will move all the .jpg files older than 30 days into a new directory.


bash delete file script

Now we can combine these two commands to archive the images older than 15 days and delete them from the archive folder if they are older then 30 days.

We are going to create a shell script that will do that and we can run it with a crontab.

#!/bin/bash
/usr/bin/find /path/to/files/ -type f -name '*.jpg' -mtime +15 -exec mv {} /path/to/archive/ \;
/usr/bin/find /path/to/archive/ -type f -name '*.jpg' -mtime +30 -exec rm {} \;

This command should work on most of the Linux distributions.

More on findhttp://www.linuxmanpages.com/man1/find.1.php



Subscribe
Notify of
guest
13 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Asanokatana
Asanokatana
6 years ago

I know this page was written a few years ago, but this was incredibly helpful and easy to follow. Thank you!

Coverup
Coverup
6 years ago

I agree, very usefull post, regards

Coverup
Coverup
6 years ago

btw everything goes wrong if you type /folder instead /folder/, instead move, a file called folder is created and is just garbage, so dont forget the / at the end

Peter Stefanovic
Peter Stefanovic
5 years ago

Decent page there Nikola.

Cant stand your name though.

Donatello Tuttle
Donatello Tuttle
5 years ago

Guys, I just shit myself reading this… what do I do??

LuisG
LuisG
4 years ago

Here is another option:

find / SYSADMIT / * -type f -not -newermt “YYYY: MM: DD HH: MI: SS” -delete

Extracted from: https://www.sysadmit.com/2019/08/linux-borrar-ficheros-por-fecha.html

Vivek P
Vivek P
4 years ago

Excellent article with very detailed explanation.
Please advise how to further list the files older than x days with ls -lrt command and also to display file count using ls -lrt | wc -l.

Vamsi Krishna
Vamsi Krishna
4 years ago

How to move 15 days older files from a specific date instead of system date (-mtime +15) in UNIX

nstojanoski
nstojanoski
4 years ago
Reply to  Vamsi Krishna

I haven’t tried that, but as a workaround you can calculate the days, let’s say you want 15 days before 01.01.2019 so it’s 91 days from 01.01.2019 to today + 15 days the -mtime will be 106

here is a way how to calculate it in a bash script https://www.cyberciti.biz/faq/shell-script-to-get-the-time-difference/

Jana
5 years ago

Great help, thanks bruh !

houmanka
houmanka
5 years ago

Life saver :)

Newbie
Newbie
4 years ago

Thanks for the post,it was very helpful.
If the retention period has to be derived from a seperate configuration file then how will i derive it into this piece of code.
Thanks for the help in advance.

Peter
Peter
4 years ago

I never understand why all tutorials about “delete files older than” go down the “-exec rm {} \;”-path when you can just use the -delete option proper to find?
find /directory/ -type f -name “*.jpg” -mtime +15 -delete
Simple as cheese!

Advertisement