In the daily maintenance of Linux servers, handling temporary files is an important and often overlooked task. Over time, temporary directories such as /tmp can accumulate a large number of unused files, which not only occupy valuable disk space but can also affect the system’s performance and stability. To optimize your server performance, let’s explore how to efficiently manage temporary files in the Linux system using two powerful tools: tmpwatch or tmpreaper.

Why Clean Linux Temporary Files Regularly?

Temporary files are generated during routine operations to store transient data. However, these files are often forgotten after use, accumulating and occupying disk space. If not cleaned, Linux temporary files may lead to the following issues:

  • Disk space waste: A large number of uncleared temporary files can take up significant disk space, which is especially important for servers with limited resources.
  • Performance decline: The more files in the file system, the longer it takes to search for and access files.
  • Security risk: Some temporary files may contain sensitive information, and their long-term presence can increase security risks.

What are tmpwatch and tmpreaper?

tmpwatch and tmpreaper are two tools used for automatically deleting files and directories that are no longer needed. They are commonly used to clean up the /tmp directory and other directories that may accumulate temporary files.

tmpwatch

tmpwatch is the default tool on Fedora, CentOS, and other Red Hat-based systems. It decides which files should be deleted based on the file’s access time, inode change time, or modification time.

Installing tmpwatch

On Fedora systems, you can install tmpwatch using the following command:

sudo dnf install tmpwatch

On CentOS systems, you can use the following command:

sudo yum install tmpwatch

tmpreaper

tmpreaper is the tool used on Debian, Ubuntu, and their derivatives, with functionality similar to tmpwatch but with different commands and configuration methods.

Installing tmpreaper

On Debian-based systems, you can install tmpreaper with the following command:

sudo apt install tmpreaper

How to Use tmpwatch/tmpreaper?

  1. Delete files not accessed for a specific number of daysTo delete files in the /var/log/ directory that have not been accessed for over 10 days, you can use the following command:
    tmpwatch 10d /var/log/

    For Debian systems, use tmpreaper as follows:

    tmpreaper 10d /var/log/
  2. Delete files not modified for a specific number of daysTo delete files based on the modification time, you can add the -m option:
    tmpwatch -m 10d /var/log/
  3. Delete symbolic linksTo delete symbolic links, you can use the -s option:
    tmpwatch -s 10d /var/log/
  4. Exclude specific directoriesTo exclude specific directories during deletion, you can use the –nodirs option:
    tmpwatch -am 10d --nodirs /var/log/
  5. Test deletionBefore actually performing the deletion, it is recommended to first use the -t option to test and see which files will be deleted:
    tmpwatch -t 10d /var/log/