Skip to content

How to delete files older than X days in Linux

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.
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments