×

du Command in Linux/Unix with Examples

The "du" command in Linux (and other Unix-like operating systems) is used to estimate the space used by a file or directory. The du command stands for "disk usage," and it is used to display the amount of disk space used by a file or directory, as well as the space used by its subdirectories. The du command is often used to determine which files or directories are taking up the most space on a system.

The basic syntax of the du command is:

du [options] [file or directory]

When executed without any options, the du command will display the amount of space used by the specified file or directory, as well as the space used by its subdirectories. For example, the following command will display the amount of space used by the directory "example":

du example

The most commonly used option for the du command is the "-h" option, which stands for "human-readable." This option causes the du command to display the space used in a more human-readable format, such as "1K" for 1 kilobyte, "1M" for 1 megabyte, etc. For example, the following command will display the amount of space used by the directory "example" in a human-readable format:

du -h example

Another useful option for the du command is the "-a" option, which stands for "all." This option causes the du command to display the space used by all files in a directory, including hidden files. For example, the following command will display the amount of space used by all files in the directory "example", including hidden files:

du -a example

The du command also has the option "-c" which can be used to display a total of the space used by all of the specified files and directories. This can be useful when trying to determine the total amount of space used by a group of files or directories. For example, the following command will display the total amount of space used by the directories "example1" and "example2":

du -c example1 example2

The du command can also be used with the option "-d" which allows you to specify the depth of the directory tree to be traversed. This can be useful when trying to limit the amount of information displayed by the du command. For example, the following command will display the space used by the top level files and directories in the "example" directory:

du -d 1 example

The du command can also be used with the option "-S" which can be used to exclude subdirectories from the disk usage calculation. This can be useful when trying to determine the disk usage of a specific file or directory without including the disk usage of its subdirectories.

du -S example

The du command can also be used with the option "-x" which can be used to limit the disk usage calculation to the file system that the specified file or directory resides on. This can be useful when trying to determine the disk usage of a specific file or directory on a specific file system.

du -x example

The "du" command is a very useful command for monitoring disk usage in Linux and other Unix-like operating systems. Here are some additional features and uses of the "du" command:

  • The "du" command can also be used with the "-b" option which can be used to display the disk usage in bytes. This can be useful when trying to determine the exact number of bytes used by a file or directory.
    • du -b example
  • The "du" command can also be used with the "-l" option which can be used to count the disk usage of symbolic links as the disk usage of the files they point to, rather than the disk usage of the links themselves.
    • du -l example
  • The "du" command can also be used with the "-L" option which can be used to count the disk usage of symbolic links as the disk usage of the links themselves, rather than the disk usage of the files they point to.
    • du -L example
  • The "du" command can also be used with the "-P" option which can be used to avoid traversing mount points. This can be useful when trying to determine the disk usage of a specific file or directory without including the disk usage of any mounted file systems.
    • du -P example
  • The "du" command can also be used with the "-r" option which can be used to include the disk usage of subdirectories and files that have been deleted but still have a file descriptor open.
    • du -r example

In summary, the "du" command is a versatile command that can be used in many ways to estimate the space used by a file or directory in Linux and other Unix-like operating systems. It can be used with various options like -b, -l, -L, -P, -r to handle different scenarios and display the disk usage in bytes, count the disk usage of symbolic links, avoid traversing mount points and include deleted files. The "du" command is an essential tool for system administrators, developers, and other users to monitor disk usage, identify large files or directories, and help keep disk usage in check.


Related Topics

Linux Tutorial for Beginners

What is Linux? Linux is an open source Operating System like Windows, Android, Mac OS, and iOS. An operating system is a software that enables communication between the user and the...

12 minutes read.

Linux Commands List

Linux Commands List The Linux systems are filled with commands for several use cases. We have divided these commands into some of the most common implementations for a better understanding of...

2 minutes read.

addgroup Command in Linux with Examples

What is the addgroup command? It is a command in Linux to create a new group using specified command lined and the system’s default values. How to add groups Adding a user group:...

4 minutes read.

Linux mkdir | Linux Create Directory

The Linux mkdir command is very much similar to Windows’s "new folder" feature. This command is used in Linux os to create new directories. The command mkdir stands for "make...

1 minute read.

curl Command in Linux/Unix with Examples

The 'curl' command in Linux is a command-line tool that allows you to transfer data from or to a server using various protocols, including HTTP, HTTPS, FTP, and many more....

3 minutes read.

netstat Command in Linux/Unix with Examples

The "netstat" command in Linux is a command-line utility that displays network connections, routing tables, and various network statistics. It is used to display the status of network connections, both...

3 minutes read.

Linux Commands with Examples

Linux Commands with Examples All the Linux commands are executed on the terminal provided by the Linux system. This terminal is a command-line interface to implement and display command outputs. Linux...

10 minutes read.

Linux rmdir command

The default use case of the rmdir command in the Linux system is to delete an empty directory. The below screenshot shows how to delete the empty directory. First, we...

1 minute read.

rm Command in Linux/Unix with Examples

The "rm" command in Linux (and other Unix-like operating systems) is used to remove files and directories. The "rm" command stands for "remove" and it is used to delete files...

4 minutes read.

sudo Command in Linux/Unix with Examples

The sudo command in Linux is used to run a command or set of commands with the privileges of another user, typically the root user. This allows users to perform...

3 minutes read.

tail Command in Linux/Unix with Examples

The "tail" command in Linux (and other Unix-like operating systems) is used to display the last few lines of a file. By default, the tail command will display the last...

4 minutes read.

Linux SCP Command

SCP stands for secure copy protocol. It is a command-line application for copying files and directories safely between two places. Command-line utilities are programs that operate on a computer's command...

3 minutes read.

Linux pwd

The pwd command is used to print or display the name of the working or current directory. Some shells have their own version of the pwd with several features, in...

1 minute read.

telnet Command in Linux/Unix with Examples

The telnet command in Linux is a command-line utility that allows you to connect to a remote host using the Telnet protocol. Telnet is a network protocol that provides a...

3 minutes read.

Linux Path Completion

Every operating system has some features which make them special. The Linux operating system primarily works on the command line interface, and we do have many features for this CLI....

1 minute read.

chmod Command in Linux/Unix with Examples

The chmod command in Linux is a command-line utility that is used to change the permissions of files and directories. It stands for "change mode" and it is used to...

4 minutes read.

mv Command in Linux/Unix with Examples

The 'mv' command in Linux is used to move or rename files and directories. The command stands for "move" and it allows you to move files and directories from one...

3 minutes read.

cut Command in Linux/Unix with Examples

The "cut" command in Linux (and other Unix-like operating systems) is used to remove sections from each line of a file or stream of data. The cut command is often...

3 minutes read.

tar Command in Linux/Unix with Examples

The 'tar' command in Linux is used to create, maintain, and extract files from archive files called tarballs. The command stands for "tape archive" and it allows you to combine...

4 minutes read.

sed Command in Linux/Unix with Examples

The 'sed' command in Linux is a powerful text processing tool that allows you to perform various operations on text files, such as searching, replacing, and manipulating text. The command...

3 minutes read.