Posted by Arjun Arjun on Tuesday 27th February 2018, 1 minute read

How to delete files older than X days in Linux

How to

In this post, I am gonna show you how to write a simple shell script to delete files from a directory if the files are older then X days. We gonna use find utility function to search and find the files from the given specific directory.

Command to delete files older then 10 Days

$ find /var/www/app/csv -type f -name *.csv -mtime +10 -exec rm {} \;

Command parts:

  • In the first part, we are using find command to search in the specified directory.
  • In the second part -type is the file type f stands for files.
  • In the third part -name is searching for files which ends with .csv extension.
  • In the fourth part -mtime gets how many days the files older than will be listed. +10 is for files older than 10 days.
  • In the fifth part -exec executes a command. In this case, rm is the command, {} gets the file list and \; closes the command.

I am Arjun from Hyderabad (India). I have been working as a software engineer from the last 7+ years, and it is my passion to learn new things and implement them as a practice. Aside from work, I like gardening and spending time with pets.