HomeLinuxHow to Delete Files Older Than 30 Days in Linux?

How to Delete Files Older Than 30 Days in Linux?

As time passes, your Linux system collects various files like logs, backups, and temporary data, which can take up important disk space. Removing these outdated files is essential for keeping your system running smoothly and avoiding storage problems. In this guide, we will discuss how to delete files older than a certain number of days or older than 30 days, ways to automate this process, and some best practices to follow using the find command, bash script, cron job, and tmpwatch command.

Before diving into commands, here’s a simple flowchart to help you decide the best method for your needs:

Do you want to delete files manually? then use find command. 
Do you want to automatically delete and want a one-time cleanup? then use find command with cron job. If you need a recurring cleanup for automatical deletion, then use tmpwatch or a bash script with cron job.. here tmpwatch is useful for temp files where as bash script with cron job can be widely used for any type of files!

Manual File Deletion

Delete Old Files Using the Find Command

The most efficient way to remove old files manually in Linux is with the find command.
Here’s a simple example:

find /path/to/directory -type f -mtime +30 -exec rm -f {} \;
OptionsDescription
/path/to/directoryThe directory where the search begins. Replace this with the actual path.
-type fSearches only for files (ignores directories).
-mtime +30Finds files modified more than 30 days ago.
-exec rm -f {} \;Deletes each file found (rm -f forces deletion).

This command removes files that are older than 30 days via the Linux command line after the command is executed by the user.

Examples:
  • Delete files older than 7 days: find /var/log -type f -mtime +7 -exec rm -f {} \;
  • Delete files older than 60 days: find /backup -type f -mtime +60 -exec rm -f {} \;
  • Delete files older than 90 days: find /tmp -type f -mtime +90 -exec rm -f {} \;
using find command, we are deleting files older than 30 days in linux from the location /var/log

Automating File Deletion

Automate the cleanup process using the find command within cron jobs or a bash script, rather than executing commands manually. For temporary files, consider using the tmpwatch utility.

Deletion of Files using Cron Job and Find Command

To delete files older than 30 days automatically with the cron job, first execute the below command to open the cron job editor:

crontab -e

Add the below line to delete old files from the location /var/log every day at 3 AM:

0 3 * * * find /var/log -type f -mtime +30 -exec rm -f {} \;

You can modify the command to fit your needs. For example, instead of deleting files from /var/log, you might want to delete files from /home/cctv_recordings. If you’re still unsure about how to set up a cron job, you can find a detailed guide on how to do that here: https://www.veeble.com/kb/cron-job-how-to-schedule-recurring-user-jobs/

Examples:

I will provide the exact entry to add to the file after running the command crontab -e for the following situations:

  1. Delete files older than 7 days at 2:15 AM:
15 2 * * * find /var/log -type f -mtime +7 -exec rm -f {} \;
  1. Delete files older than 60 days at 10:45 PM:
45 22 * * * find /var/log -type f -mtime +60 -exec rm -f {} \;

(👆10:45 PM in 24-hour format is represented as 45 22 in the crontab entry.)

  1. Delete files older than 90 days at 5:00 AM:
0 5 * * * find /var/log -type f -mtime +90 -exec rm -f {} \;

Continuous Automatic Cleanup of Old Files

If you prefer a continuous cleanup process instead of a scheduled one using a cron job, consider the following two approaches based on whether you’re dealing with temporary or regular files.

Automatic, Continuous Cleanup of Temporary Files. (tmpwatch command)

tmpwatch is a specialized tool that we use for automatic, continuous cleanup for removing old temporary files. This may or may not be installed on your system. To install it, run the command below according to the package manager your OS uses:

ie; for Ubuntu/Debian:

sudo apt install tmpreaper

and for RedHat/CentOS:

sudo yum install tmpwatch

After installing the tool, using it is simple. Simply type “tmpwatch” or “tmpreaper” then specify the number of hours in numerical format, followed by the target directory.

Examples:
  1. Deleting files not accessed in the last 24 hours from /tmp location:
tmpwatch 24 /tmp
  1. Deleting files not accessed in the last 48 hours from /var/log location:
tmpwatch 48 /var/log

(👆If you have installed tmpreaper instead of tmpwatch, use tmpreaper in the command where you see the word “tmpwatch.” For example: tmpreaper 24 /tmp.)

Automatic, Continuous Cleanup of Old Regular Files (Bash Script with Find)

For non-temporary files, using a bash script with the find command is a more effective method.
You can automate the file deletion commands from the previous section using a bash script. Follow these steps to create and run the script:

  1. Open a terminal and create a new script file using the following command:
vi cleanup.sh

This will open a text editor.

  1. Add the cleanup command with the find command in a bash script. Copy and paste the code below, replacing the directory path and adjusting the days from 7 to your desired number in the cleanup.sh file:
#!/bin/bash
find /path/to/directory f -mtime +7 -exec rm -f {} \;
echo "Cleanup completed on $(date)" >> /var/log/cleanup.log
  1. Then Save and exit by pressing CTRL+X, then Y, and hit Enter.

A line-by-line explanation of the code in the script:

LineExplanation
#!/bin/bashSpecifies that this is a Bash script.
find /path/to/directory -type f -mtime +7 -exec rm -f {} \;Finds and deletes files older than 7 days.
echo “Cleanup completed on $(date)” >> /var/log/cleanup.logLogs the cleanup time for reference.
  1. Make the script executable by executing the chmod command with executable permission for cleanup.sh file:
chmod +x cleanup.sh
  1. Whenever you want to delete old files, run the script by executing the command manually with the command:
./cleanup.sh
  1. If you want this script to run automatically (for example: every day at midnight), add it to a cron job by opening the crontab with the command:
crontab -e

Then add the following line to it:

0 0 * * * /path/to/cleanup.sh

This will execute the script daily at midnight.

Automatically deleting files older than 30 days in Linux using bashscript and cron tab with the find command.

You can delete files that are older than a specified number of days by following this tutorial. This blog explains different methods for deleting files, whether they are older than 7, 30, or 90 days. It covers both manual and automated deletion techniques to help you manage storage and improve system performance in Linux.

Scroll to Top