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?

In this entry we will see some commands that will allow us to see when the directories and files occupied (the size), which are the heaviest directories, which are the ones that weigh more than one Gb, among others; for this we use the du command.

du Command Options [options] [file]

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

-sWith this option we indicate that it only takes into account the specified files (* for all files).
-cWe can use this option to show us the total space consumed.
-hThis option is used to improve the readability of the data presented by adding the size of the files in kb, mb, gb...

Common Examples of Using the du Command

Briefly explaining your options, let's see some examples where we use these options:

To see the size of 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
4	Público
750560	sts-bundle
4	Vídeos
452280	workspace

Although the above output the numerical section that corresponds to the size is somewhat cumbersome to understand, to correct this and put 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

Using multiple commands

How should you know if you use Linux, to send the information from one command to another we use pipe (|) to use other commands; For example, if you are only interested in files and directories that are larger 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 (ordered):

[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 largest 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 together with other commands and their main options, those that I consider the most useful depending on the need, but there are many more combinations where it seems that imagination is the limit...

Andrés Cruz

Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz In Udemy

I agree to receive announcements of interest about this Blog.

!Courses from!

10$

On Udemy

There are 3d 17:23!


Udemy

!Courses from!

4$

In Academy

View courses

!Books from!

1$

See the books
¡Become an affiliate on Gumroad!