After few months or years later, you will notice unnecessary files, libraries and/or documentation eating up your disk space On Debian or Ubuntu Linux. Try the following tips to free up disk space.

Remove cached .deb files

When you run apt-get command, it downloads and caches the package in /var/cache/apt/archives directory. cd into this directory:
# cd /var/cache/apt/archives
# du –ch


8.0K    ./partial
838M    .
838M    total

Remove all cached *.deb files using the following rm command:
# rm –f *.deb

Remove Orphaned package

The deborphan command finds packages that have no packages depending on them. The default operation is to search only within the libs and oldlibs sections to hunt down unused libraries. The following command displays orphaned package list:
$ deborphan -sz

  • -s: Show the sections the packages are in.
  • -z: Show the installed size of the packages found.

You can remove orphaned package with apt-get command remove option:
# apt-get remove packagename
Alternatively, remove all orphaned package in a single command, type:
# apt-get remove $(deborphan)
Better try:
# apt-get remove --purge $(deborphan)

If you find command line hard to use try orphaner command which is nothing but a neat frontend for deborphan displaying a list of orphaned packages with dialog or whiptail. Packages may be selected for removal with apt-get which is then called to do the work. After removal a new list of orphaned packages is gathered from deborphan. The program ends when either “Cancel” is pressed or no package is marked for removal.
# orphaner

(Fig 01: orphaner command in action )

However some packages are important and if you want to keep them forever, use editkeep command which is a frontend for deborphan displaying a list of orphaned packages (-a for all-sections is implied) and packages which are tagged to never been shown using dialog or whiptail:
# editkeep

cruft command

cruft is a program that goes over the Debian packaging system’s database, and compares the contents of that with the files actually on your system, and produces a summary of the differences. For example, you can clean spool with following command:
# cruft –k

Read the cruft man page for advance usage.

Remove log files

Backup log files and remove them from /var/log directory. To truncate log file you can run for loop:
# cd /var/log/squid
# for i in *; do >$i; done

Also, consider rotating log files using logroate facility.

See also:

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 8 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
8 comments… add one
  • Doug May 4, 2006 @ 22:07

    One point – you don’t have to manually remove the debs in Apt’s download archives. The command ‘apt-get clean’ will remove all the package files in /var/cache/apt/archives for you. Better. ‘apt-get autoclean’ will remove just those debs that can no longer be downloaded, and so are obsolete.

  • Badar Mar 12, 2007 @ 12:58

    Thanks a lot for this.. i was desperately needing it.

  • dot tilde dot Mar 2, 2008 @ 0:28

    is there any difference between

    rm -rf /var/cache/apt/archives/* and
    apt-get clean ??


  • dot tilde dot Mar 2, 2008 @ 0:30

    aah. should have read the comments, too.


  • dan Apr 12, 2008 @ 22:11

    also, to got reading every package description and then being asked to purge it or not, use the following:

    # for file in `deborphan`; do aptitude search "^$file$" && aptitude purge "$file";done;

  • pedro Dec 15, 2008 @ 21:35

    what about the files that are created on the folder var/bin every time we install something. using sudo apt-get clean this files disappear to?

  • pedro Dec 15, 2008 @ 21:39

    I made a mistake I meant to say the folder ‘usr/bin’ not ‘var/bin’

  • john Dec 14, 2010 @ 13:58

    Is there any way we can automate the above commands? Lets say once in a month or something? I have been spending last 6 hours just getting rid of those unnecessary files!

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum