How to display file and directory sizes in Linux and Unix

Displaying the size of a directory or file is an easy task in Linux and Unix. There are a few tools that do that: du, ls -lh, tree, etc.

The du command is the most used for displaying sizes.

How to display file and folder sizes with du:

The most used du commands are: du -h, du -h –max-depth=1 and du -sh

How to display a file’s or a folder’s size:
The -h option displays sizes in human readable format:

File size only:

$ du -h /etc/passwd
4.0K /etc/passwd

Dir size only:

$ du -sh ~
83M /home/razvan

If you use du -h /home, you will get the sizes of all the files and folders inside /home.
Use du –max-depth for more accurate output:

$ du -h --max-depth=1 /home/razvan/naboo/
4.0K /home/razvan/naboo/anakin
4.0K /home/razvan/naboo/jedi
8.0K /home/razvan/naboo/force
20K /home/razvan/naboo/

When I need to display sizes of directory structures, I use tree -sh /path/to/dir. Read more about tree here.

How to display sizes with ls -l:

For files: ls -lh
For dirs: ls -ldh

Read more about the ls command here.

Extra: use df -h to get info about the system usage:

$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 19G 3.1G 15G 18% /
udev 490M 4.0K 490M 1% /dev
tmpfs 200M 764K 199M 1% /run
none 5.0M 0 5.0M 0% /run/lock
none 498M 532K 498M 1% /run/shm
/dev/sr0 634M 634M 0 100% /media/Linux Mint 12 64-bit

Liked it? Take a second to support Geekster on Patreon!
Tagged with: , , , ,
Posted in The Linux and Unix Articles!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Support LinuxG via Patreon
Support LinuxG.net on Patreon!
Subscribe

  

Subscribe to get the latest Linux news and how to guides directly on your e-mail!