How to delete junk files in Linux elegantly

  • 2021-07-24 12:09:34
  • OfStack

I don't know if everyone is like me. Is it Cheng Xuyuan who only arranges his own computer files in an orderly manner and deletes useless files in time? If so, then we can happily discuss the content of the next 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 be put in place in one step, so why not?

tmpwatch defaults to determining which files or directories to delete based on their access time (access time). In addition, you can also operate according to inode change time (inode change time) and modification time (modification time).

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

Important warning! !

Do not run tmpwatch in/(root)!
Do not run tmpwatch in/(root directory)! !
Do not run tmpwatch in/(root directory)! ! ! (3 warnings! ^-^)

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

Installing tmpwatch

Installation of tmpwatch is available in the default repository for 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 derivative versions (such as Ubuntu), tmpwatch is also called tmpreaper:


$ sudo apt install tmpreaper

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

The usage of tmpwatch and tmpreaper is almost identical, and it can be considered that the two are one kind of commands. In order to facilitate description, this article takes tmpwatch as an example to explain, and friends who use Debian-based system can change the following tmpwatch to tmpreaper.

1. Delete files that have not been accessed for more than X days
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
As mentioned earlier, tmpwatch deletes files by default based on the access time, and now we use the-m option to delete files based on the modification time of files (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/

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

d-Days h-hour m-min 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 them in the following form:


tmpwatch -m 10 /var/log/

3. Remove symbolic links

You can remove symbolic links 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 delete 1 special files, such as symbolic links, directories, pipe files and so on. In this case, you need to use the-a option:


tmpwatch -a 10 /var/log/

5. Exclude directories on deletion
If you do not want to delete a directory, you can use the--nodirs option to exclude deletion of the directory at the time of deletion:


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

6. Test deletion (do not actually delete anything)
Once again, don't rush to use the tmpwatch command when deleting files from important directories! Might as well look at what files are deleted after the command runs first, otherwise it hurts to delete the wrong skull. . Develop a good habit

You can enter test mode using-t:


$ sudo yum install tmpwatch
0

Output under CentOS 7:


$ sudo yum install tmpwatch
1

Output under the system based on Debian:


$ sudo yum install tmpwatch
2

In fact, the above process does not really delete files, but simulates deletion to tell you which files will be deleted.

You can remove the-t option and then execute tmpwatch for real deletion when you ensure that all the files you want to delete are correct.

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


tmpwatch -f 10h /var/log/

8. Skip some files when deleting
If you want to keep the specified file on deletion, that is, whitelist, you can use the--protect option. Suppose we want to keep all txt type files:


$ sudo yum install tmpwatch
4

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'.

Setting cron job to automatically delete files periodically

(Secretly tell you, tmpwatch/tmpreaper and cron job are better to eat at first.)

Enter the cron job task editing window:


$ sudo yum install tmpwatch
5

Add 1 cycle task:


$ sudo yum install tmpwatch
6

The above code sets tmpwatch to run at 1 am every day, and deletes files from 30 days ago.

Do not understand corn job small partners can search the Internet under its beginner's guide ha.

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 from 7 days ago. You can change this setting by modifying "TMPREAPER_TIME=7d" in the TMPREAPER. conf file.

Write at the end
Finally, under the reminder 1, when deleting files, 1 must carefully check the path to avoid data loss.

tmpwatch and tmpreaper man pages:


$ sudo yum install tmpwatch
7

Related articles: