How to see the size of directories and files in Linux?

- 👤 Andrés Cruz

🇪🇸 En español

How to see the size of directories and files in Linux?

When working in Linux and you start running out of disk space, the question isn't whether something is taking up too much room, but which directory or file is responsible. In my case, more than once I've had to quickly locate huge folders, detect which ones exceeded 1 GB, or simply understand where the space was going.

In this guide, I show you how to see the size of directories and files in Linux, from the basics to practical combinations I use daily with du, so you can audit your system without wasting time.

In this post, we will look at some commands that allow us to see how much space (the size) directories and files take up, which are the heaviest directories, which ones weigh more than one Gb, among others; for this, we use the du command.

Why ls is not enough to know how much a folder occupies

The ls command is usually the first resource:

ls -lh

With the options:

  • -l: long format
  • -h: human-readable size (KB, MB, GB)

This works well for files, but with directories, it is misleading. The size shown by ls corresponds to the directory itself, not to the content it houses. Therefore, although it is useful for listing files, it is not useful for knowing how much a folder actually occupies.

That is where the correct command comes into play.

Options of the du command [options] [file]: the correct way to see directory sizes in Linux

Its basic syntax is:

du [options] [file or directory]

There are several options for this command which you can consult and study through the man du command, although among those I consider most important we have the following three:

-sWith this option, we indicate that it should only take into consideration the specified files (* for all files).
-cThis option can be used to show us the total space consumed.
-hThis option serves to improve the readability of the presented data by adding the file size in kb, mb, gb...

Common examples of using the du command

Briefly explained its options, let's see some examples where we use these options:

To see the size of the files listed in some directory (folder) and files:

[andres@localhost ~]$ du -s *
49180	androidemulator
604860	backup linux
19916	bucardo
4	bucardo.restart.reason.log
4	bucardo.restart.reason.txt
649532	Descargas
8	Desktop
52240	Documentos
90020	Documents
3722728	Dropbox
4	Escritorio
135196	glassfish-4.1
3852	Imágenes
32728	jre-oraclejava.rpm
4	Música
620488	netbeans-8.0.2
40	NetBeansProjects
25188	oo.war
52	pgadmin.log
4	Plantillas
4blico
750560	sts-bundle
4	Vídeos
452280	workspace

Although in the previous output the numeric section corresponding to the size is somewhat cumbersome to understand, to correct this and place the size of the files and directories in a more readable format, we use the following command:

[andres@localhost ~]$ du -sh *
49M	androidemulator
591M	backup linux
20M	bucardo
4,0K	bucardo.restart.reason.log
4,0K	bucardo.restart.reason.txt
635M	Descargas
8,0K	Desktop
52M	Documentos
88M	Documents
3,6G	Dropbox
4,0K	Escritorio
133M	glassfish-4.1
3,8M	Imágenes
32M	jre-oraclejava.rpm
4,0K	Música
606M	netbeans-8.0.2
40K	NetBeansProjects
25M	oo.war
52K	pgadmin.log
4,0K	Plantillas
4,0K	Público
733M	sts-bundle
4,0K	Vídeos
442M	workspace

Now, if we want to see, in addition to the previous information, the total space consumed or occupied:

[andres@localhost ~]$ du -csh *
49M	androidemulator
591M	backup linux
20M	bucardo
4,0K	bucardo.restart.reason.log
4,0K	bucardo.restart.reason.txt
635M	Descargas
8,0K	Desktop
52M	Documentos
88M	Documents
3,6G	Dropbox
4,0K	Escritorio
133M	glassfish-4.1
3,8M	Imágenes
32M	jre-oraclejava.rpm
4,0K	Música
606M	netbeans-8.0.2
40K	NetBeansProjects
25M	oo.war
52K	pgadmin.log
4,0K	Plantillas
4,0K	Público
733M	sts-bundle
4,0K	Vídeos
442M	workspace
6,9G	total

See the total size of a directory in readable format

If you only want to know how much an entire directory occupies, this is the most direct command:

du -sh directory

Real example:

du -sh Dropbox

Output:

3.6G    Dropbox

It is the command I usually use as a first step when something "smells" like excessive disk consumption.

Show the size of files and subdirectories

To see what is inside a directory and how much each thing occupies:

du -sh *

This shows files and folders with their total size, something much more useful than a flat list. In real systems, documents, logs, projects, downloads appear mixed... exactly what you need to analyze.

Show also the total occupied

When I want to know how much space everything occupies as a whole, I add -c:

du -csh *

At the end, you will get a line with the grand total, ideal for contrasting with the available space on the system.

Limit folder depth with du --max-depth

In large structures, showing everything can be excessive. To limit depth levels:

du -h --max-depth=1

This shows only the directories immediately below the current one, keeping the correct total. It is very useful when you have large trees and only want to identify which root folder is the problematic one.

Sort directories and files by size

This is where du really starts to shine when you combine it with other commands.

To sort from smallest to largest:

du -h | sort -h

And from largest to smallest (my most common case):

du -h | sort -h -r

When I am clearing space, I almost always start this way to directly attack the heaviest items.

See only directories that occupy more than 1 GB

If you are only interested in large folders, you can easily filter:

du -csh * | grep G

In my experience, this is ideal for detecting "forgotten" directories that have been growing out of control.

Show the heaviest files and folders (top N)

To see, for example, the 3 directories or files that occupy the most space:

du -csh * | sort -nr | head -3

This type of combination is what really makes the difference compared to basic tutorials, because it directly answers the question: what do I delete or move first?

Using multiple commands

As you should know if you use Linux, to send information from one command to another we use pipe (|) to employ other commands; for example, if you are only interested in files and directories that occupy more than 1 Gb:

[andres@localhost ~]$ du -csh * | grep G
3,6G	Dropbox
6,9G	total

If we wanted to see the files from the heaviest to the lightest (sorted):

[andres@localhost ~]$  du -cs * | sort -nr
8747536	total
3881532	Dropbox
2032968	Descargas
747856	sts-bundle
620488	netbeans-8.0.2
604860	backup linux
452280	workspace
135196	glassfish-4.1
89124	Documents
52240	Documentos
49180	androidemulator
32728	jre-oraclejava.rpm
25188	oo.war
19916	bucardo
3852	Imágenes
52	pgadmin.log
40	NetBeansProjects
8	Desktop
4	Vídeos
4	Público
4	Plantillas
4	Música
4	Escritorio
4	bucardo.restart.reason.txt
4	bucardo.restart.reason.log

If for example, we wanted to see the 3 heaviest files and folders:

[andres@localhost ~]$ du -csh * | sort -nr | head -3
733M	sts-bundle
635M	Descargas
606M	netbeans-8.0.2

These were some combinations using the du command in conjunction with other commands and its main options, those that I consider most useful depending on the need, but there are many more combinations where it seems the imagination is the limit…

Practical tips when analyzing disk usage in Linux

  • Some details I have learned with daily use:
  • du can take time on large systems or those with many small files.
  • In system directories, it may be necessary to use sudo.
  • du sizes reflect blocks used, not always the "logical" size.
  • Directories that weigh 4.0K are usually empty or only contain metadata.

Frequently Asked Questions

  • What command is used to see the size of a directory in Linux?
    • du, especially with -sh.
  • Why doesn't ls show the real size of a folder?
    • Because it only shows the size of the directory, not its content.
  • How to see the heaviest directories?
    • With du -h | sort -h -r.
  • Does du show the actual size or disk space?
    • It shows the space actually occupied on disk.

Conclusion

Knowing how to see the size of directories and files in Linux is key to maintaining a healthy system. Although ls can serve for a quick view, the real work is done with du and, above all, combining it with sort, grep, and head.

In my case, these combinations have saved me hours when diagnosing space problems on both desktops and servers. Once you internalize them, analyzing disk usage becomes fast and almost automatic.

I agree to receive announcements of interest about this Blog.

We will see some commands that will allow us to see when the directories and files occupy (how much they weigh), which are the heaviest directories, which are the ones that weigh more than one Gb, among others; for this we use the du command.

| 👤 Andrés Cruz

🇪🇸 En español