TECHIES WORLD

For Techs.... Techniques.... Technologies....

BashLinux

How to delete files older than specific days in Linux

We can use the find command for searching the files older than specific days and delete it.

#find FOLDER-PATH -type f -mtime +N -delete 

Where FOLDER-PATH need to be replaced with the full path to the folder and N with the number of days.

This will remove all files older than specified days from the mentioned folder.

If we need to delete files with specific extensions only, use name filter along with the find command.

#find FOLDER-PATH "*.EXT" -type f -mtime +N -delete 

Where FOLDER-PATH need to be replaced with the full path to the folder, N with the number of days and EXT with the required file extension.

Note that always do the dry run first with out the "-delete" option for the safer side.

That's all…