Free up space in Linux (Ubuntu)
Strategies and commands to remove unnecesary data.
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.
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
- Script source: How to remove old version of installed snaps answer by popey.
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.
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:
References
- man apt
- man apt-get
- Find out IP addresses from MACs in a Local Area NetworkMay 10, 2023
- Choose any key as the modifier in i3wm in 6 stepsJanuary 20, 2021
- Adding a swap memory to Linux from command line in 6 stepsApril 2, 2020
- Free up space in Linux (Ubuntu)
- Switch between languages in Linux. Write in multiple languages with same keyboard.March 21, 2020
- How to make Ubuntu display emojisFebruary 12, 2020
- Detect and mount USB devices in Linux from consoleJanuary 24, 2019
- How to make screencasts in Ubuntu LinuxJanuary 21, 2019
- Using i3 window manager in LinuxJanuary 7, 2019
- Setting Up A Fresh Linux ServerAugust 25, 2018
- How To Download A Website With Wget The Right WayJune 30, 2017
- Replicate Installed Package Selections From One Ubuntu System To AnotherApril 24, 2017
- Using Clamav Antivirus In UbuntuJanuary 25, 2017
- How to Type Spanish Characters, Accents and Symbols in LinuxJune 6, 2016
Ubuntu
- How to activate tap to click touchpad's feature in Ubuntu in 4 stepsMarch 4, 2021
- Difference between suspend and hibernate in Ubuntu and how to execute them from command lineApril 12, 2020
- Solving Google Chrome's gpu-process error message in Ubuntu LinuxJanuary 7, 2019
- Solving Google Chrome's secret service operation error message in Ubuntu LinuxJanuary 7, 2019
- Start Emacs In Ubuntu The Right WayJune 10, 2017
Unix Shell
- Connect to a Bluetooth device from command line in Ubuntu LinuxJune 23, 2020
- Add Infolinks Script To An Existing Website From Console With Sed CommandApril 4, 2017
- How to change all files permissions to 644 and directories to 755January 10, 2017
- Shell Redirect Output And Errors To The Null Device In BashDecember 9, 2016
- Prevent Running Of Duplicate Cron JobsDecember 8, 2016
- Delete All Backup Files Recursively In BashNovember 28, 2016
- Bash Script to Find Out If MySQL Is Running Or NotNovember 9, 2016
Articles
Subcategories
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
·