How to View Folder and File Sizes in Linux (du Command)?

- Andrés Cruz - ES En español

How to View Folder and File Sizes in Linux (du Command)?

When working in Linux and starting to run out of disk space, the question is not just whether storage is full, but which directory or file is responsible. In my daily experience managing servers and local environments, more than once I have had to quickly locate huge folders, detect which ones exceeded 1 GB, or simply understand what was consuming the space.

In this guide, you will learn how to view the size of directories and files in Linux directly from the terminal. We will analyze everything from the syntax of the du (Disk Usage) command to advanced combinations with sort, grep, and head to audit your system quickly and without wasting time.

Quick summary: If you need to know how to see the size of a folder in Linux or how large a file is in the console, the main command is du -sh /path. To sort directories from largest to smallest and detect the heaviest ones, the ideal combination is du -h --max-depth=1 | sort -hr.

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

If you are looking for the Linux command to display the contents of a folder or the file listing, the first resource that comes to mind is ls:

ls -lh

Where its parameters represent:

  • -l: Shows the listing in long, detailed format (permissions, owner, date).
  • -h: Expresses the size in a human-readable format (human-readable: KB, MB, GB).

This works perfectly for measuring individual files. However, to find out how large a folder is in Linux, ls proves misleading: it only shows the size of the directory entry itself in the file system (usually 4.0K or 4096 bytes), completely ignoring the content accumulated inside that folder.

For this reason, to find out the actual size of files and folders in the Linux console, we must use the du command.

Syntax and options of the du command: The correct way to view directory sizes in Linux

The du command (short for Disk Usage) is the standard tool in Unix/Linux systems to calculate the actual disk usage of any file or directory. Its basic syntax is:

du [options] [file_or_directory]

You can consult the official documentation by running man du in your terminal. The main options you will use on a day-to-day basis are:

Option / FlagsDescription and main usage
-s (--summarize)Displays only the total accumulated summary of the specified item, without detailing each internal subdirectory.
-h (--human-readable)Displays sizes in readable units (such as K, M, G).
-c (--total)Prints an additional row at the end with the total calculation consumed by all specified items.
--max-depth=NLimits analysis depth to N levels of subfolders, preventing massive data dumps on large screens.

Practical examples: How to know how large a folder or file is

Let's look at the most common scenarios when analyzing files and directories from the Linux console:

1. View the size in blocks of files and folders

When running du -s * inside your working directory, the command shows the size in standard 1 Kilobyte blocks (1024 bytes):

[andres@localhost ~]$ du -s *
49180	androidemulator
604860	backup linux
19916	bucardo
4	bucardo.restart.reason.log
649532	Descargas
52240	Documentos
3722728	Dropbox
135196	glassfish-4.1
3852	Imágenes
32728	jre-oraclejava.rpm
620488	netbeans-8.0.2
25188	oo.war
750560	sts-bundle
452280	workspace

2. View the size of folders and files in readable format (KB, MB, GB)

To transform those block numbers into understandable readings in Megabytes (M) or Gigabytes (G), we add the -h option (du -sh *):

[andres@localhost ~]$ du -sh *
49M	androidemulator
591M	backup linux
20M	bucardo
4,0K	bucardo.restart.reason.log
635M	Descargas
52M	Documentos
3,6G	Dropbox
133M	glassfish-4.1
3,8M	Imágenes
32M	jre-oraclejava.rpm
606M	netbeans-8.0.2
25M	oo.war
733M	sts-bundle
442M	workspace

3. View the overall accumulated total with -c

To get a grand total sum of all analyzed content at the end of the listing, we include the -c flag (du -csh *):

[andres@localhost ~]$ du -csh *
49M	androidemulator
591M	backup linux
20M	bucardo
635M	Descargas
3,6G	Dropbox
606M	netbeans-8.0.2
733M	sts-bundle
442M	workspace
6,9G	total

4. Check how large a specific folder is

To quickly check the size of a single folder in Linux (for example, the Dropbox folder), simply pass its name or path as an argument:

du -sh Dropbox

Estimated output:

3,6G    Dropbox

5. Limit inspection levels with --max-depth

When scanning large projects with thousands of subfolders (such as node_modules directories, vendor folders, or git repositories), you can restrict the level of exploration with:

du -h --max-depth=1

This will exclusively print first-level child folders without losing accuracy in the total calculation.

Advanced combinations: Sort and detect the largest directories

The Linux console shines when we combine the du command with other utilities via the pipe (|).

1. Sort directories from largest to smallest size

If your goal is to detect which folders are clogging up space, you can list and sort items from largest to smallest using sort -nr or sort -hr:

du -cs * | sort -nr

Sorted output in the terminal:

[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

If you are using readable values (-h), you can sort them correctly from largest to smallest using sort -hr:

du -h --max-depth=1 | sort -hr

2. Filter folders larger than 1 GB

To quickly filter and see only directories that exceed 1 GB of disk space, pipe grep to the output of du:

du -csh * | grep G

Obtained output:

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

3. Show the Top N largest files and folders

To immediately list the top 3 items consuming the most space on your system, append head -3 to the end of the command chain:

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

Obtained output:

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

Summary table (Cheat Sheet) of commands to view disk size in Linux

Saving this table will serve as a quick reference whenever you need to audit space in the terminal:

Search GoalRecommended Command
Find out the size of a specific folder or filedu -sh /path/to/directory
View the size of all current content and the totaldu -csh *
Sort folders from largest to smallest space (readable)du -h --max-depth=1 | sort -hr
List only directories larger than 1 GBdu -csh * | grep G
Get the top 5 largest files or foldersdu -csh * | sort -nr | head -5
View free space on disk partitionsdf -h
Visual and interactive browser in the consolencdu

Frequently Asked Questions and key concepts (FAQ)

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

Does Linux display sizes in GiB or GB?

Native Linux tools such as du -h and df -h calculate size by default in binary powers of 1024. This means that when you see 1G in the terminal, it represents 1 Gibibyte (GiB) (1024^3 bytes = 1,073,741,824 bytes) and not the standard decimal Gigabyte (GB = 1000^3 bytes). If you prefer to see the calculation in decimal base (1000), in GNU du you can use the --si flag.

What is the difference between the du and df commands?

Both commands are used to analyze storage in Linux, but for different purposes:

  • du (Disk Usage): Inspects and sums the size of specific folders or files by traversing their structure.
  • df -h (Disk Free): Examines the overall state of the file system, showing free and used space on complete disk partitions (such as /, /home, or external drives).

Why does an empty folder occupy 4.0K in Linux?

In standard Linux file systems such as ext4, directories are special files intended to index the names and inodes of their contents. The minimum block allocation on disk is usually 4096 bytes (4.0K), which explains why an empty folder will always mark at least 4.0K.

Is there an interactive console alternative to du?

Yes. If you prefer to navigate visually through folders and delete large files directly from the terminal, the best alternative is ncdu (NCurses Disk Usage). You just need to install it and invoke the ncdu command to explore the disk using navigation keys.

Practical tips when auditing disk space in Linux

  • Use superuser permissions on system paths: When checking directories like /var/log, /tmp, or /usr, prepend sudo (example: sudo du -sh /var/log/*) to avoid missing permission warnings.
  • Optimize query time: On drives with millions of small files, du's recursive reading can take time. Using --max-depth=1 drastically speeds up the process.
  • Deleted files retained in memory: If df -h indicates that your partition is at 100% but du cannot find large files, active processes may be holding file descriptors for files that have already been deleted.

Conclusion

Knowing how to view the size of directories and files in Linux is a key technical skill for keeping your system healthy. While the ls -lh command is handy for a quick file glance, deep analysis is achieved with the du command and its integration with sort, grep, and head.

Mastering these tools will allow you to identify and solve low-space issues in seconds, both on your local machine and on production servers.

Learn how to view the size of folders and files in the Linux console using the `du` command. Discover how to list the largest directories and free up space.


Únete a la comunidad de desarrolladores que han decidido dejar de picar código y empezar a construir productos reales. Recibe mis mejores trucos de arquitectura cada semana:

I agree to receive announcements of interest about this Blog.