• Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate
TecNStuff
Menu
  • Home
  • Linux
  • Ubuntu
  • Debian
  • CentOS
  • Linux Commands
  • About Us
  • Donate

Du Command in Linux

Written by Admin, Updated On November 21, 2020
disk, du, terminal
Du Command in Linux

The du command is a short form of “disk usage” is used to get details of used disk space by given files or directories. Generally, it is used to find out the large size files which occupies more disk space. In this article we will cover how to use the du command in Linux.

How to Use the du command#

Below is the basic syntax for the du command:

du [OPTIONS]... FILE...

If you will run the du command without giving FILE name it will display the disk usage of the current working directory. When pass a directory path as FILE, the du command displays the disk usage summary for each files and subdirectories.

du ~/Documents

You can also pass multiple files and directories to the du command as arguments:

du ~/Documents ~/Pictures ~/.zshrc

If the user have not proper permissions for a file or directory which passed in du command, it will show error like “du: cannot read directory”. You should run the command with sudo.

There are many options of du command, here most frequently used are discussed below:

Using -a option you can get the disk space usage for each file within the directory.

du -a ~/Documents

Generally, we need to get space occupied by given directory in a human-readable format. You can use the -h option for that.

For example, to get the total size of the /var/lib and all of its subdirectories, you would run the following command:

sudo du -h /var

We are using sudo because most of the files and directories inside the /var/lib directory are owned by the root user and are not readable by the regular users. The output will look something like this:

...
8.0K    /var/lib/cloud/sem
32K     /var/lib/cloud/data
388K    /var/lib/cloud
614M    /var/lib
4.0K    /var/crash
2.3G    /var
...

Use -s option to get the total size of the specified directory and not for subdirectories:

sudo du -sh /var
2.3G /var

The -c option tells du to report a grand total. This is useful when you want to get the combined size of two or more directories.

sudo du -csh /var/log /var/lib
948M    /var/log
614M    /var/lib
1.6G    total

Use the --max-depth option to display the disk usage of the n-level subdirectories.

For example, to get a report about the first-level directories you would use:

sudo du -h --max-depth=1 /var/lib
36M     /var/lib/dpkg
8.0K    /var/lib/digitalocean
8.0K    /var/lib/logrotate
4.0K    /var/lib/misc
4.0K    /var/lib/man-db
388K    /var/lib/cloud
620M    /var/lib

Using du with Other Commands#

You can use the du command with the combination of the other commands using pipes.

For example, we will find the 3 largest directories inside the /var directory. For that we need to pass the output of du to the sort command for sorting the directories by their size then pipe the output to the head command to print only the 3 directories:

sudo du -h /var/ | sort -rh | head -3
2.3G    /var/
946M    /var/log
921M    /var/log/journal

Conclusion#

You learned how to use the du command in Linux. The du command gives you estimate of disk space used by given file or directories while df command show the information about the disk usage of the mounted file systems.

If you have any questions or feedback, please leave a comment below.

If our content helps you, please consider buying us a coffee

Thank you for your support.

Share On
Share on Facebook
Share on Twitter
Share on Reddit
Share on Tumblr
 Previous Article Bash For Loop
Next Article   How to Start, Stop, or Restart Nginx

Related Posts

  • How to Install SSH Keys on Ubuntu 22.04

    How to Set up SSH Keys on Ubuntu 22.04

    January 7, 2023
  • How to Install Fail2ban on Ubuntu 22.04

    How to Install and Configure Fail2ban on Ubuntu 22.04

    December 5, 2022
  • How to Enable SSH on Ubuntu 22.04

    How to Enable SSH on Ubuntu 22.04

    December 1, 2022

Leave a Reply Cancel reply

DigitalOcean Referral Badge

Popular Posts

  • How to Install Microsoft Edge Browser on Ubuntu 22.04
    How to Install Microsoft Edge Browser on Ubuntu 22.04 March 14, 2023
  • How to Install Ruby on Ubuntu 22.04 LTS
    How to Install Ruby on Ubuntu 22.04 LTS February 27, 2023
  • How to Install LEMP Stack on Ubuntu 22.04
    How to Install LEMP Stack on Ubuntu 22.04 March 18, 2023
  • How to Install Set Up Apache Virtual Hosts on Ubuntu 22.04
    How to Set Up Apache Virtual Hosts on Ubuntu 22.04 March 2, 2023
  • How to Install MariaDB on Debian 11 Bullseye
    How to Install MariaDB on Debian 11 Bullseye March 8, 2023
© 2020 TecNStuff All rights reserved. This website is using and storing cookies on your browser. By using this website you agree our Privacy Policy.  Follow us -  Twitter | Facebook