Command Line Disk Usage Utilities: df and du

Mar 20, 2007 - 7 Comments

Terminal in OS X Getting disk usage information on a Mac is often gathered by selecting a file, directory, or hard drive and hitting Command-I for Get Info, then appears a nice GUI interface with extended information about the selected object including disk usage. Command-I isn’t the only way to get disk info however, with the command line there are two useful utilities to gather this data that you should know; df and du. The following is a brief explanation of each command and how you can use them:

df – displays disk usage information based on file system (ie: entire drives, attached media, etc)

At the command prompt, type:
df -h
The -h flag is for ‘human readable form’ meaning return results in the familiar megabyte/gigabyte format. You should see something like this:

$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/disk0s2 74G 52G 22G 70% /

In this case, /dev/disk0s2 is the main hard disk, and 70% of it is in use.

du – displays disk usage information for each file and directory (ie: home directories, folders, etc)

At the command prompt type:
du -sh ~
The -s flag is for a summary, and once again the -h flag is for ‘human readable form’, the ~ is your home directory. You should see something like this:
$ du -sh ~
26G /Users/MacUser

This users home directory takes up 26gb of space!

Another example, type du -sh * at the terminal. The * wildcard will cover all files in your home directory or whatever directory you are currently in, by default the Terminal will launch with your home directory as the pwd (present working directory).

$ du -sh *
32M Desktop
217M Documents
531M Downloads
12G Library
5.2G Movies
2.1G Music
1.5G Pictures
8.0k Public
36k Sites

As you can see, the * allows for a breakdown of the space taken up by which directory. This can be extremely useful when trying to clear up disk space if you don’t know what is hogging all the room.

.

Related articles:

Posted by: Paul Horowitz in Command Line, Mac OS, Tips & Tricks

7 Comments

» Comments RSS Feed

  1. jade says:

    I am looking to use the du -sh to see the total file size in cluded all subdirectories as grand total

  2. […] is the same as TopGear says, but maybe quicker: Command line disk usage utilities df and du Support requests by PM will remain unanswered, sorry.  GA-Z68X-UD3H-B3 i7-2600K; […]

  3. kurtis mcdonald says:

    i need to clear my mac’s info like games and the sort, but when I hit du, all the game info and whatnots also come out. How do I delete all that info PERMANANTLY?

  4. Bill says:

    You can also use ls -Sr

  5. Try

    du -ck * | sort -rn | head -11

  6. Darrell says:

    I never knew about du, good info to know. And thanks lar3ry for the extended info on du

  7. lar3ry says:

    The DU utility is good for finding out where your disk space is being wasted. Use something like:

    du -k * | sort -n

    This will show the usage (in 1-K units) of all the files/directories in the current directory, which will be piped to the “sort” utility, using a numeric comparison on the first field (the size). Thus, your largest files/directories will be the last ones listed.

    Note that while the -h option is useful for seeing “human readable” sizes, sort isn’t smart enough to realize that “256M” is larger than 816K. [smile]

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site