How do I get the size of a directory on the command line?
I tried to obtain the size of a directory (containing directories and sub directories) by using the ls
command with option l
. It seems to work for files (ls -l file name
), but if I try to get the size of a directory (for instance, ls -l /home
), I get only 4096 bytes, although altogether it is much bigger.
du -sh file_path
Explanation
du
(disc usage) command estimates file_path space usage-
The options
-sh
are (fromman du
):-s, --summarize display only a total for each argument -h, --human-readable print sizes in human readable format (e.g., 1K 234M 2G)
To check more than one directory and see the total, use
du -sch
:-c, --total produce a grand total
du
is your friend. If you just want to know the total size of a directory then jump into it and run:
du -hs
If you also would like to know which sub-folders take up how much disk space?! You could extend this command to:
du -h --max-depth=1 | sort -hr
which will give you the size of all sub-folders (level 1). The output will be sorted (largest folder on top).
The du
command shows the disk usage of the file.
The -h
option shows results in human-readable form (e.g., 4k, 5M, 3G).
du -h (file name)
Just use the du
command:
du -sh -- *
will give you the cumulative disk usage of all non-hidden directories, files etc in the current directory in human-readable format.
You can use the df
command to know the free space in the filesystem containing the directory:
df -h .
Others have mentioned du
, but I would also like to mention Ncdu — which is an ncurses version of du
and provides interactivity: You can explore the directory hierarchy directly and see the sizes of subdirectories.
sudo ls -1d */ | sudo xargs -I{} du {} -sh && sudo du -sh
personally I think this is best, if you don’t want to use ncdu
# du -sh ./*
df -h .; du -sh -- * | sort -hr
This shows how much disk space you have left on the current drive and then tells you how much every file/directory takes up. e.g.,
Filesystem Size Used Avail Use% Mounted on
/dev/sdb2 206G 167G 29G 86% /
115M node_modules
2.1M examples
68K src
4.0K webpack.config.js
4.0K README.md
4.0K package.json
All of the above examples will tell you the size of the data on disk (i.e. the amount of disk space a particular file is using, which is usually larger than the actual file size). There are some situations where these will not give you an accurate report, if the data is not actually stored on this particular disk and only inode references exist.
In your example, you have used ls -l on a single file, which will have returned the file’s actual size, NOT its size on disk.
If you want to know the actual file sizes, add the -b option to du.
du -csbh .
One more variant:
du -h --max-dep=1
In order to get the total size of files under a directory, you can select the type by find
- For files only:
find -type f -print0 | xargs -0 stat -c %s | awk '{total+=$1} END {printf("%.0fn",total)}'
- For everything but directories (files + symlinks + sockets + fifos + devices + …):
find -not -type d -print0 | xargs -0 stat -c %s | awk '{total+=$1} END {printf("%.0fn",total)}'
Why not use du
?
The du
command is easier but it will count all types of files, and you don’t have an option to change it. For example, assuming the current directory has a file, an empty dir and a symlink:
$ ls -AlF
total 8,192
-rw-r--r-- 1 nordic nordic 29 Mar 28 19:05 abc
drwxr-xr-x 2 nordic nordic 4,096 Mar 28 19:06 gogo/
lrwxrwxrwx 1 nordic nordic 3 Mar 28 19:06 s_gogo -> abc
$ find -type f -print0 | xargs -0 stat -c %s | awk '{total+=$1} END {printf("%.0fn",total)}'
29
$ du -sb
8224 .
Here is a function for your .bash_aliases
# du with mount exclude and sort
function dusort () {
DIR=$(echo $1 | sed 's#/$##')
du -scxh $(mount | awk '{print $3}' | sort | uniq
| sed 's#/# -- exclude=/#') $DIR/* | sort -h
}
sample output:
$ dusort /
...
0 /mnt
0 /sbin
0 /srv
4,0K /tmp
728K /home
23M /etc
169M /boot
528M /root
1,4G /usr
3,3G /var
4,3G /opt
9,6G total
for subdirs:
$ dusort .
$ dusort /var/log/
You can use
du -sh directory/
and
du -sh filename
to know the space occupied by the folder or file.
df -h
will show the disk usage in human readable format -h does that.
There is also a gui based program called Disk Usage Analyzer
.
Here is a POSIX script that will work with:
- A file
- Files
- A directory
- Directories
ls -A -R -g -o "$@" | awk '{n1 += $3} END {print n1}'
Note that du
prints the space that a directory occupy on the media which is usually bigger than just the total size of all files in the directory, because du
takes into account the size of all auxiliary information that is stored on the media to organize the directory in compliance with file system format.
If the file system is compressible, then du
may output even smaller number than the total size of all files, because files may be internally compressed by the file system and so they take less space on the media than just uncompressed information they contain. Same if there are sparse files.
if there are hard links in the directory, then du
may print smaller value as well because several different files in the directory refer the same data on the media.
To get the straightforward total size of all files in the directory, the following one-line shell expression can be used (assuming a GNU system):
find . ! -type d -print0 | xargs -r0 stat -c %s | paste -sd+ - | bc
or even shorter:
find . ! -type d -printf '%sn' | paste -sd+ - | bc
It just sums sizes of all non-directory files in the directory (and its subdirectories recursively) one by one. Note that for symlinks, it reports the size of the symlink (not of the file the symlink points to).