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

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 hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

Arjun
I am Arjun from Hyderabad (India). I have been working as a software engineer from last 7+ years, and its my passion to learn new things and implement them as a practice. Aside from work, I likes gardening and spending time with pets.
DMCA.com Protection Status