Keeping Linux Clean

One of the great things about Linux is how stable it is over time. The biggest challenge with Linux is getting it installed, finding and configuring the software you need to get stuff done. Once you get that accomplished, it pretty much just runs. There’s not much in the way of system maintenance you have to worry about. Windows, on the other hand, is what I call a “dirty” system,in that it generates lots and lots of extra data that it leaves on the hard drive as it runs. It’s notorious for slowing down over time, as this data piles up and Windows users either have to install software to clean all of this trash out or reload the system periodically to keep that freshly booted up feeling. There’s actually a whole industry devoted to selling “cleaners” for Windows., Some of these programs are really just malware in disguise but many are quite useful. Of course, the problem is figuring out which is which.

If you use Linux then you’re probably saying to yourself how glad you are that you don’t have to deal with such nonsense, but don’t speak too soon. While Linux isn’t nearly as dirty as Windows, it does benefit from a little dusting and cleaning every once in a while. Linux also generates data as it runs in the form of logs, caches, and temporary files. This extra data doesn’t usually affect system performance. The main reason anyone would want to clear this stuff out is to reclaim hard drive space. If you’re running SSDs, that might be something you care about since solid state drives tend to be smaller than what we’ve become accustomed to and we have to keep a close eye on the space we’re taking up until the price for large capacity SSDs comes down a bit. Let’s go through a few things you can do to tidy up your Linux box.


Removing Old Kernels

WARNING! Proceed with extreme caution when removing anything associated with the Linux Kernel! It is quite possible that removing the wrong package could cripple or totally trash your computer! You have been warned!

Every distribution of Linux handles kernel management differently. Some have nice GUI tools that allow you to choose what kernel to run like OpenSUSE, Linux Mint and Manjaro. Others very rarely or never update the kernel, leaving it entirely up to the user to do manually. The kernel is pretty important and having an updated kernel is one of the main ways you can keep your system safe from viruses and getting hacked. I’m going to focus on Ubuntu here but I encourage you to do a bit of research on kernels and learn how your distribution deals with updating them.

Ubuntu updates the kernel along with everything else on your system and it leaves the older kernels behind. Every once in a while, a kernel update breaks something and it’s nice to have the older one around so you can go back to it if you have to. The problem is that Ubuntu just leaves the old kernels there and after a while you can end up with a bunch of them installed on your system. Kernels and their associated files take up a lot of space – running into the hundreds of megabytes. Thus, removing old kernels can free up a lot of space.

The best tool for managing the kernel in Ubuntu is Synaptic Package Manager. If it is not already installed on your system, get it now. I show you how to do this in the video but let’s go through it here as well. Before you open Synaptic, open a terminal and issue this command:

uname -r

You should get an output that looks something like this:


This is the version number of the currently running kernel and you’ll want to make a note of it because we want to make damn sure we don’t do anything thing to this kernel or any of the files that go along with it.

Now that we know what kernel we’re running, we can go on and open Synaptic and see if there are any older kernels taking up space. To find the kernels, simply type “linux-image” into the search box and then sort the list by the installed version. You can do this by clicking on the header in the package list. Even if you only have one version of the kernel installed, you’re going to see a bunch of files listed that are installed along with it. You don’t have to mess with taking them out individually highlighting the main kernel image package and marking it for complete removal from the right click menu will cause the system to remove everything associated with it. Be sure to double check the version numbers to make sure you’re not removing the current kernel. If you’ve sorted the list by installed packages, the latest running kernel should be at the top of the list. You can mark all the old kernels now but before you click the apply button there’s one more package we need to look for.

Clear the search field and now do a search for “linux-headers.” If they’re installed, you should get a list of all the installed Linux header files associated with the kernels installed on your system. The version numbers go along with the kernel so just make sure you don’t remove the headers for your current kernel. Actually, the header files are not necessary for day to day operation of your computer but they are good to have around if you ever want to compile programs or drivers from source code. They don’t take up much space so I always leave the headers for the running kernel on the system.

Using BleachBit

There are some really nice programs for Linux that will automatically go through the system and remove extra files. The one I always go for is BleachBit. It’s been around quite a while and it’s available on just about every distribution. I have yet to run into any issues with it removing something that has broken anything on the system. BleachBit is lightweight but offers some pretty snazzy features. We’ll talk about the very basics here but do take some time to learn more about what it can do because you just might find some of the advanced features quite useful.

Once you install BleachBit, you’ll notice that you’ll find two entries for it in the dash or the applications menu. One will allow you to run it as root and the other is for running it as yourself. Running it as yourself will remove files that are associated with your account while running it as root will remove things like cached packages and system logs. BleachBit also lets you clear caches for web browsers, mail clients and plugins that might be installed on your system. Choose carefully here because it will happily blow out all of your saved passwords and settings and while that might be good if you want to clear everything, it might also result in unhappy users if you should run it on someone else’s account. I find that just clearing the cache in web browsers reclaims a huge amount of space while leaving the other settings intact.

Running BleachBit as root will let you use the APT cleanup commands to clear cached packages and updates on your system. You cold use the clean, auto clean and auto remove commands in a terminal, but here it’s all done for you. Speaking of the terminal, BleachBit is available there too. You can issue the ‘bleachbit’ command and it will run with the presets you setup with the GUI. This is nice because it means one can create a Bash script to automate system cleanup.

Defragmenting Your Hard Drives

It has been said many times that one of the advantages to running Linux is that you never ever have to defragment your hard drive. Well, this is not completely true. Most average users should never have to worry about files fragmenting to the point where it affects the performance of their spinning hard drives. The ext4 file system is very good at storing files in such a way that they do not fragment. Unlike the NTFS file system used on Windows, ext4 spreads data across the entire drive, grouping similar files together in such a way that there is plenty of space for them to grow without fragmenting. This means that as long as you have plenty of free space on your hard drive you probably won’t get much fragmentation at all. I have run systems for two or three years and checked the drives only to find that the fragmentation score was ‘0.’

So why do I bother telling you about defragmentation then? Well, there are some situations where you may need to think about it, like if your drive starts to fill up. Any spinning ext4 drive that’s more than 80 percent full is prone to start fragmenting files because it starts getting harder for the system to keep files separated enough to ensure space to grow. Another thing that might cause file fragmentation is if you have a whole lot of very large files. People who do a lot of video production, work with large databases or virtual machines will end up with a bunch of very large files. These files can become very fragmented over time and running defrag just may improve drive performance enough to be noticeable.

Open a terminal and issue this command to see if your system needs defragmenting:

sudo e4defrag / -c

Enter your password and the program will go off and analyze your file system. This can take up to a minute if you have a lot of files so be patient. You should see output that looks like this:

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.

The main thing we’re concerned with is the fragmentation score. It’s 0 in this case so we don’t have to worry about defragmenting the drive. If you have your home folder off in its own partition, and you really should, you would replace the / with /home to check on the fragmentation status there too. If you want to go on and run the defragmentation program, just issue the command without the -c argument:

sudo e4defrag /

It will then go off and go through the files system defragmenting files that need it. There will be lots of output scrolling by while it does this. This process can take a long time if you have a lot of files on your system so you may want to turn it loose late in the evening just before you go off to bed and have the system shutdown when it’s done. To do that, you could issue a command that looks like this:

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

What I generally do these days is go on and run the defrag tool after I finish setting up a new system and transferring all the files to it. After that, I will occasionally check the status and I have found that the fragmentation score pretty much stays at 0 from then on.

It’s worth mentioning that you should never ever attempt to defragment a solid state drive. As a matter of fact, running the above commands on an SSD will usually result in a message telling you that the operation is not allowed. Those of you who are using SSDs exclusively can just disregard this section.

One last thing: Don’t expect a tremendous boost in performance after defragging your drives in Linux because ext4 is really good at being fast even if there are some fragmented files on the drive. Remember, it was designed with high input/output file servers in mind and it’s very efficient. It doesn’t even breathe heavy when it has to handle your laptop or desktop devices.

Don’t Obsess!

Some computer users get very obsessive-compulsive about keeping their systems pristine and running at peak performance. Keep in mind that there are many Linux systems that run for years without doing anything I listed above. Linux simply doesn’t slow down like Windows or Mac does, even after prolonged use. The main reason you want to clear files is to reclaim drive space or to control the size of full system backups. If you are one of those who like to try different distros all the time, you may never end up having to do any of this, simply because you’re reinstalling so frequently. Still, it’s good to know that these tools are there.

Have fun!

Joe Collins
Joe Collins worked in radio and TV stations for over 20 years where he installed, maintained and programmed computer automation systems. Joe also worked for Gateway Computer for a short time as a Senior Technical Support Professional in the early 2000’s and has offered freelance home computer technical support and repair for over a decade.

Joe is a fan of Ubuntu Linux and Open Source software and recently started offering Ubuntu installation and support for those just starting out with Linux through The goal of EzeeLinux is to make Linux easy and start them on the right foot so they can have the best experience possible.

Joe lives in historic Portsmouth, VA in a hundred year old house with three cats, three kids and a network of computers built from scrounged parts, all happily running Linux.

2 thoughts on “Keeping Linux Clean”

  1. I find that, for Ubuntu at least, Ubuntu Tweak is a much handier tool for removing old Linux kernels. Its Janitor module not only scans and lists the older kernels and their headers, it also ignores the current in use kernel. On top of that, once you uninstall the older kernels, it goes back and flags the config files for those removed kernels and allows you to remove those as well. You can also pick which older kernels you want to get rid of, or just clean them all out wholesale.

Leave a Comment