Get the real file or directory size in unix or linux

Size of DirectoriesAll BASH users (Linux, Unix, OSX, etc) use the ls command, but when we want to know how much disk space has been used, the ls command just doesn’t cut it sometimes. While it is a useful command for listing information about files in a directory, or the directory structure, it doesn’t give you the overall space that a directory uses – including the files inside of it. Sometimes you need a lot more information and the commands to do it are not commonly known. Here’s a typical output of the ls command:

user@localhost:~/Pictures$ ls -lh

 total 644M
 -rw------- 1 user user 993K Jul 12 15:08 IMAG0142.jpg
 -rw------- 1 user user 790K Jul 12 15:08 IMAG0143.jpg
 -rw------- 1 user user 1.1M Jul 12 15:08 IMAG0144.jpg
 -rw------- 1 user user 1.3M Jul 12 15:08 IMAG0145.jpg
 -rw------- 1 user user 1.1M Jul 12 15:08 IMAG0146.jpg
 -rw------- 1 user user 1.1M Jul 12 15:08 IMAG0147.jpg

Notice how the output shows the sizes of the files. However, if we cd .. and look at the directory itself, it shows this:

drwxr-xr-x 2 user user 24K Jul 21 12:02 Pictures

Although it is clearly stated above while in the Pictures directory that the content takes up 644 Megabytes of space, listing the directory itself only shows that it is 24 Kilobytes. That’s a little misleading, don’t you think?

In order to get around this issue, there is a different command that will do the trick; the du command.

user@localhost:~/Pictures$ du -sh
 686M .

The command as typed above shows the combined size of all directories in the present working directory. However, if you were to add a directory name to the command, you would have this output:

user@localhost:~/$ du -sh Pictures
 644M Cell

If you were to use a * instead of a directory name, you would retrieve the results of all of the directories in the current directory:

user@localhost:~/$ du -sh *
 644M Pictures
 2.6M Videos
 4.5M Wallpapers

Comments are closed.

Post Navigation