Terminus
How to Check the Size of Folders in Linux

How to Check the Size of Folders in Linux

[#output-folder-size] The short answer [#output-folder-size]

On Unix-like operating systems such as Linux and macOS, to output the size of a folder in a human-readable format, you can use the [.inline-code]du[.inline-code] command (which stands for "disk usage") as follows:

$ du -sh <path>

Where:

  • The [.inline-code]-s[.inline-code] flag is used to specify a directory depth of [.inline-code]0[.inline-code] (i.e. equivalent to [.inline-code]-d 0[.inline-code]).
  • The [.inline-code]-h[.inline-code] flag is used to display the size in a human-readable format using automatic unit suffixes, such as [.inline-code]K[.inline-code] for Kilobytes, [.inline-code]M[.inline-code] for Megabytes, and so on.
  • The [.inline-code]path[.inline-code] argument is the relative or absolute path to the folder.

For example:

$ du -sh ~/server
632M   /home/johndoe/server

[#output-subfolder-size] Outputting the size of subdirectories [#output-subfolder-size]

To also output the size of subdirectories, you can specify the depth using the [.inline-code]-d[.inline-code] flag as follows:

$ du -h -d <depth> <path>

Where:

  • The [.inline-code]depth[.inline-code] argument is the number of subdirectories the [.inline-code]du[.inline-code] command will traverse down.

For example:

$ du -h -d 1 ~/server
237M   /home/johndoe/server/node_modules
325M   /home/johndoe/server/.next
 32M    /home/johndoe/server/public
 35M    /home/johndoe/server/.git
2.0M    /home/johndoe/server/src
632M   /home/johndoe/server

[#easily-recall-commands-with-ai] Easily retrieve these commands using Warp's AI Command Suggestions feature [#easily-recall-commands-with-ai]

If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Suggestions feature:

Entering [.inline-code]check folder size[.inline-code] in the AI Command Suggestions will prompt a [.inline-code]du[.inline-code] command that can then quickly be inserted into your shell by doing [.inline-code]CMD+ENTER[.inline-code].