June 21, 2018

5 Linux Speed Tips - page 2

Quick Launchers, File Manager Shortcuts

  • February 1, 2010
  • By Carla Schroder

Viewing Available Disk Space

I work with audio and digital photo files, so I need to keep an eye on how much disk space I have. This little command-line incantation spits it out long before graphical file managers finish adding it all up:
$ df -h | grep ^/dev
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdb1       19G  3.3G   15G  19% /
/dev/sdb2       54G  3.8G   47G   8% /home
/dev/sda1      193G  118G   66G  65% /home/carla/sda1
df is the "disk-free" command. -h means "human-readable". Piping it through grep weeds out the temp, udev, and other filesystems that have nothing to do with available disk space. The caret, ^, means "beginning of the line matches only." Without using grep to filter the results it looks like this:
Filesystem      Size  Used Avail Use% Mounted on
/dev/sdb1        19G  3.3G   15G  19% /
tmpfs           506M     0  506M   0% /lib/init/rw
udev             10M  740K  9.3M   8% /dev
tmpfs           506M     0  506M   0% /dev/shm
/dev/sdb2        54G  3.8G   47G   8% /home
/dev/sda1       193G  118G   66G  65% /home/carla/sda1
Without ^, it would still display the udev and tempfs filesystems.

Finding Biggest Directories

This little du (disk used) incantation sorts directories by size, so you can quickly find disk hogs. This example displays all directories one gigabyte or larger, starting from the current working directory, sorted with the biggest last:
$ du -h  | grep ^[0-9.]*G | sort -n
23G     ./sda1/carla
36G     ./sda1/photos/2007
39G     ./sda1/Photos
53G     ./sda1/photos
118G    ./sda1
121G    .
Again, -h means "human-readable", instead of in bytes with no commas. grep ^[0-9.]*G means "show lines beginning with any number that also ends in capital G", and then sort -n puts them in order. To see the largest values first, use rn, reverse numbering. You can specify any directory like this:
$ du -h /home/carla/* | grep ^[0-9.]*G | sort -n
You need the asterisk wildcard, which means "everything", to display subdirectories.

This finds the ten biggest directories:

$ du -h |grep ^[0-9.]*G | sort -rn| head -n 10
If you don't have directories that take up gigabytes, M is megabytes and K is kilobytes.

I'm a pretty good typist, but typing all that out wastes precious keystrokes. So I created a Bash alias, and now all I do is type topten to get the ten largest directories in my current working directory. Aliases go in ~/.bashrc. This is what my topten alias looks like in ~/.bashrc, including a comment so I know what it does:

# find top ten biggest directories
alias topten='du -h |grep ^[0-9.]*G | sort -rn| head -n 10'

~/.bashrc is read after new logins, so to get instant gratification open a new terminal and type in your new alias:

$ topten
121G    .
118G    ./sda1
53G     ./sda1/photos
39G     ./sda1/Photos
36G     ./sda1/photos/2007
23G     ./sda1/carla
17G     ./sda1/photos/2008
16G     ./sda1/Photos/2007-sept-montana
16G     ./sda1/photos/2007/2007-sept-montana
5.4G    ./sda1/carla/pictures
Type alias to see your existing aliases.

Most Popular LinuxPlanet Stories

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.