How to Delete Junk Files in Linux Elegantly

How to Delete Junk Files in Linux Elegantly

I wonder if you are like me, a programmer who arranges your computer files in an orderly manner and deletes useless files in time? If so, then we can happily discuss the content of the article. If not, you can stay and join in the fun (>-<).

The following is today's protagonist - tmpwatch, which can help us recursively delete files and empty directories that have not been accessed within a given time.

Of course, we can also use the find command to find and delete files that have not been accessed for more than x days, but tmpwatch can do it in one step, so why not?

By default, tmpwatch decides which files or directories to delete based on their access time. In addition, you can also operate according to inode change time and modification time.

Typically, tmpwatch is used to delete files in the /tmp directory, as well as other unused files in other places, such as old log files.

IMPORTANT WARNING! !

Do not run tmpwatch in / (root)!
Do not run tmpwatch in / (root)! !
Do not run tmpwatch in / (root)! ! ! (Three times warning! ^-^)

The / directory contains important files necessary for the operation of the Linux system, and tmpwatch does not have a built-in protection mechanism to prevent it from running on the / directory. Once those important files are deleted, the consequences will be disastrous! Therefore, friends must be cautious when using this command!

Install tmpwatch

Installation of tmpwatch is available in the default repositories of most Linux distributions:

On Fedora:

$ sudo dnf install tmpwatch

On CentOS:

$ sudo yum install tmpwatch

On openSUSE:

$ sudo zypper install tmpwatch

On Debian and its derivatives (such as Ubuntu), tmpwatch is also called tmpreaper:

$ sudo apt install tmpreaper

Use tmpwatch/tmpreaper to delete files that have not been accessed within a specified time

The usage of tmpwatch and tmpreaper is almost the same, and they can be considered the same command. For ease of description, this article uses tmpwatch as an example. Friends who use Debian-based systems can change the following tmpwatch to tmpreaper.

1. Delete files that have not been accessed for more than X days <br /> Example: Delete all files and empty directories in the /var/log/ folder that have not been accessed for more than 10 days

tmpwatch 10d /var/log/

2. Delete files that have not been modified for more than X days <br /> As mentioned above, tmpwatch deletes files based on access time by default. Now we use the -m option to delete files based on modification time.

Example: Delete files in the /var/log/ folder that have not been modified for more than 10 days

tmpwatch -m 10d /var/log/

The d in the above two commands is the time parameter, as follows:

  • d - number of days
  • h - hours
  • m - minutes
  • s - seconds

The default time parameter is hours. If you want to delete files that have not been modified in the past 10 hours, you can write it in the following form:

tmpwatch -m 10 /var/log/

3. Delete the symbolic link

Symbolic links can be removed using the -s option:

tmpwatch -s 10 /var/log/

4. Delete all files (including regular files, symbolic links and directories)

tmpwatch can not only delete ordinary files, but also some special files, such as symbolic links, directories, pipe files, etc. In this case, you need to use the -a option:

tmpwatch -a 10 /var/log/

5. Exclude directories when deleting <br /> If you do not want to delete a directory, you can use the --nodirs option to exclude the directory from being deleted:

tmpwatch -am 10 --nodirs /var/log/

6. Test deletion (without actually deleting anything)
Here we must emphasize again that when deleting files in important directories, do not rush to use the tmpwatch command! You may want to first check which files are deleted after the command is run, otherwise you will have a headache if you delete the wrong files. . (Develop a good habit!)

You can use -t to enter test mode:

tmpwatch -t 30 /var/log/

Output under CentOS 7:

removing file /var/log/wtmp
removing directory /var/log/ppp if empty
removing directory /var/log/tuned if empty
removing directory /var/log/anaconda if empty
removing file /var/log/dmesg.old
removing file /var/log/boot.log
removing file /var/log/dnf.librepo.log

Output on Debian based systems:

$ tmpreaper -t 30 /var/log/
(PID 1803) Pretending to clean up directory `/var/log/'.
(PID 1804) Pretending to clean up directory `apache2'.
Pretending to remove file `apache2/error.log'.
Pretending to remove file `apache2/access.log'.
Pretending to remove file `apache2/other_vhosts_access.log'.
(PID 1804) Back from recursing down `apache2'.
(PID 1804) Pretending to clean up directory `dbconfig-common'.
Pretending to remove file `dbconfig-common/dbc.log'.
(PID 1804) Back from recursing down `dbconfig-common'.
(PID 1804) Pretending to clean up directory `dist-upgrade'.
(PID 1804) Back from recursing down `dist-upgrade'.
(PID 1804) Pretending to clean up directory `lxd'.
(PID 1804) Back from recursing down `lxd'.
Pretending to remove file `/var/log//cloud-init.log'.
(PID 1804) Pretending to clean up directory `landscape'.
Pretending to remove file `landscape/sysinfo.log'.
(PID 1804) Back from recursing down `landscape'.
[...]

The above process does not actually delete the files, but only simulates the deletion to tell you which files will be deleted.

When you are sure that the files to be deleted are correct, you can remove the -t option and execute tmpwatch to actually delete them.

7. Forced deletion
By default, tmpwatch will not delete files that the current user does not have write access to. But if you must delete those files, you can use the -f option to force deletion:

tmpwatch -f 10h /var/log/

8. Skip certain files when deleting <br /> If you want to keep specific files when deleting, that is, add them to the whitelist, you can use the --protect option. Suppose we want to keep all files of type txt:

tmpreaper --protect '*.txt' -t 10h /var/log/

Output:

(PID 2623) Pretending to clean up directory `/var/log/'.
(PID 2624) Pretending to clean up directory `apache2'.
Pretending to remove file `apache2/error.log'.
Pretending to remove file `apache2/access.log'.
Pretending to remove file `apache2/other_vhosts_access.log'.
(PID 2624) Back from recursing down `apache2'.
(PID 2624) Pretending to clean up directory `dbconfig-common'.
Pretending to remove file `dbconfig-common/dbc.log'.
(PID 2624) Back from recursing down `dbconfig-common'.
(PID 2624) Pretending to clean up directory `dist-upgrade'.
(PID 2624) Back from recursing down `dist-upgrade'.
Pretending to remove empty directory `dist-upgrade'.
Entry matching `--protect' pattern skipped. `ostechnix.txt'
(PID 2624) Pretending to clean up directory `lxd'.

Set up a cron job to automatically delete files regularly

(Secretly, tmpwatch/tmpreaper works best with cron jobs.)

Enter the cron job task editing window:

# crontab -e

Add a periodic task:

0 1 * * * /usr/sbin/tmpwatch 30d /var/log/

The above code sets tmpwatch to run at 1:00 AM every day and delete files older than 30 days.

Friends who don’t know about corn job can search for its beginner’s guide on the Internet.

When you install tmpreaper, it automatically creates a daily cron job (/etc/cron.daily/Tmpreaper). It reads the configuration from the /etc/timereaper.conf file and executes it. The default setting is to delete files older than 7 days. You can change this setting by modifying the TMPREAPER.conf file with "TMPREAPER_TIME=7d".

Final Thoughts
Finally, I would like to remind you that when deleting files, you must carefully check the path to avoid data loss.

tmpwatch and tmpreaper man pages:

$ man tmpwatch
$ man tmpreaper

This is the end of this article on how to elegantly delete junk files in Linux. For more relevant Linux junk files content, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Autotrash tool for Linux to automatically delete old junk files at a scheduled time

<<:  Vue implements time countdown function

>>:  Example code comparing different syntax formats of vue3

Recommend

Basic usage details of Vue componentization

Table of contents 1. What is componentization? 2....

Pitfall notes of vuex and pinia in vue3

Table of contents introduce Installation and Usag...

Div nested html without iframe

Recently, when doing homework, I needed to nest a ...

What codes should I master when learning web page design?

This article introduces in detail some of the tech...

Detailed explanation of the usage of the alias command under Linux

1. Use of alias The alias command is used to set ...

Examples of correct judgment methods for data types in JS

Table of contents Preface Can typeof correctly de...

JS realizes the case of eliminating stars

This article example shares the specific code of ...

Example of implementing QR code scanning effects with CSS3

Online Preview https://jsrun.pro/AafKp/ First loo...

Example code for using text-align and margin: 0 auto to center in CSS

Use text-align, margin: 0 auto to center in CSS W...

JavaScript to achieve digital clock effects

This article example shares the specific code for...