How to Get the Size of a Directory from Command Line

Mar 9, 2017 - 5 Comments

Terminal in macOS

Want to see the size of a directory from the command line? You may have noticed that using the traditional ls command to list the contents of a a directory won’t necessarily show the total size of a directory. Instead, to see what the disk usage is for a specific directory you’ll want to use the dedicated du command, which will display disk usage statistics for any path or directory specified. We’ll show you how to use this command.


This is obviously aimed at command line users, and the du command for retrieving the size of a directory will work the same on Mac OS, mac OS X, linux, and most other unix varieties. For casual Mac users, an easier way to get the size of a directory is through the Finder using the Get Info command on any specified folder.

Getting the Size of a Directory via Command Line

From the Terminal command line, issue the following syntax to see the size of a directory:

du -sh /directory/path

For example, to get the total size of the /Applications folder, you would issue the following command string:

du -sh /Applications/

You can use this to calculate the size of any specified directory and to see the size of containing directories and files as well.Get the size of a directory by command line

The -s flag will insure that each specific entry is calculated, and the -h flag will make the output in human readable size format (output of size will be shown kilobytes as KB and megabytes as MB, rather than bytes). We have discussed the disk usage commands here as well for du and the separate df command.

How to View the Size of All Directory Contents by Command Line

If you want to see the size of the current directory contents from the command line, including any containing folders and files, the du -sh command with a star wildcard, like so:

du -sh *

This will show the size of everything in the current directory, including the total size of folders and the total size of individual files, in a long list format.

See size of directory contents command line

You can also use the wildcard with other directory paths if desired, for example if you wanted to see the size of a users Desktop folder and all contents, the command would be:

du -sh /Users/NAME/Desktop/*

Keep in mind that du is calculating the total file size of each directory, its contents, and individual files, and so depending on what your target directory is it may take a while to report the contents size back to you. Obviously the faster a computer, the faster this processing will take place.

It’s also worth noting that if you only want to get the size of a particular file within a directory rather than the entire directory, you can use the ls -l command for that specific file instead.

Know of another helpful trick to retrieve the size of directories from the command line? Let us know in the comments, and if you enjoyed this article you’d likely find our other command line material to be interesting as well.

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

Related articles:

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

5 Comments

» Comments RSS Feed

  1. Joss says:

    Better to install the coreutils; it’s du comes with a lot more options, e.g. output of the actual size in bytes (which is kinda important!), inode info, apparent size etc. The macOS version is very rudimentary in comparison.

  2. Jan Steinman says:

    Keep in mind that “du -s” is expensive! It does a recursive directory traversal.

    One sure way to book yourself a coffee break is “du -s /”. :-)

  3. Walter says:

    tried the command you noted du -sh /Applications/ in macOS Sierra however terminal going thru each file in my Applications folder one at a time showing access denied.

    After about 10 minutes i simply aborted the process.

    No doubt i did something wrong.

    Would appreciate comments/help please.

    • Stephen says:

      Walter, if anything, you might want to try:

      sudo du -sch /Applications

      If you’ve got an SSD, that should be relatively fast…for an SATA, well, a fair bit longer…

      Give it a go, good luck, bro!

  4. Rex Rollman says:

    When I am in a directory and want to know it’s size, I use the following command:

    du -h -d 0

    The -d 0 limits the depth and gives me an overall total of the directory I am in.

Leave a Reply

 

Shop for Apple & Mac Deals on Amazon.com

Subscribe to OSXDaily

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

Recent Posts