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.


Combine commands

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



13
Leave a Reply

avatar
12 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
12 Comment authors
PeterNewbiehoumankaJananstojanoski Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Asanokatana
Guest
Asanokatana

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

Coverup
Guest
Coverup

I agree, very usefull post, regards

Coverup
Guest
Coverup

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
Guest
Peter Stefanovic

Decent page there Nikola.

Cant stand your name though.

Donatello Tuttle
Guest
Donatello Tuttle

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

houmanka
Guest
houmanka

Life saver :)

Jana
Guest

Great help, thanks bruh !

Vamsi Krishna
Guest
Vamsi Krishna

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

nstojanoski
Guest
nstojanoski

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/

Vivek P
Guest
Vivek P

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.

LuisG
Guest
LuisG

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

Newbie
Guest
Newbie

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
Guest
Peter

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