Terminal in macOSTerminal 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 lineGet 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 lineSee 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.

Source

Follow Me:
Top Best Sellers!!