How to get the size of a directory or file on Linux in human readable format (2023)

Learn a simple command for getting file sizes in human readable format on Linux.

Ralph J. Smit Laravel Software Engineer

Sometimes you need to deal with issues like debugging why the disk space of a server is suddenly filling up. To figure that out, it is very useful to check what size certain files or folders are. In this tutorial I'll show you two quick ways for letting Linux list the size of certain directories and files.

How to find the size of directories in Linux

First, we want to find directories that are very big. To do so, it makes sense to navigate to your home directory (cd ~). Then, we can run the following command to list the file size of all the folders in it, so that we can spot big folders:

du -h . --max-depth=1
 
20K ./.ssh
343M ./.npm
8.0K ./.aws
303M ./.cache
542M ./staging.example.com
3.0G ./app.example.com
20K ./.local
32K ./.config
641M ./staging.example.com-deploy
4.2G ./example.com
8.9G .

First of all, what does this command do?

  • The du is the base command
  • The -h flag tells the command to show file sizes in human readable formats (like KB, MB and GB)
  • The . tells the command that we want to start from the current directory
  • The --max-depth=1 tells the command that it should only list files and folders in the current directory. If omitted, it will list recursively check every directory and list every file it finds, which obviously isn't useful for us now.

Next, what does the result tell us?

First, the total folder size is 8.9GB. That is big, but not insane.

Next, there are several bigger directories, that each take up a few hundred MB, like the staging.example.com and the staging.example.com-deploy directory.

Finally, there are two big directories, the app.example.com and example.com directories. These directories we will need to investigate. You can cd into these directories, run the same command again to find big folders and so forth.

How to find file sizes in Linux

We can use the above command to find file sizes in Linux. However, there is a second way as well which only works for files. That way is by using the ls command.

ls -lsah .
 
4.0K drwxr-xr-x 15 user user 4.0K Feb 9 16:34 .
4.0K drwxr-xr-x 13 user user 4.0K Jan 5 10:01 ..
48K -rw-rw-r-- 1 user user 48K Feb 26 2022 .editorconfig
4.0K -rw-rw-r-- 1 user user 1.6K Jan 5 20:07 .env
4.0K -rw-rw-r-- 1 user user 1.2K Apr 11 2022 .env.example
4.0K drwxrwxr-x 8 user user 4.0K Feb 9 16:33 .git
4.0K -rw-rw-r-- 1 user user 111 Feb 24 2022 .gitattributes
4.0K -rw-rw-r-- 1 user user 437 Jan 23 19:17 .gitignore
4.0K -rw-rw-r-- 1 user user 194 Feb 24 2022 .styleci.yml
4.0K -rwxrwxr-x 1 user user 1.7K Feb 24 2022 artisan
4.0K drwxrwxr-x 3 user user 4.0K Feb 24 2022 bootstrap
4.0K -rw-rw-r-- 1 user user 151 Feb 25 2022 clear-compiled
4.0K -rw-rw-r-- 1 user user 3.6K Jan 25 17:56 composer.json
476K -rw-rw-r-- 1 user user 474K Feb 9 16:33 composer.lock
4.0K drwxrwxr-x 2 user user 4.0K Feb 9 16:33 config
4.0K drwxrwxr-x 4 user user 4.0K Nov 3 12:24 database
4.0K drwxrwxr-x 4 user user 4.0K Feb 24 2022 lang
...

I truncated the results a bit, but this is obviously a directory with a Laravel app in it. What does the ls -lsah command do?

  • The ls command lists all files and folders in the current directory
  • The -lsah consists of 4 separate flags:
  • The -l flag says to list the files in a neat ordered vertical list, so that more information can be shown
  • The -s flag tells the command to show the file sizes in human readable formats like KB, MB and GB
  • The -a flag tells the command to also list hidden files that start with a .
  • The -h flag is additional for the human readable sizes

As you might notice, this command works great for simple files. However, for folders it will always show 4.0KB. Not all folders are the same size, so why is that? The reason is that the 4.0KB represents the size it takes on the disk to store the meta-information about the folder itself, like the name of the folder and the position. That's why the 4.0KB is more or less the same for every folder, but it doesn't say anything about the contents.

Conclusion

I hope this was useful and that it will help you finding and debugging large files and folders on your server. 👌

Published by Ralph J. Smit on in Servers .