Easy Technical Guides for Windows and Linux Admins
  • Home
  • Linux Basics
  • Squid Proxy
  • Linux HowTo's
  • Windows HowTo's
  • Free E-Books
  • Disclaimer
  • Contact
  • About
16 April 2016

How to Delete Files Older than X Number of Days in Linux

Written by H.Haider
Shell Scripts 4 Comments

Last updated on April 19th, 2018 at 07:48 am

INTRODUCTION

How to Delete Files Older Than X Number of Days in Linux

After reading our old article on Linux Backup, many readers asking how to keep limited amount of backup files or how to delete backup files older than x number of days so now I am writing this guide in response to our readers question,

Queries from the readers:

How to delete backups that are older

How do i keep only limited amount of these backup files

SOLUTION

Using find utility we can easily figure out files older than x number of days and then use the rm command to delete them.
Here is combination of find and rm command to find and delete older files using some interesting arguments.

1- Find All Files Inside Directory and Delete Files which is Older Than 10 Days

find /location/to/find/files/* -mtime +10 -exec rm {} \;

Command Explanation:

1- find /location/to/find/files/*

This command use to find files in Linux operating system. Next part of this command is /locatin/to/find/files/ which means find files inside this directory suppose I have directory on root named backup so my command will be find /backup/*.

2- mtime +10

The next argument -mtime, is used to specify the number of days. In our example we uses -mtime +10 so it will find files older than 10 days.

3- -exec rm {} \;

The last argument contains two parts, “–exec” allows you to pass an other command such as rm, and the second part “{} \;” is required to end the command.

2- Delete Files which is Older Than 5 Days With .tar File Extension.

find /backup -name "*.tar" -type f -mtime +5 -exec rm -fr {} \;

Very Important: Be VERY careful to supply an absolute path while using “rm” commands!.

3- Find and Delete Files using Find Command.

Same task you can perform using find command only. See below example:

find /location/to/find/files/* -mtime +10 -type f -delete

Tip:- you can add this command in bash script and automate this process using cron job utility.

Like This Article?

We have tons of great free content! Join 590 others and get access to coupons, freebies, and other great Tips & Tricks.

Invalid email address
We promise not to spam you. You can unsubscribe at any time.
Thanks for subscribing! Please check your email for further instructions.

Post Views: 767
Share On
Share on Facebook
Share on Twitter
Share on Google+
Share on LinkedIn
Share on Pinterest
Share on StumbleUpon
Share on Tumblr
Share on Whatsapp

Related Posts

  • Website & MySQL Backup Script

    Backup Script For Website and MySQL – Linux

    November 11, 2018
  • Bash Script FTP Upload

    How to Backup Files to a Remote FTP Server With Bash Script

    March 1, 2017
  • Bash Script Send Email on Failure or Success

    May 21, 2016

4 Comments

  1. Krishnam Reply to Krishnam
    July 20, 2016 at 4:12 pm

    I want to ZIP the file in the same directory, if its older than 7 days. This has to be performed in all folders of my unix box. Thanks in advance.

  2. GoldenLee Reply to GoldenLee
    July 20, 2016 at 8:43 pm

    Thank you

  3. SANGAY WANGMO Reply to SANGAY
    October 9, 2017 at 4:10 pm

    Am I right to write find comments in the script files such as filename.sh??

  4. Adrian Reply to Adrian
    April 8, 2018 at 12:10 am

    Hi, can i use this bash script? auto backup folder and delete tar files older than 7 days.

    #!/bin/bash
    #Purpose = Backup of Important Data
    #Created on 17-1-2012
    #Author = Hafiz Haider
    #Version 1.0
    #START
    TIME=`date +%b-%d-%y` # This Command will add date in Backup File Name.
    FILENAME=backup-$TIME.tar.gz # Here i define Backup file name format.
    SRCDIR=/imp-data # Location of Important Data Directory (Source of backup).
    DESDIR=/mybackupfolder # Destination of backup file.
    tar -cpzf $DESDIR/$FILENAME $SRCDIR
    find /mybackupfolder -name “*.tar” -type f -mtime +7 -exec rm -fr {} \;
    #END

Leave a Reply

Cancel reply

Search Tech Resources

Never Miss an Update !

Receive all our future posts instantly in your inbox. Enter your email to enroll.
Invalid email address
We promise not to spam you. You can unsubscribe at any time.
Thanks for subscribing! Please check your email for further instructions.

Categories

  • Android (16)
  • CentOS/RHEL (118)
  • Deals (1)
  • Game Errors (6)
  • iPhone Tips & Tricks (4)
  • Linux Basics (19)
  • Mobile Tips & Tricks (3)
  • Photoshop Tips & Tricks (1)
  • Printer Tips & Tricks (2)
  • Proxy (10)
  • Security (6)
  • Shell Scripts (6)
  • Ubuntu (3)
  • Windows (40)
  • Wordpress (4)
  • WordPress Basics (1)
  • YouTube Tips (5)

Popular Posts

  • Easy Guide – How to Save Facebook Video to iPhone/iPad H.Ali September 28, 2018
  • Mac restriction in squid proxy
    Deny Access Based on MAC Address in Squid Proxy H.Haider September 12, 2017

Quick Links


Home
About
Windows HowTo's
Linux Basics
Contact

Social Media

  • Connect on Facebook
  • Connect on Twitter
  • Connect on YouTube

Copyright 2009-2019