Seeing a Folder’s Size in the Terminal

Question:

Mac Geek Gab listener Daryn writes, “I recently have been trying to figure out if there’s a way to see the size of a directory when using Terminal in Recovery Mode. I would like to know how much space I may need when trying to pull data from a volume on a Mac to an external device.

I occasionally have to backup data from Terminal in Recovery Mode due to filesystem damage or other issues. I’m wondering if you guys know of any way to see the size of a whole directory while in the Terminal?”

Answer:

Absolutely, Daryn!

The command you’ll want to use from the Terminal is ‘du‘ which is short for, “display disk usage statistics”. Just invoking ‘du‘ will give you too detailed of a list (and not the answer you want), but with a couple of switches and a filename target you’ll get what you’re looking for. From the Terminal, type: du -sh * and that will give you a listing of your files and folders with their sizes.

By issuing 'du -sh *' in the Terminal I can see the sizes of all my files and folders
By issuing ‘du -sh *’ in the Terminal I can see the sizes of all my files and folders

So you understand what you just typed (or are about to type):

  • du – that’s the command
  • -s – this switch tells ‘du’ to show you one line per filename specified
  • -h – this switch tells ‘du’ to output the sizes in ‘human-readable’ format. Because we’re using two switches, s and h, we can combine them together with ‘-sh
  • * – this is the filename target, and the asterisk means “everything that doesn’t start with a period.” You could specify a single directory here or a list, or individual files. du will take them all.

For anyone doing this from the Terminal of a normally-booted Mac, this will work exactly as you see above. Unfortunately Recover Mode is a little different because the ‘du’ command isn’t included in the skeleton system you’re running. You’ll have to point to a different volume that contains it. Thankfully, your Mac’s normal hard drive has this, and it will work even if it’s in read-only mode. You’d modify the above command and add “/Volumes/[Volume Name]/usr/bin/” to the beginning of the statement, where [Volume Name] is the name of your hard drive. If your hard drive name contains a space, it’s safer to wrap the whole thing in double quotes as I have just to make life easier.

My hard drive is named “Back Seat Betty” because I name all my drives after Miles Davis songs, so for me that command would be:

“/Volumes/Back Seat Betty/usr/bin/du” -sh *

The Terminal can be both handy and efficient, not to mention fun. Tread thoughtfully and deliberately, and always remember to use the ‘man‘ command to get details if you’re unsure. For today’s lesson, typing ‘man du‘ will give you more information about the ‘du’ command than you ever wanted. Enjoy!

This question was originally answered on MGG 618: Quick Tips, iCloud Music and Resistant Clients

About MGG Answers:

Each week Dave Hamilton and John F. Braun provide some great troubleshooting advice to listeners of the Mac Geek Gab podcast. Here with MGG Answers we share some of those tips with the rest of the world!

7 thoughts on “Seeing a Folder’s Size in the Terminal

      1. It’s ok I have figured out a way. I had to mount the filesystem, then cd to “/Volumes/Macintosh HD/usr/bin”. Even when in the bin directory just typing du and pressing enter does not work. It has to be ./du
        I was using recovery mode and a the system was halfway through a macOS upgrade if that has any relevance

      2. Yep, that makes sense. In recovery mode you might not have /usr/bin in your default path, especially if there was an installation failure.

  • By the way, those quotes are really important in the “Back Street Betty” example.

    At the command-line (using bash in this case), spaces are delimiters. So if there’s a space in the name, you have to wrap it in quotes, or escape the spaces.

    So this will work just as well …
    /Volumes/Back\ Seat\ Betty/usr/bin/du -sh *

    The backslash can also be used to escape a carriage-return when you’re entering a really, really, really long line. Just end the line with a backlash and have the carriage-return follow it immediately (with NO intervening space). Do this on each of the lines to be continued.

  • There are also files that start with ‘.’. As these are hidden, it’s a good idea to look at them, too. You can do that with: du -sh .[^.]*

    This is the same command, only the file list the shell creates to process includes only those that start with ‘.’ and excludes “..”. The latter is a link to the parent directory, which is unlikely what you want. The pattern means “select all files that start with ‘.’ followed by something other than another ‘.’. This is useful to make sure that something isn’t hiding.

    Use ‘ls -a’ to see all the files in the directory to make sure there isn’t a file “…” that’s trying to hide.

  • Actually, the -s option is to tell du to print a summary of the file size if the file specified is a directory. By default, du will traverse directories and give you a size of each file. By specifying the -s, it will only tell you the size, or disk usage, of the directory.

    Try it: du Documents
    Versus: du -s Documents

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.