How to Check the Size of Folders in Linux
Razvan Ludosanu
Founder, learnbackend.dev
Updated: 7/11/2024
Published: 7/11/2024
The short answer
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 du command (which stands for "disk usage") as follows:
$ du -sh <path>
Run in Warp
Where:
- The -s flag is used to specify a directory depth of 0 (i.e. equivalent to -d 0).
- The -h flag is used to display the size in a human-readable format using automatic unit suffixes, such as K for Kilobytes, M for Megabytes, and so on.
- The path argument is the relative or absolute path to the folder.
For example:
$ du -sh ~/server
632M /home/johndoe/server
Run in Warp
Outputting the size of subdirectories
To also output the size of subdirectories, you can specify the depth using the -d flag as follows:
$ du -h -d <depth> <path>
Run in Warp
Where:
- The depth argument is the number of subdirectories the du 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
Run in Warp
Easily retrieve these commands using Warp's AI Command Suggestions feature
If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Suggestions feature:
Entering check folder size in the AI Command Suggestions will prompt a du command that can then quickly be inserted into your shell by doing CMD+ENTER.
Written by
Razvan Ludosanu
Founder, learnbackend.dev
Filed Under
Related Articles
List Open Ports in Linux
Learn how to output the list of open TCP and UDP ports in Linux, as well as their IP addresses and ports using the netstat command.
Count Files in Linux
Learn how to count files and folders contained in directories and subdirectories in Linux using the ls, find, and wc commands.
Linux Chmod Command
Understand how to use chmod to change the permissions of files and directories. See examples with various chmod options.
POST JSON Data With Curl
How to send valid HTTP POST requests with JSON data payloads using the curl command and how to avoid common syntax pitfalls. Also, how to solve the HTTP 405 error code.
Format Command Output In Linux
Learn how to filter and format the content of files and the output of commands in Linux using the awk command.
Create Groups In Linux
Learn how to manually and automatically create and list groups in Linux.
Switch Users In Linux
Learn how to switch between users, log in as another user, and execute commands as another user in Linux.
Remover Users in Linux
Learn how to remove local and remote user accounts and associated groups and files in Linux using the userdel and deluser commands.
Delete Files In Linux
Learn how to selectively delete files in Linux based on patterns and properties using the rm command.
Find Files In Linux
Learn how to find and filter files in Linux by owner, size, date, type and content using the find command.
Copy Files In Linux
Learn how to safely and recursively copy one or more files locally and remotely in Linux using the cp and scp command.
Create Files In Linux
Learn how to create regular files in Linux using command line interface commands like touch, echo, cat, printf, and in-terminal text editors like Vim.