Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

Du Command in Linux - DesignLinux

Oct 17 2020
designlinux 0 Comments
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.

Related

Tags: disk, du, terminal

How to Install Odoo 14 on Ubuntu 20.04

Prev Post

Install Lubuntu 20.04 – A Lightweight Linux Desktop Environment

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 0
  • 65
  • 1,054,837

DesignLinux.com © All rights reserved

Go to mobile version