Checking free space

  1. df

    This is the most basic command of all; df can display free disk space. Here’s what it will return when run:

     
    # df
     
  2. df -h

     
    # df -h
     

    Filesystem Size Used Avail Use% Mounted on

  3. df -Th

    When figuring out disk space issues it can be important to know what type of file system you’re dealing with to plan accordingly. For instance, the ext4 file system in Linux has a maximum file system size of 16 Tb and can support files up to 2 Tb.

    The "df -Th" command will display the same output as in the previous command but also includes file system types:

     
    # df -T -h
     
  4. du -sh *

    The du command is short for "disk usage." In its most basic usage, if you simply run "du" at the root volume, it will show you the size of all the directories on your system. However, this may produce a glut of information difficult to parse.

    Adding the switches -sh ("s" means "summarize" and again, "h" means " human readable format") to run du - sh * at the root volume will produce a cleaner output.

     
    # du -sh *
     

    You can easily see which directories contain the largest amount of data

  5. du -a /var | sort -nr | head -n 10

    Let’s parse this command string.

    du is the disk usage command, of course.

    -a is the "all" switch to show all items.

    /var tells du to scan the /var directory.

    The pipe ("|") character pipes the results to the sort command.

    sort -nr will display the largest directories at the top of the list.

    The pipe ("|") character pipes the results to the head command.

    head -n 10 will limit the number of results to the top ten largest directories.

     
    # du -a /var | sort -nr | head -n 10
     
    Note
    to run this against the entire file system, just change:
      
    du -a /var | sort -nr | head -n 10
        to
    du -a / | sort -nr | head -n 10
     
  6. du -xh / |grep '^\S*[0-9\.]\+G'|sort -rn

    Looks very complicated but it’s easy to break down:

    du is the disk usage command

    -x tells du to only check this file system (makes the command run faster) and the h after it is to present results in human readable format.

    The pipe ("|") character pipes the results to the grep command.

    grep searches the results using the '^\s*[0-9\.]\+G' regex - [0-9\.] will list all directories and subdirectories beginning with (^\S) non-whitespace characters (such as a tab, carriage return, etc.) - it must show alpha characters only (no numeric characters) and +G will display directories 1 Gb in size or larger.

    The pipe ("|") character pipes the results to the sort command.

    sort -rn will display the largest directories at the top of the list.

     
    # du -xh / |grep '^\S*[0-9\.]\+G'|sort -rn
     
  7. find / -printf '%s %p\n'| sort -nr | head -10

    The above is one of my favorite commands since it will show me the ten biggest files on my system. Like before, let’s break it down.

    find is the find command to search for files, obviously.

    / is the root volume

    -printf '%s %p\n' will nicely format the results by showing file size in bytes (%s) and the file name (%p).

    The pipe ("|") character pipes the results to the sort command.

    sort -nr will display the largest directories at the top of the list

    The pipe ("|") character pipes the results to the head command.

    head -n 10 will limit the number of results to the top ten largest directories.

     
    # find / -printf '%s %p\n'| sort -nr | head -10
     
  8. find / -xdev -type f -size +100M -exec ls -la {} \; | sort -nk 5

    find is the find command to search for files, obviously.

    / is the root volume.

    -xdev - only search this file system.

    -type f - look for files.

    -size +100M - only show files over 100 Mb in size.

    -exec ls -la {} \; - runs the ls command to show all files with the long listing format.

    The pipe ("|") character pipes the results to the sort command.

    sort -nk 5 will display the largest files at the bottom of the list.

     
    # find / -xdev -type f -size +100M -exec ls -la {} \; | sort -nk 5
     

Deciding what to clean up

Finding what’s taking up space on your file system is the easy part. Figuring out what you can get rid of is trickier. If you found large database files and removed them you could produce catastrophic consequences.

Conclusion

These are only a few of the many powerful commands (and accompanying command switches) you can run in Linux.

How to clear the APT cache

Inspect APT cache configuration

Check out the current APT cache configuration using the following command.

 
$ apt-config dump | grep "^Dir\( \|::Ca\)"
 

Remove every package from the cache

Perform a simulation of the whole process.

 
$ sudo apt-get clean --dry-run
 

Remove retrieved package files.

 
$ sudo apt-get clean
 

Remove outdated packages from the cache

Perform a simulation of the whole process.

$ sudo apt-get autoclean --dry-run