How to clean junk file on Linux

Background

With the help from the friend in LCPU, I learnt and make a disk clean days before. As there was limited left space for me on my VPS, I had to learn how to clear unnecessary files, though I had never done it before. I am here writing the skills about cleaning junk file on Ubuntu.

How to clean junk file

First of all, we need to install a useful tool called ncdu which can help you find the largest junk file on your system.

1
apt install ncdu

Then use it by:

1
ncdu

You will find which file you need to delete. In my case, I need to clean the following junk file.

Clean apt cache

1
sudo apt clean

Clean yarn cache

1
yarn cache clean

Clean snap cache

1
sudo snap clean

Clean log file and set the limit space for log

1
2
journalctl --disk-usage # see how many space log file uses
vim /etc/systemd/journald.conf
1
2
3
[Journal]
SystemMaxUse=500M # set the max disk usage for log
RuntimeMaxUse=200M # set the max disk usage for log when running
1
2
sudo systemctl restart systemd-journald
sudo journalctl --vacuum-time=7d # delete old log file and keep the recent 7days log file

Outcome

Before clean

WechatIMG56986

After clean

WechatIMG57113