In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to delete garbage files in Linux system, which has certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let Xiaobian take you to understand it together.
tmpwatch, which helps us recursively delete files and empty directories that have not been accessed in 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 determines 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 useless files elsewhere, such as old log files.
Important warning!
Do not run tmpwatch in/(root directory)!
Do not run tmpwatch in/(root directory)!!
Do not run tmpwatch in/(root directory)!!! Three warnings! ^ – ^ )
The/directory contains important files necessary for the Linux system to run, and tmpwatch has no built-in protection mechanism to prevent running on the/directory. Once those important files are deleted, the consequences are unimaginable! Therefore, friends must be careful when using this command!
Install tmpwatch
An installation of tmpwatch is available in the default repository 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 for a specified time
tmpwatch and tmpreaper are used almost identically, and can be thought of as the same command. For ease of description, this article takes tmpwatch as an example to explain, using Debian-based system friends 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 based on access time by default. Now we use the-m option to delete files based on their modification time.
Example: Delete files in/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 the time parameter, as follows:
d-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 something like this:
tmpwatch -m 10 /var/log/
3. Delete Symbolic Links
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 delete not only ordinary files, but also special files, such as symbolic links, directories, pipeline files, etc. In this case, you need to use the-a option:
tmpwatch -a 10 /var/log/
5. exclude directories when deleting
If you do not want to delete a directory, you can use the--nodirs option to exclude deletion of the directory when deleting:
tmpwatch -am 10 --nodirs /var/log/
6. Test deletion (does not actually delete anything)
Again, don't rush to use tmpwatch when deleting files from important directories! Might as well look at the command after the deletion of files which, or delete the wrong skull and pain. Make a good habit!
You can use-t to enter test mode:
tmpwatch -t 30 /var/log/
Output under CentOS 7:
removing file /var/log/wtmpremoving directory /var/log/ppp if emptyremoving directory /var/log/tuned if emptyremoving directory /var/log/anaconda if emptyremoving file /var/log/dmesg.oldremoving file /var/log/boot.logremoving file /var/log/dnf.librepo.log
Output from 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, in fact, does not actually delete files, just simulate deletion, telling you which files will be deleted.
After making sure that the files to be deleted are correct, remove the-t option and execute tmpwatch to actually delete them.
7. force delete
**tmpwatch does not delete files that the current user does not have write access to by default. ** But if you have to delete those files, you can use the-f option to force deletion:
tmpwatch -f 10h /var/log/
8. Skip certain files when deleting
To keep a specified file when deleted, i.e. whitelisted, 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 cron job to delete files automatically on a regular basis
(Between you and me, tmpwatch/tmpreaper works best with cron job.)
Enter cron job task editing window:
# crontab -e
Add a recurring task:
0 1 * * * /usr/sbin/tmpwatch 30d /var/log/
The code above sets tmpwatch to run at 1 a.m. every day and deletes files that are 30 days old.
Little friends who don't know corn job can search the Internet for its beginner's guide.
When tmpreaper is installed, it automatically creates a daily cron job (/etc/cron.daily/Tmpreaper). It reads the configuration from the/etc/timepaper.conf file and executes it. The default setting is to delete files older than 7 days. You can change this setting by modifying "TMPREAPER_TIME= 7d" in the TMPREAPER.conf file.
written in the end
Finally, remind you that you must carefully check the path when deleting files to avoid data loss.
tmpwatch and tmpreaper man pages:
$ man tmpwatch
$ man tmpreaper
Thank you for reading this article carefully. I hope the article "How to delete junk files in Linux system" shared by Xiaobian will be helpful to everyone. At the same time, I hope you will support it a lot. Pay attention to the industry information channel. More relevant knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.