Delete Files Older Than 7 Days. Linux Shell Script Example

Linux Shell Script To Delete Files Older Than 7 Days

Below i will show  basic Linux shell script for Delete Files Older Than 7 Days in Linux. Of course you can change amount of days as you need.

Also it possible implement this script as cron job for running by schedule.

Ok, lets create new file and make it executable with command chmod:

# touch
# chmod +x

Then edit new script file and fill:

cd $DIR
find $DIR -mtime +$DAYSOLD -type f -exec rm -v {} \;

For testing this script before actual delete files (without execution rm) you can use: find $DIR -mtime +$DAYSOLD -type f

Where is:

DIR – directory where is your files need to delete

DAYSOLD – how many days old files should be deleted, so you can put any number of days

-v – verbose

# ./
As result script fill be fond and delete files older than X days.

If you like what you are reading, please:

Buy me a coffeeBuy me a coffee