Skip to content

How to Find Disk Usage while Excluding a Directory

Hello to all the readers!

I was upgrading my Manjaro linux system. And stumbled upon that the disk space is not sufficient to carry out the upgrade. Therefore, I started hunting what is consuming the disk and is unnecessary, so that it can be cleaned up.

I started running the common du command to find which directory under root is taking up disk space.

$ sudo du -sh /* | sort -h

du stands for Device usage or Disk usage. It calculates total usage of each directory.

But while running this command, I remember that /home is mounted as a separate disk partition and disk space warning is for /. Therefore, I had to skip calculating usage of /home folder. I could find that du provides an option to exclude particular directory. And this is how it works.

$ sudo du -sh --exclude=/home /* | sort -h
0       /bin
0       /dev
0       /lib
0       /lib64
0       /mnt
0       /proc
0       /sbin
0       /snap
0       /srv
0       /sys
8.0K    /rootfs-pkgs.txt
24K     /desktopfs-pkgs.txt
208K    /tmp
2.4M    /run
13M     /etc
207M    /root
266M    /boot
3.3G    /opt
19G     /usr
37G     /var

--exclude matches a regex pattern. So, create the exclude pattern carefully.

Okay and now I can see that some unnecessary stuff lies under /var/ directory as cache, logs are stored there.

And after searching more, I could find that it is the pacman that uses around 11 GB of storage /var/cache/pacman/pkg and thereby upgrade fails to download latest packages and put there in the cache.

So I found that pacman keeps downloaded packages in the cache folder even if some previous packages have been uninstalled. Still it keeps those in the cache. Therefore I need to clear this cache to get ample disk back for filesystem operations.

sudo pacman -Sc                                                                                                                                                                                                     

Packages to keep:
  All locally installed packages

Cache directory: /var/cache/pacman/pkg/
:: Do you want to remove all other packages from cache? [Y/n] Y
removing old packages from cache...

Database directory: /var/lib/pacman/
:: Do you want to remove unused repositories? [Y/n] Y
removing unused sync repositories...