74

I'm using Ubuntu 11.04 and I want to free up some space in my root directory, which is overloaded. I especially want to change the path used for installing applications (they are getting installed directly to the root drive).

Another consideration is that I'm working on a MySQL database server. The server is installed in the root directory itself, so I don't want to risk losing any data.

Please give me some tips to help sort out this problem.

Jorge Castro
  • 71,754
rihan
  • 841

16 Answers16

76

I successfully cleared 3.5 GB by removing old headers and images.

Please note on some systems this also attempts to remove the current kernel which is not a great idea - see comments below for how to check.

I used the following command:

dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | xargs sudo apt-get -y purge

You can check what packages will be purged executing the first part of the command:

dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d'

Reference

homebrand
  • 860
  • 6
  • 5
  • 1
    Other tips didn't help, but this saved me around 5G. Thanks. – baltasvejas Apr 14 '15 at 08:07
  • 4
    The option -y in the apt-get command is not really necessary. If you omit it apt-get will ask you (only once), if all piped packages should be removed. Imho it's better to not use -y, it gives you one more possibility to check (besides the second command mentioned above). For me this freed 15GB on the root partition! – Boris Däppen Jan 07 '17 at 13:03
  • 4
    The command dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | xargs sudo apt-get -y purge tried to remove the currently installed kernel version in my system. Be careful not to make your system unbootable. – HelloWorld101 Aug 09 '18 at 12:50
  • @Antony yaikes! i was about to try this. how can i verify which are ok to delete ? – Sonic Soul Aug 28 '18 at 15:12
  • 7
    @SonicSoul I eventually ended up doing what one of the answers below (https://askubuntu.com/a/778442/333806) suggests. Basically, ran uname -r to find the current kernel and took care not to remove it. If the command provided by homebrand to check which packages will be purged does not show the current kernel, then you should be safe running the purge command mentioned by him. – HelloWorld101 Aug 29 '18 at 13:25
  • Very nice. Be aware though that your system might think -as did mine- that it can purge the kernel currently in use. Debian has an alert right before the purge, luckily – Jos Apr 14 '21 at 12:16
  • @Antony this was a good safe check – Carmine Jun 04 '21 at 07:58
  • ignored the warning, accidentally deleted all my kali kernels :( NOW I CAN'T USE MY LAPTOP – BlueBeret Nov 20 '22 at 12:55
55

Recently I faced similar situation. Too many applications got installed and they started using my root mount space. I am listing out few steps which I followed and hoping that you could also use the same.

  1. Clean apt-get cache. Following command will remove all downloaded deb files from apt-get cache directory.

    Run this command: sudo apt-get clean

  2. Move /home mount point to different drive. Previously, my home folder was situated on root drive. So I moved my home folder to separate drive. This helped me to release lot of stress from root mount because most of applications store their data in /home/user_name/ folder. Read how to move home folder to separate drive.

  3. Increase size of root partition I know it is very obvious answer. But believe me, our data need changes over the time. I thought 20 GB /root mount would suffice but withing a year I have re-sized my root mount and increased to 50 GB.

Daan
  • 123
Amey Jah
  • 2,695
  • 14
    sudo apt-get clean freed up more space than expected, +1 – btk Mar 01 '14 at 02:46
  • Beware that apt-get clean removes even installed packages. You may want to keep those in case an upgrade fails. – timss Jul 12 '15 at 13:56
  • I cleaned my cache and it cleared up 10 Gb! +1 – Numeri Feb 09 '16 at 16:55
  • 1
    @timss According to the manual of apt-get, one could add APT::Clean-Installed "off"; to APT's configuration file (/etc/apt/apt.conf) and use apt-get autoclean. This way installed packages are not removed. – Giuseppe Jul 31 '20 at 15:19
47

Use dpkg-query to find the largest packages and remove the ones you don't need anymore (source):

dpkg-query --show --showformat='${Package;-50}\t${Installed-Size}\n' | sort -k 2 -n | grep -v deinstall | awk '{printf "%.3f MB \t %s\n", $2/(1024), $1}'
Pablo Bianchi
  • 15,657
  • 2
    Now this is a really useful one-liner. Perfect for a quick summary of the biggest install packages. Come to my dotfiles! – arainone Jul 01 '16 at 18:27
  • You can get this with wajig sizes – Pablo Bianchi Oct 14 '19 at 03:12
  • any suggestion on how to learn to be able to write such command? – Carmine Aug 01 '21 at 13:27
  • This has been very useful to find big packages, but I'll just add that you might find that uninstalled packages are still listed in there after removing them. For them to actually be completely gone, you will have to use sudo apt purge packagename instead of just sudo apt remove packagename. – stragu Nov 22 '21 at 03:12
  • I am struggling to create an alias for this, getting the error: bash: syntax error near unexpected token \(' . I have tried escaping the'and"characters with\` but that didn't help. – Paidoo Mar 04 '23 at 11:55
26

Here is a script I run to free space on root partition

function myclean {
    ## Show free space
    df -Th | grep -v fs
    # Will need English output for processing
    LANG=en_GB.UTF-8

    ## Clean apt cache
    apt-get update
    apt-get -f install
    apt-get -y autoremove
    apt-get clean

    ## Remove old versions of snap packages
    snap list --all | while read snapname ver rev trk pub notes; do
        if [[ $notes = *disabled* ]]; then
            snap remove "$snapname" --revision="$rev"
        fi
    done
    ## Set snap versions retain settings
    if [[ $(snap get system refresh.retain) -ne 2 ]]; then snap set system refresh.retain=2; fi
    rm -f /var/lib/snapd/cache/*

    ## Remove old versions of Linux Kernel
    # This one-liner is deprecated since 18.04
    # dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | xargs apt-get -y purge
    # New 2 lines to remove old kernels
    dpkg --list | grep 'linux-image' | awk '{ print $2 }' | sort -V | sed -n '/'"$(uname -r | sed "s/\([0-9.-]*\)-\([^0-9]\+\)/\1/")"'/q;p' | xargs apt-get -y purge
    dpkg --list | grep 'linux-headers' | awk '{ print $2 }' | sort -V | sed -n '/'"$(uname -r | sed "s/\([0-9.-]*\)-\([^0-9]\+\)/\1/")"'/q;p' | xargs apt-get -y purge

    ## Rotate and delete old logs
    /etc/cron.daily/logrotate
    find /var/log -type f -iname *.gz -delete
    journalctl --rotate
    journalctl --vacuum-time=1s

    ## Show free space
    df -Th | grep -v fs
}
cmak.fr
  • 8,696
19
sudo apt autoclean  # clean /var/cache/apt/archives folder which save packages while install.
sudo apt autoremove # this command remove unused packages.

sudo shutdown -rf   # it will restart your PC immediately and check filesystem in next boot.
Pablo Bianchi
  • 15,657
shantanu
  • 8,599
  • 31
    You provide no explanations. If someone was to just copy/paste your code, then the system would shut down with no warning, possibly causing a loss of data. Please don't provide commands without any explanation of what they do. -1 – Jo-Erlend Schinstad Aug 20 '11 at 19:14
  • 12
    Why the need to check the filesystem? – Nathan Osman Aug 20 '11 at 21:46
17

When I need make more free space on servers I use this command:

find / -type f -size +50M -exec du -h {} \; | sort -n

It finds all files bigger than 50 MB and "du -h" make a better list of files and "sort -n" after pipe make list numerically sorted by file size.

Pablo Bianchi
  • 15,657
zorbon.cz
  • 1,177
  • Depending on how large your system is, this can take some time. Took me 15 mins for about 500 gb on a HDD – Gabriel Fair Nov 07 '19 at 15:37
  • 3
    If you're trying to clear space under / then add -mount eg. alter above to "find / -mount -type ..." to restrict to files on the / partition ( not list files on other partitions/dirs mounted under / ) – Bob Mar 06 '22 at 15:11
15

Removing old kernel versions (as suggested already by homebrand) can free up a decent amount of space if you haven't yet got around to doing that.

There are a number of ways to remove the old kernel versions and a range of different options can be found in the answers posted to: How do I remove old kernel versions to clean up the boot menu?

My preferred method is mostly this answer from penreturns where it's broken down into fairly simple, understandable steps:

Open terminal and check your current kernel:

uname -r

DO NOT REMOVE THIS KERNEL!

Next, type the command below to view/list all installed kernels on your system.

dpkg --list | grep linux-image

Find all the kernels that are lower than your current kernel. When you know which kernel to remove, continue below to remove it.

Run the command below to remove the kernel you selected.

sudo apt-get purge linux-image-x.x.x.x-generic

The answer then says to 'update-grub2' when you're finished purging, which is likely to be out of date now: sudo update-grub should suffice for Ubuntu 14.04 onward. They also then say to 'Reboot your system' (which seems to be so that you can see the cleaned up boot menu) so in this case isn't necessary.

The grub bootloader menu used to show all the older kernel versions on the main page, but they are now placed out of the way behind a sub-menu. It's much neater but a newcomer to Ubuntu/Linux may not be aware that they are there taking up space.

As suggested, don't remove the current kernel and it's also advisable to keep the previous kernel version too, just in case you need to roll back to that one.

There are faster ways to do this, but I prefer the simplicity of this method, mainly because I can understand each command along the way:

"What kernel version am I using? What kernel versions do I have? Okay, purge that one."

Rinse, repeat, admire the space you've freed up.

It's fairly easy to copy the name of the specific older kernel you want to remove from the results that dpkg --list | grep linux-image gives you in the terminal, and then use sudo apt-get purge and paste the copied name in.

Removing 3 or 4 older kernels will usually free up about a GB of space in your root drive.

Pablo Bianchi
  • 15,657
pHeLiOn
  • 1,778
11

For me, the issue was dangling and unused Docker images, and volumes.

I use the following command to find how much space can be reclaimed:

docker system df

Next, if you intend to prune only dangling images:

docker system prune 

If you want to go all in and do a deep clean, then prune with force:

docker system prune -af --volumes

I reclaimed about 70 GB back. My day-to-day work involves a lot of work with Docker.

10

Following the instructions on the Ubuntu community docs I discovered a massive trash file -- it looked like a backup of /var/log/syslog.1, presumably something was spewing loads of output to syslog...

The command that found it was:

sudo find / -size +1G

And then any large file in a folder called .Trash is probably good to delete...

hwjp
  • 1,387
3

I've just created a bash script to do that:

IMPORTANT: before run this script reboot your system to ensure you have the last installed kernel running

#!/bin/bash

set -eu

REBOOT first to ensure you have the last installed kernel running

clean Journal

sudo journalctl --vacuum-size=50M

clean old unused kernels

sudo dpkg -l 'linux-' | sed '/^ii/!d;/'"$(uname -r | sed "s/(.)-([^0-9]+)/\1/")"'/d;s/^[^ ]* [^ ]* ([^ ])./\1/;/[0-9]/!d'

clean apt packages

sudo apt autoremove
sudo apt clean

first ensure to have the configured snap retain to 2

sudo snap set system refresh.retain=2

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

This will free up the common system garbage from journal logs, unused kernels and old snap packages.

I've just cleaned up to 7GB.

OTHER TIP:

If you use Docker, consider moving the content of /var/lib/docker in your home partition: https://www.guguweb.com/2019/02/07/how-to-move-docker-data-directory-to-another-location-on-ubuntu/

  • IMPORTANT: before run this script reboot your system to ensure you have the last installed kernel running Why does this need to be done? – David Mar 11 '22 at 11:33
  • 1
    @David sometimes you can install a new version of the kernel without rebooting and using it. So if you try to delete all kernel with version different from the current, you could accidentally delete the latest one installed and if you reboot could be a serious problem – bonaccorso.p Mar 11 '22 at 20:44
3

In Ubuntu, each folder can have its own filesystem. That means you can move any folder onto its own partition, another disk or even on a remote network. This is particularly popular for home directories, since that means you can reinstall Ubuntu without changing your personal settings or loosing any files. It's also popular in networks where users should be able to log onto different machines and still get their personal settings and files. But it is useful in many different cases, such as yours.

Applications aren't installed into a specific folder, like you seem to suggest. Different parts of the application is placed in different parts of the filesystem. The main program is usually placed in /usr/bin, whereas configuration files are placed in /etc, for instance. In your case, MySQL, the databases themselves are placed somewhere in /var. I think /var/mysql.

Since /usr and /var are both directories in the root filesystem, they will use the root filesystems space. But as I said, you can move them to different filesystems. In the case of MySQL, you can configure where databases are stored. You could easily move databases to /home/username/.mysql/databases for instance.

2

Check which folder is taking space in the root partiion

du -hsc *

I found \timeshift folder taking most of space. (You may or may not have enabled this periodic snapshot). You can move this from root to \home (assuming you have mounted home in a different partition). Or reduce the frequency from timeshift GUI and if you feel it is safe, delete the older backups.

Alex Punnen
  • 262
  • 3
  • 7
1

You cannot change the path where the package manager install applications. Most application files are saved to /usr. If you want to recover space on the root partition, moving /usr to a different partition is a possible solution.

From comments:

  • Preserve the permissions when copying, i.e. better use the command line if you are unsure what your file manager will do.

  • The right way to this, is to mount a new filesystem to /usr or use mount --bind. It's not clear how well a symlink would work.

Jan
  • 3,598
1

If you have a lot of seperate filesystems, the following trick might prove handy: Mount / another time, but this time under /mnt. Now all of your searching for large or many files can be done, without traversing wrong fileystems.

It can also help you find the files that are hidden under another mount.

1

As of today it's better to use the new feature (since 14.04) to remove older kernel images:

sudo apt autoremove

Check out more details: Why doesn't Ubuntu remove old kernels automatically?

Pablo Bianchi
  • 15,657
ywu
  • 11
  • 2
0

If you have mssql database server installed, that can eat up your memory. Run NCurses Disk Usage

$ sudo ncdu / 

To check what folders and files are taking max storage. The mssql data are usually stored on

/var/opt/mssql/backup/
Pablo Bianchi
  • 15,657
Yogesh
  • 119