221

I know you are able to see the byte size of a file when you do a long listing with ll or ls -l. But I want to know how much storage is in a directory including the files within that directory and the subdirectories within there, etc. I don't want the number of files, but instead the amount of storage those files take up.

So I want to know how much storage is in a certain directory recursively? I'm guessing, if there is a command, that it would be in bytes.

0

11 Answers 11

250

Try doing this :

du -s dir

or

du -sh dir

needs -h support, depends of your OS.

See

man du
5
  • 4
    add -b to output in bytes
    – pcnate
    Mar 17, 2016 at 21:47
  • 4
    --all list all files and -h shows file size in human readable format du -h --all
    – Praneeth
    May 10, 2017 at 1:29
  • I get a lot of cannot read directory, permission denied is it safe to use sudo du?
    – Shayan
    Aug 16, 2020 at 14:33
  • 1
    To get size of all directories, use du -sh *. Aug 27, 2020 at 18:00
  • Is it correct to assume this will sum up all mounted partitions on any folder which is a subdirectory? Say you du -s /mount, will this give the total of all mounted files in said directory? What about sshfs mounts, in the same way?
    – mazunki
    Mar 23, 2021 at 12:01
46

You just do:

du -sh /path/to/directory

where -s is for summary and -h for human readable (non standard option).

Be careful however, (unlike ls) this will not show you file size but disk usage (i.e. a multiple of the filesystem block-size). The file itself may actually be smaller, or even bigger.

So to get the files size, you can use the --apparent-size option:

du -sh --apparent-size /path/to/directory

This is the size that would be transferred over the network if you had to.

Indeed, the file may have "holes" in it (empty shell), may be smaller than the filesystem block-size, may be compressed at the filesystem level, etc. The man page explains this.

As Nicklas points out, you may also use the ncdu disk usage analyser. Launched from within a directory it will show you what folders and files use disk space by ordering them biggest to smallest.

You can see this question as well.

4
  • I get a lot of cannot read directory, permission denied is it safe to use sudo du?
    – Shayan
    Aug 16, 2020 at 14:33
  • 1
    @Shayan it is not dangerous, but will not give you the information about storage. Is that what you mean by "safe"?
    – Totor
    Oct 16, 2020 at 2:07
  • I was scared sudo du might change the ownership of everything to root or something else unexpected. @Totor
    – Shayan
    Oct 16, 2020 at 6:20
  • 1
    @Shayan no, it is purely a "read only" tool. No risk to modify data nor metadata here. :)
    – Totor
    Oct 17, 2020 at 11:36
42

Note that if you want to know all {sub}folders size inside a directory, you can also use the -dor --max-depth option of du (which take an argument: the recursive limit)

For instance :

du -h /path/to/directory -d 1

Will show you something like

4.0K /path/to/directory/folder1
16M  /path/to/directory/folder2
2.4G /path/to/directory/folder3
68M  /path/to/directory/folder4
8G   /path/to/directory/folder5

PS: Entering 0 as the recursive limit is equivalent to the -s option. Those 2 commands will give you the same result (your given directory recursive human readable size):

du -h /path/to/directory -d 0
du -sh /path/to/directory
2
  • 1
    -d 1 needs to be before directory path Oct 4, 2018 at 9:17
  • 3
    Needs? It works for me after the directory path, on ubuntu/debian. It is nicer though to have it before, I agree
    – Flo Schild
    Oct 4, 2018 at 21:53
22

This will give you a list of sizes from current directory, including folders(recursive) and files.

$ du -hs *
7.5M    Applications
9.7M    Desktop
 85M    Documents
 16K    Downloads
 12G    Google Drive
 52G    Library
342M    Movies
8.3M    Music
780M    Pictures
8.5G    Projects
8.0K    Public
 16K    client1.txt
1
  • 8
    to order by size: du -hs * | sort -hs
    – Kaiser
    Sep 3, 2018 at 13:16
19

An alternative to the already mentioned du command would be ncdu which is a nice disk usage analyzer for use in terminal. You may need to install it first, but it is available in most of the package repositories.

Edit: For the output format see these screenshots http://dev.yorhel.nl/ncdu/scr

1
  • Miracle! With this I can see which folders that is holding high volume of disk size storage. I even also found all of my files that were mysteriously disappeared. Great tool it is.
    – Faron
    Sep 16, 2015 at 1:53
4

In Unix, a directory just contains names and references to filesystem objects (inodes, which can refer to directories, files, or some other exotic things). A file can appear under several names in the same directory, or be listed in several directories. So "space used by the directory and the files inside" really makes no sense, as the files aren't "inside".

That said, the command du(1) lists the space used by a directory and all what is reachable through it, du -s gives a summary, with -h some implementations like GNU du give "human readable" output (i.e., kilobyte, megabyte).

3

For me it worked backwards in the case of the depth and the path on a OS X El Capitán

du -h -d 1 /path/to/directory
3

I like the following approach:

du -schx .[!.]* * | sort -h

where:

  • s: display only a total for each argument
  • c: produce a grand total
  • h: print sizes in a human-readable format
  • x: skip directories on different file systems
  • .[!.]* *: Summarize disk usage of each file, recursively for directories (including "hidden" ones)
  • | sort -h: Sort based on human-readable numbers (e.g., 2K 1G)
2

You can use "file-size.sh" from the awk Velour library:

ls -ARgo "$@" | awk '{q += $3} END {print q}'
1

This works:

To get the size of each directory under current directory.

du -h --max-depth=1 .

In general:

du -h --max-depth=1 <dirpath>
0

This is the best for me:

find . -type d -exec du -sk \"{}\" \;

You will get all the dirs recursively with at the top the root dir size:

588591456   ./photo
2171676 ./photo/2004
163916  ./photo/2004/AAA
114252  ./photo/2004/BBB
49660   ./photo/2004/CCC
7238148 ./photo/2005
184 ./photo/2005/.thumbcache
33592   ./photo/2005/AAA
228 ./photo/2005/BBB

                                                                       

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.