Summary of garbage cleaning methods in Linux system

Time:2020-11-24

One of the biggest advantages of Linux is that it remains stable after a period of use. The biggest challenge for Linux is to install it and find and configure the software you need to do your job. Once you’ve done that, it’s almost going to work. As for system maintenance, there is not much to worry about. On the other hand, windows is what I call a “dirty” system, because it generates a lot of extra data during its operation and leaves it on the hard disk. As we all know, after a period of time, with the continuous accumulation of this kind of data, the running speed of windows will naturally slow down. At this time, Windows users either install software to clean up all the junk data, or regularly reload the system to make the startup speed feel brand new. In fact, there is an entire industry dedicated to selling windows oriented “cleaning tools.”. Some of them are actually malware in sheep’s clothing, but many are quite useful. Of course, the key is to figure out which tool is really good.

If you use Linux, you may be glad you don’t have to deal with this kind of hassle, but don’t get too excited. Although Linux is not as dirty as windows, it does benefit in part from the occasional cleanup. Linux also generates data during the running process, including logs, cache data and temporary files. These additional data usually do not affect the performance of the system. The main reason to clean up the data is to reclaim the hard disk space. If you are running a solid state drive (SSD), this may be something you care about, because the capacity of SSD is often not as large as the traditional hard disk we are used to using, so we should pay close attention to the space occupied until the price of high-capacity SSD has dropped a little. Here are a few key points you can take to clean up your Linux system.

Clear old kernel
Warning! Be extremely careful when cleaning anything related to the Linux kernel! Clear the wrong software package is likely to cause your computer to crash, or completely reduced to garbage! We warned you!

Each Linux distribution handles kernel management differently. Some have excellent GUI tools that let you choose which kernel to run, such as openSUSE, Linux mint, and manjaro. Others rarely or never update the kernel, leaving manual updates entirely to the user. The kernel is very important. Having an updated kernel is one of the main ways to keep your system safe and away from viruses and hackers. I’ll focus on Ubuntu here, but I’d like to encourage you to dig deeper into the kernel to see how the distribution you’re using handles kernel updates.

Ubuntu updates the kernel along with everything else on the system, leaving the old kernel behind. Kernel updates occasionally cause problems, so it’s better to keep the old kernel. That way, you can go back to the old kernel if you need to. The problem is that Ubuntu leaves the old kernel there completely; after a while, you end up with a bunch of old kernels installed on your system. The kernel and related files take up a lot of space, up to hundreds of MB. As a result, cleaning up the old kernel frees up a lot of space.

The best tool for kernel management in Ubuntu is Synaptic Package Manager. If it’s not already installed on your system, install it now. Before you open the manager, open the terminal and execute this command:

Copy code

The code is as follows:

uname -r

You should see something like this:

Copy code

The code is as follows:

3.16.0-38-generic

This is the version number of the kernel that is currently running and needs to be written down because we want to make sure that nothing has been done to the kernel or any files that come with it.

Now that we know what version to run, we can open the manager and see if any old kernels are taking up space. To find the kernel, just type “Linux image” into the search box and sort the list by installed version. To do this, you can click on the title of the package list. Even if you have only one version of the kernel installed, you will see a list of files installed along with it. You don’t have to clean them up one by one. By highlighting the main kernel image package and marking it as completely cleared from the right-click menu, you can have the system clear all the files associated with it. Be sure to double check the version number to make sure that the current version is not cleared. If you sort the list by installed packages, the latest running kernel should appear at the top of the list. Now you can tag all the old kernels, but you need to look for another package before you click the Apply button.

Clear the search bar and now search for “Linux headers.”. If they are installed, you should get a list of all installed Linux header files related to the kernel installed on the system. The version number accompanies the kernel, so make sure you don’t clear the title file for the current version. In fact, the title file is not necessary for the daily operation of the computer, but if you want to compile a program or driver from source code, having a header file is a good thing. They don’t take up too much space, so I always leave the header file of the running kernel on the system.

Using bleachbit
There are really great Linux programs that automatically check the system and clean up extra files. One of the programs I always use is bleachbit. It has a long history and is available on almost every Linux distribution. I’ve used it to clean up any abnormal files on the system, and I haven’t encountered any problems. Bleachbit is lightweight, but it offers some fairly powerful features. We’re only going to introduce the basic features here, but it takes a little time to get a deeper understanding of its capabilities, and you’ll find some advanced features very useful.

Once you have bleachbit installed, you will see two entries corresponding to it in the dashboard or application menu. One lets you run it as root, and the other runs it as you. Running as yourself will clear files related to your account, while running as root will clear cached packages and system logs and other data. Bleachbit also allows you to empty the cache of web browsers, email clients, and plug-ins that may be installed on the system. Be careful when choosing here, because it will erase all your saved passwords and settings; if you want to clear everything, it may not matter, but if you use someone else’s account to run it, it may upset users. I’ve found that just clearing the cache in your web browser can reclaim a lot of space by leaving other settings unchanged.

Running bleachbit as root allows you to clear cached packages and updates on your system using the apt cleanup command. You can use clean, auto clean and auto remove commands in the terminal, but they are all ready for you here. Speaking of terminals, bleachbit can also be used here. You can execute the “bleachbit” command and it will run at the default values that you configured with the GUI. This is good because it means you can make a bash script that cleans up the system automatically.

Defragment the hard disk
It’s been said many times: one of the advantages of running Linux is that you never need to defragment your hard disk. In fact, it is not entirely so. Most ordinary users never have to worry about file defragmentation, because the fragmentation will not affect the performance of the rotating hard disk. Ext4 file system is very good at storing files in a way that does not defragment. Unlike the NTFS file system used on windows, ext4 spreads data across the entire hard disk, grouping similar files into one category, so that there is no need to defragment, leaving a lot of space for file growth. This means that as long as you have a lot of free space on your hard disk, you may not need to defragment at all. My system has been running for two or three years. After checking the hard disk, I found that the defragmentation score is “0”, which indicates that there is no need to defragment at all.

In that case, why should I bother telling you to defragment? In some cases, you may need to consider defragmentation, such as if your hard disk starts to fill up. Any ext4 spinning hard disk that has taken up more than 80% of its space tends to begin to defragment files, as the system separates the files and ensuring that space increases starts to become more difficult. Another situation that causes file defragmentation is if you have a very large number of files. In the end, users will have to deal with a large number of video files if they are faced with a large number of virtual machines or large databases. After a period of time, the fragmentation of these files will become very serious, and running defragmentation will completely improve the performance of the hard disk.

Open the terminal and execute this command to see if your system needs to be defragmented:

Copy code

The code is as follows:

sudo e4defrag / -c

Enter the password and the program will quickly analyze your file system. If you have a lot of files, this process can take a minute, so be patient. You should see something like this:

Copy code

The code is as follows:

now/best size/ext

1. /var/log/syslog 3/1 4 KB 
2. /var/log/samba/log.nmbd 2/1 4 KB 
3. /var/log/boot.log 2/1 4 KB 
4. /var/log/auth.log 3/1 5 KB 
5. /var/log/ConsoleKit/history 3/1 10 KB 
 
Total/best extents 128413/128382 
Average size per extent 41 KB 
Fragmentation score 0 
[0-30 no problem: 31-55 a little bit fragmented: 56- needs defrag] 
This directory (/) does not need defragmentation. 
Done.

General cleaning methods for disks
1. Scan and compare important file systems regularly, and analyze those files that are often read and written

Copy code

The code is as follows:

#IS-IR/home>;files.txt
#diff filesold.txt files.txt

By analyzing and predicting the growth of space, we can also consider the compression of infrequent read-write files to reduce the occupation of space.
2. Check inodes consumption of spatial file system

Copy code

The code is as follows:

#df-i/home

If there are still a large number of inpdes available, it indicates that large files take up space. Otherwise, a large number of small files may occupy space.
3. Find the directory that takes up more space
View the space occupied by / home

Copy code

The code is as follows:

#du-hs/home

Check that the space occupied by / home is more than 1000m

Copy code

The code is as follows:

#du/awk’$1>;2000′

4. Find out the files that take up more space

Copy code

The code is as follows:

#find/home-size +2000K

5. Find the recently modified or created files
First touch a file of the time you want as follows

Copy code

The code is as follows:

#TOUCH-t 08190800 test
#find/home-newer test-print

Delete log

Copy code

The code is as follows:

#rm-rf/var/log/*

6. Connect the partitions
In the partition with space, connect the partition without empty space

Copy code

The code is as follows:

#in-s/home/use/home

7. Identify processes that consume a lot of space
According to different applications, find out the corresponding process and analyze the reasons.
8. Check and repair the file system

Copy code

The code is as follows:

#fsck-y/home

9. Restart the machine
With the above ten moves, we should be able to solve most of the problems, but the key is to plan the partition when installing. In addition, when the disk is found to be brute, do not rush, operate carefully, carefully analyze the cause, and then deal with it carefully. It should be noted that the above ten moves do not need to be executed in sequence. Some may be blocked by one move, and some may need to be used in combination with several moves. Be careful when deleting. If not, we have to increase the hard disk and re install the system
just so so:

Copy code

The code is as follows:

cd/
du-h–max-depth=q/grep M/sort-n

Find the largest directory and enter it
Rerun

Copy code

The code is as follows:

du-h-max-depth=1/grep M /sort-n

Find out and see if it’s useful
Cut it out if it doesn’t work
It is hoped that the above cleaning methods and steps can release some disk space for users operating Linux system.
The main parameter we are concerned about is the defragmentation score. It’s zero here, so we don’t have to worry about hard disk defragmentation. If your home folder is not in its own partition, then you should indeed change / to / home and check the defragmentation status there. If you want to run the defragmenter, simply execute the command without the – C parameter:

Copy code

The code is as follows:

sudo e4defrag /

It then checks the file system and defragments the files that need to be defragmented. In this process, there will be a large number of output scrolling. If you have a lot of files on your system, it’s a long process, so you can start the defragmenter before you go to bed at night, and then shut down the system. To do this, you can execute commands like this:

Copy code

The code is as follows:

sudo e4defrag / && sudo e4defrag /home && sudo shutdown -h now

Now, what I usually do is run the defragmenter after I’ve finished installing the new system and reversing all the files. After that, I would check the status occasionally, and I found that since then, the defragmentation score has basically remained at 0.

It’s worth mentioning that you don’t need to defragment your SSD at all. In fact, executing the above command on the SSD usually pops up a message saying that this operation is not allowed. Those who only use SSDs don’t need to read this section.

Finally: after defragmenting the hard disk in Linux, don’t expect a big performance improvement, because ext4 is good at running fast even if there are some fragmented files on the hard disk. Remember: ext4 was designed with a high input / output file server in mind, and it’s very efficient. Even if you’re dealing with a laptop or a desktop, it won’t be “breathless.”.

Don’t be too clean!
Some computer users are making sure the system is clean and running at the highest performance. Keep one thing in mind: many linux systems have been running for years without taking these measures at all. Linux doesn’t slow down like windows or Mac, even after a long time of use. The main reason you want to empty files is to reclaim hard disk space or control the size of a full system backup. If you like to try different distributions as much as others, it’s likely that you won’t need to take any of these measures at all in the end, because you’re reloading frequently. But it’s always a good thing to know that there are such tools.

Recommended Today

Talking about Java collection

preface Most programming languages provide array to save objects, and array is one of the most important data structures. However, the length of the array has been defined during initialization, which is immutable and troublesome to use. Therefore, Java inJDK 1.2The collection framework is added to the version to save and manipulate objects. The container […]