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 cleanup.sh

# chmod +x cleanup.sh

Then edit new script file and fill:

#!/bin/bash
DIR="/dir"
DAYSOLD="7"
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

# ./cleanup.sh
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

Summary
Linux Shell Script To Delete Files Older Than 7 Days
Article Name
Linux Shell Script To Delete Files Older Than 7 Days
Description
Example Linux Shell Script To Delete Files Older Than X Days
Author
Publisher Name
ARSTECH
Publisher Logo

arstech