June 23, 2018

Linux Commands for 99 Bottles of Beer and Disk Space

99 Bottles of Beer, df command

  • November 5, 2010
  • By Carla Schroder
Linux server admins need good scripting skills and command-line chops, but who says they can't be fun? Learn Bash Karaoke with 99 Bottles of Beer on the Wall, quickly find disk hogs, and display used/free disk space.

I haven't had time to finish setting up my Arch Linux backup server to do automatic wakeups, backups, and shutdowns, so here are some fun command-line hacks instead.

99 Bottles of Beer on the Wall

How about a bit of geek Karaoke? Just copy and paste this into a Bash session, and it will look like this:

$ x="bottles of beer";y="on the wall";for b in {99..1};do echo "$b $x $y, $b $x. Take one down pass it around, $(($b-1)) $x $y"; sleep 3;done
99 bottles of beer on the wall, 99 bottles of beer. Take one down pass it around, 98 bottles of beer on the wall
98 bottles of beer on the wall, 98 bottles of beer. Take one down pass it around, 97 bottles of beer on the wall
97 bottles of beer on the wall, 97 bottles of beer. Take one down pass it around, 96 bottles of beer on the wall

And so on, until it finishes. You can adjust the tempo by changing the sleep value, which in this example is 3 seconds. This can also be preserved in a script. Copy it into any text editor and save it under whatever name you like, like beer.txt. You have probably read that you need to enter #!/bin/bash as the first line, and then make the script executable with the command chmod +x beer.txt. Then run the script like this:

$ ./beer.txt

You need the dot-slash when you're running the script from the current working directory. Don't use them if you're running it from a different directory, like this:

$ myscripts/beer.txt

But that's a lot of work when you're just fooling around, or testing some new stuff. Skip the #!/bin/bash line, skip the chmod stuff, skip the dot-slash, and run your nice new script like this:

$ sh beer.txt

You could even name it plain old "beer" and not have a file extension at all, because Linux is smart and reads inodes instead of file extensions, so it cannot be fooled by bogus file extensions like poor old dumb Windows.

This is a great little script for learning a number of fundamental Bash operations, which I leave as your homework.

Disk Space and File Sizes

Graphical file managers like Dolphin and Nautilus are ghastly slow at calculating disk space, and never display the information I want all at once. I'm old and I don't have time to waste while they faff around, so I use the df command:
$ df -h | egrep -i "file|^/"
Filesystem   Size  Used Avail Use% Mounted on
/dev/sdb1    19G  3.2G   15G  19% /
/dev/sdb2    54G  4.4G   47G   9% /home
/dev/sda1   193G  118G   66G  65% /home/carla/sda1
The -h option means use human-readable numbers instead of bytes, like 3.2G instead of 3322416. Piping the output to the egrep incantation displays only your root and data filesystems, and not all the weird little temp and virtual filesystems, and it retains the header that shows what each column represents. Add the T option to see the filesystem types:
$ df -hT | egrep -i "file|^/"
Filesystem    Type    Size  Used Avail Use% Mounted on
/dev/sdb1     ext3     19G  3.2G   15G  19% /
/dev/sdb2     ext3     54G  4.4G   47G   9% /home
/dev/sda1     ext4    193G  118G   66G  65% /home/carla/sda1
df shows only filesystems, not individual directories or files. For these we want du.

Most Popular LinuxPlanet Stories

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