Deleting Old Files With BASH / Removing Old Log Files with BASH (Linux)

Bash script to delete files older than 31 days old

Log files are a brilliant feature of any piece of software / operating system. However, over time they can take up space and become a pain to remove. In this blog post I am going to look at a quick bash script that can be used via cron to delete older log files.

The Set Up

In this scenario I have a folder (/home/pi/test/) where one of my programs writes regular log files to. When the log file reaches a certain size (e.g. 100MB) it generates a new log file (sequentially numbered). The scenario could also be that the log file is generated daily.

geektechstuff_99_log_files
99 “log files” in a directory

For this scenario I created the .log files using the Linux touch command:

touch {1..99}.log
geektechstuff_touch_to_create_99_files
touch {1..99}.log creates 99 files called 1.log, 2.log etc…

As I want to delete files that are old (i.e. not created today) I had a few options.

  • Wait several days to test my bash script.
  • Change the systems date/time to the future.
  • Use touch to modify the files.

I took the option of using the touch command to change the files named 1.log through 55.log.

touch -a -m -t 202004011220.01 {1..55}.log

This command tells touch to:

-a is the access time

-m is the modification time

-t tells touch that I want to provide a specific date/time which is in the format:

YYYYMMDDHHMM.SS

Note: YYYY in this case is DECADE-INDIVIDUAL_YEAR-CENTURY-CENTURY, so 1982 would be 8219.

With that in mind 202004011220.01 becomes 1st April 2020 at 12:20 (and 1 second).

touch -a -m -t 202004011220.01 {1..55}.log
touch -a -m -t 202004011220.01 {1..55}.log

Why Have A Set Up?

This bash script deletes files. In theory it could delete a lot of files, and if run with sudo or root it could delete system files. With this in mind I wanted to test the script, run it multiple times and tweak it, so I needed a test set up.

Warning: I recommend testing (and retesting) scripts like this before using them on a live system. Messing up a wildcard, folder path or file name pattern can have major consequences.

The Bash Script

Bash script to delete files older than 31 days old
Bash script to delete files older than 31 days old

The bash script that finds and deletes the files is only a few lines long and I’ve added in a basic logging function so that I know when it ran and what it deleted.

#!/usr/bin/env bash

# geektechstuff

# script to remove old files

folder_path=/home/pi/test/

log_of_files=/home/pi/files_deleted.log

date >> $log_of_files

echo "---" >> $log_of_files

find $folder_path -mtime +31 -name "*.log" >> $log_of_files

find $folder_path -mtime +31 -name "*.log" -delete

echo "--- " >> $log_of_files

Breaking the bash script down:

#!/usr/bin/env bash

A she-bang line to tell the operating system what to use to run the script

folder_path=/home/pi/test/

The folder path where the log files are. I thought best to hard code this here so that it is not accidentally mistyped at run time.

log_of_files=/home/pi/files_deleted.log

The location of where I want my script to log what it has done.

date >> $log_of_files

echo "---" >> $log_of_files

Outputting the systems current date/time and then a line of hyphens into the log file.

find $folder_path -mtime +31 -name "*.log" >> $log_of_files

Outputs the files that the find command finds to the log file. These will be the files that bash script will delete.

-mtime is the files modification time. In this case I’m asking for anything greater than 31 days.

-name is the files name. In this case I am asking for any file names that end .log

find $folder_path -mtime +31 -name "*.log" -delete

This line is very similar to the above, but instead of outputting the list of files to the log file it deletes them (-delete).

echo "--- " >> $log_of_files

Adds another line of hyphens to the log file.

Modifying Run Permissions And Scheduling

Once created and tested, the file can be made executable and set to run at certain times on certain dates:

Making it executable
Scheduling Tasks With Cron

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.