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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Try doing this :
du -s dir
or
du -sh dir
needs -h support, depends of your OS.
See
man du
Method 2
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.
Method 3
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
Method 4
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
Method 5
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
Method 6
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).
Method 7
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
Method 8
I like the following approach:
du -schx .[!.]* * | sort -h
where:
s: display only a total for each argumentc: produce a grand totalh: print sizes in a human-readable formatx: 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)
Method 9
You can use “file-size.sh” from the awk Velour library:
ls -ARgo "[email protected]" | awk '{q += $3} END {print q}'
Method 10
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>
Method 11
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
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0