In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to delete junk files in Linux, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
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 done in one step, so why not?
Tmpwatch defaults to decide which files or directories to delete based on the access time (access time) of the file or directory. In addition, you can also operate according to the 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 tmpwatchdogs in / (root directory)!
Do not run tmpwatchdogs in the / (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 it from running on the / directory. Once those important files are deleted, the consequences will be unimaginable! Therefore, the children must be careful when using this command!
Install 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 are not accessed within a specified period of time
The usage of tmpwatch and tmpreaper are almost the same, and they can be considered the same command. For ease of description, this article takes 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
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/
two。 Delete files that have not been modified for more than X days
As mentioned earlier, tmpwatch deletes files based on access time by default, so now we use the-m option to delete files based on file modification time (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-number of days
H-hour
M-minute
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. Delete symbolic link
You can delete symbolic links 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, pipe files, and so on. In this case, use the-an option:
Tmpwatch-s 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 the deletion of the directory when deleting:
Tmpwatch-am 10-- nodirs / var/log/
6. Test deletion (do not actually delete anything)
Again, don't rush to use the tmpwatch command when deleting files from important directories! You might as well take a look at the files deleted after the command is run, otherwise the wrong skull will hurt again. Form 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 based on Debian system:
$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 really delete files, but simulates deletion to tell you which files will be deleted.
Only when you make sure that the files you want to delete are correct can you remove the-t option and then perform tmpwatch for actual deletion.
7. Force deletion
By default, tmpwatch does not delete files that the current user does not have write access to. But 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 when you delete it, that is, to be whitelisted, you can use the-- protect option. Suppose we want to keep all files of type txt:
Tmpreaper-- protect'* .txt'- t 10h / var/log/
Output result:
(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
(to tell you secretly, tmpwatch/tmpreaper is better served with cron job.)
Go to 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 a. M. every day and delete files from 30 days ago.
If you don't know corn job, you can search the Internet for its beginner's guide.
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 that are 7 days old. You can change this setting by modifying "TMPREAPER_TIME=7d" in the TMPREAPER.conf file.
Write at the end
Finally, I would like to remind you that when you delete a file, you must carefully check the path to avoid data loss.
Tmpwatch and tmpreaper man pages:
The above $man tmpwatch $man tmpreaper is all the contents of the article "how to delete junk files in Linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.