Terminal 101: Managing Disk Space
Posted 08/27/2012 at 12:40pm
| by Cory Bohon
Every Monday, we'll show you how to do something new and simple with Apple's built-in command line application. You don't need any fancy software, or a knowledge of coding to do any of these. All you need is a keyboard to type 'em out!
Whether you've got limited space because you're running OS X on a tiny SSD, or you're just conscious about getting the most out of every byte you've paid for, you can use two Terminal commands to help you keep track of disk space usage. We’ll show you how to track down large files eating away at your disk space using these built-in tools.
df

The df command gives you an overall view of the drives and volumes on your Mac and how much storage is currently being occupied. Just typing "df" into the terminal and pressing enter will give you a readout that lists the block sizes, the amount used, available, capacity, and some more statistics.

If you use the "-h" flag and type in the following command, however, you will get a nicer, human readable format that includes the size, used, and available capacities in bytes (Bi), kilobytes (Ki), megabytes (M) and gigabytes (Gi):
df -h
du
The du command lets you easily view the disk usage information on a file or directory basis. Try this:
du -sh ~
This command will calculate the amount of disk space currently being occupied by your home directory. The "-sh" flags tell the command to summarize the output, and present it in a human readable output. You’ll get an output that tells you the amount of space being used in gigabytes (G), megabytes (M) kilobytes (k), or bytes (b) at the front of the outputted line.

Replace the tilde (~) with the location of any directory or file to find out how much space the file or directory is currently occupying on your drive. Note that the command may take a few minutes to load and present output if there are a lot of files and subdirectories in the directory that you are testing.

You can also use the wildcard specifier (*) to cover all of the files and folders in the current working directory in the terminal. Navigate to the directory you’d like to test the disk usage of, and type the following command:
du -sh *
This command will output a listing of all of the files and subdirectories in the current folder, along with the space being occupied by each. This command is helpful when tracking down large files that are eating away space on your Mac.
Cory Bohon is a freelance technology writer, indie Mac and iOS developer, and amateur photographer. Follow this article's author, Cory Bohon on Twitter.