Free up space in Linux (Ubuntu)

Tux cleaning
Image: Tux cleaning (License: CC-BY-SA-NC Marcelo Canina)

Strategies and commands to remove unnecesary data.

Published:
Last modified:

Overview

After using Linux for a while (i.e.: years) we need to clean up things. This is a guide to suggest commands and strategies I common use when need to free up space in hard drives.

Check

First of all, let’s check the free space of each drive with df -h, the -h parameter stands for human readable sizes, i.e. count in MB or GB. In my case:


$ df -h
Filesystem               Size  Used Avail Use% Mounted on
udev                     910M     0  910M   0% /dev
tmpfs                    189M  1,4M  187M   1% /run
/dev/mmcblk0p3           9,1G  7,8G  810M  91% /
tmpfs                    941M   81M  860M   9% /dev/shm
tmpfs                    5,0M  4,0K  5,0M   1% /run/lock
tmpfs                    941M     0  941M   0% /sys/fs/cgroup
/dev/loop0                55M   55M     0 100% /snap/core18/1705
/dev/loop1                19M   19M     0 100% /snap/node/2336
/dev/loop2                92M   92M     0 100% /snap/core/8689
/dev/loop3                25M   25M     0 100% /snap/heroku/3881
/dev/loop4               161M  161M     0 100% /snap/gnome-3-28-1804/116
/dev/loop5                49M   49M     0 100% /snap/gtk-common-themes/1474
/dev/loop6               1,0M  1,0M     0 100% /snap/gnome-logs/93
/dev/loop7                29M   29M     0 100% /snap/hugo/7807
/dev/loop8               4,3M  4,3M     0 100% /snap/tree/18
/dev/loop9                15M   15M     0 100% /snap/gnome-characters/495
/dev/mmcblk0p1            37M  7,2M   30M  20% /boot/efi
/dev/mmcblk0p2            20G   19G  190M  99% /home
tmpfs                    189M  8,0K  189M   1% /run/user/1000

Analysis

GUI and console

DUC “is a collection of tools for inspecting and visualizing disk usage.”. To install i: sudo apt-get install duc .


$ duc
usage: duc  [options] [args]

Available subcommands:

  help      : Show help
  index     : Scan the filesystem and generate the Duc index
  info      : Dump database info
  ls        : List sizes of directory
  xml       : Dump XML output
  graph     : Generate a sunburst graph for a given path
  cgi       : CGI interface wrapper
  gui       : Interactive X11 graphical interface
  ui        : Interactive ncurses user interface

Global options:
       --debug               increase verbosity to debug level
  -h,  --help                show help
  -q,  --quiet               quiet mode, do not print any warning
  -v,  --verbose             increase verbosity
       --version             output version information and exit

Use 'duc help ' or 'duc  -h' for a complete list of all
options and detailed description of the subcommand.

Use 'duc help --all' for a complete list of all options for all subcommands.

GUI

A graphical disk usage analyzer helps us to have a representation of each directory dize from a selected drive, my favorite is xdiskusage which have a simple interface and works fast.

xdiskusage screenshot

Packages

Debian/Ubuntu

  • apt autoremove Remove automatically all unused packages.
    • “autoremove is used to remove packages that were automatically installed to satisfy dependencies for other packages and are now no longer needed as dependencies changed or the package(s) needing them were removed in the meantime.”
  • apt-get clean
    • “Erase downloaded archive files”

$ sudo du -hs /var/cache/apt
104M	/var/cache/apt
$ sudo apt-get clean
$ sudo du -hs /var/cache/apt
48K	/var/cache/apt

GUI remove

To explore installed packages and their sizes, the best app is Syanptic package manager, running sudo synaptic.

Synaptic is a graphical package management program for apt. It provides the same features as the apt-get command line utility with a GUI front-end based on Gtk+.

If you don’t have it, just run: sudo apt-get install synaptic

Ruby Gems

gem cleanup

And remove all unused gems with:

sudo gem cleanup | grep -Z "gem uninstall"| while read -r line ; do 
    echo "Uninstalling $line"; 
	tee sudo "$line"; 
done

Script explained at: How To Uninstall Old Versions Of Ruby Gems.

Snaps

Snap package manager, keeps old versions in each update.

Check used space: du -hs /var/lib/snapd/snaps/.

Now we can remove all old versions of snaps and keep the current active version:

#!/bin/bash
# Removes old revisions of snaps
# CLOSE ALL SNAPS BEFORE RUNNING THIS
set -eu

LANG=C snap list --all | awk '/disabled/{print $1, $3}' |
    while read snapname revision; do
        snap remove "$snapname" --revision="$revision"
    done

Logs

Journal

Some distros use excessive space for logs, you can adjust how long to keep your logs depending on your needs.

To check the space used by the Linux Journal: journalctl –disk-usage, that will show the size typically of the folder du -hs /var/log/journal/.


$ journalctl
--disk-usage
Archived and active journals take up 800.3M in the file system.

To clean systemd journal logs you can choose to remove a certain period of time, using journalctl to query the systemd journal, for example, 3 days: sudo journalctl –vacuum-time=3d


$ sudo journalctl --vacuum-time=3d
-- Logs begin at Mon 2019-12-09 07:22:01 -03, end at Fri 2020-03-27 10:23
dic 09 07:22:01 scarone systemd[1057]: Stopped Pending report trigger for....
...
Deleted archived journal /var/log/journal/b6fb7asdfkladsjf407f9b129df238ba5b7d/system@4a6015sladgj8c442c90502dd44d56d682-000000000019gas-0005a45rkjs6f35fac2.journal (8.0M).
Vacuuming done, freed 760.3M of archived journals from /var/log/journal/123473222d42407f9b129df238ba5b7d.

But to limit the journal log to get bigger again, tweak its configuration file at sudo vi /etc/systemd/journald.conf.

For example, to avoid getting the logs bigger than 150M, change the parameter SystemMaxUse which ‘Enforce size limits on the journal files stored’1:

Journal]
#...
SystemMaxUse=150M

Finally, take the changes into account and vacuums sudo service systemd-journald restart.


$ journalctl
--disk-usage
Archived and active journals take up 192.0M in the file system.

Files

Duplicates

Look for duplicate files.

  • In command line: fdupes2
    • “FDUPES is a program for identifying or deleting duplicate files residing within specified directories.”
  • GUI: fslint3
    • “FSlint is a utility to find and clean various forms of lint on a filesystem”

Big files

Most of the time some extremely big files placed somewhere eats up a lot of space, the problem is to locate them.

We can look for them easily with the powerful find command using the parameter -size n[cwbkMG] to spot files using at least n units of space.

For example, to locate files bigger than 200 MB: find . -type f -size +200M.

Ducks

Retrieve the list of top 10 largest files in a directory.

We can have some ducks 🦆 in our command line for this.

First we calculate the size of each file/directory with du -cks.

Explanation:

  • du: estimate file space usage. 4
    • -c: produce a grand total
    • -k: same as --block-size=1K
    • -s: summarize; display only a total for each argument

Second, order the above list by its size with sort -rn.

Explanation:

  • sort: sort lines 5
    • -r: reverse the result to make bigger numbers appear first
    • -n: order by number, same as --numeric-sort

Resulting command: du -cks * | sort -rn | head -10’

🦆: alias ducks=‘du -cks * | sort -rn | head -10’

There is also an alternative to du called NCurses Disk Usage Ncdu, which is “a disk usage analyzer with an ncurses interface”: https://dev.yorhel.nl/ncdu. 6

Optimization

Optimizing existing files is also a possible way to reduce used space. Obviously you won’t optimize every possible file, just find the biggest ones and make them smaller.

PDF

Detect biggest PDF files: find ~/ -iname “*.pdf” -type f -print0 | xargs -0 ls -s | sort -n | tail.

Optimize them with:

  • Ghostscript ps2pdf myfile.pdf
  • Pdfsizeopt 7

Images

Some extremely big images can also be good candidates for image optimization.

Detect biggest JPG or PNG files: find ~/ -iname “*.jpg” -type f -print0 | xargs -0 ls -s | sort -n | tail.

Optimize them with:

  • For JPGs: jpegoptim 8
  • For PNGs: optipng 9

References

  • man apt
  • man apt-get
Uruguay
Marcelo Canina
I'm Marcelo Canina, a developer from Uruguay. I build websites and web-based applications from the ground up and share what I learn here.
comments powered by Disqus


How to remove unneeded data in Linux, commands and software recommendations to help you remove caches, temporary files, duplicates, old versions, etc.

Clutter-free software concepts.
Translations English Español

Except as otherwise noted, the content of this page is licensed under CC BY-NC-ND 4.0 . Terms and Policy.

Powered by SimpleIT Hugo Theme

·