How to Get the Size of a Directory from Command Line

Mar 9, 2017 - 7 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.

.

Related articles:

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

7 Comments

» Comments RSS Feed

  1. Michael A says:

    Is there any way to do this in recovery mode? When I tried it says command not found

  2. Awesome_Unix_Guy says:

    I don’t use Macintosh, so I could be wrong, but if the MacOS version of du is the same as GNU du, it might be best to add a “-b” switch to the command. Technically, du reports the number of blocks, *not* bytes. That said, the size of a block can be specified as any arbitrary number (see below the “-B” (uppercase) and “–blocksize” options below), but I think it’s 512 bytes by default.

    The “-b” switch changes the block size to 1 byte, effectively making “blocks” and “bytes” the same. If the MacOS version of du doesn’t have a “-b” option, try “-B 1” (do take note that the “B” is capitalized, as Un*x systems – including MacOS – use case-sensitive command-line options), or “–block-size=1”.

    To learn more about du (including everything you need to know to fact-check this potentially-inaccurate post) type “man du” into a terminal, and read on. The man command will probably be able to scroll using arrow keys or vi-like keyboard shortcuts (probably both).

  3. 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.

  4. 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!

  5. 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 /”. :-)

  6. 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.

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