×

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

echo Command in Linux/Unix with Examples

The "echo" command in Linux is used to display a line of text on the terminal. It can also be used to print the value of a variable or to...

3 minutes 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.

chown Command in Linux/Unix with Examples

The 'chown' command in Linux is used to change the ownership of a file or directory. The command stands for "change owner" and it allows you to change the user...

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.

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.

mkdir Command in Linux/Unix with Examples

The "mkdir" command in Linux (and other Unix-like operating systems) is used to create new directories. The "mkdir" command stands for "make directory" and it is used to create a...

3 minutes read.

Rename Folder in Linux

Rename Folder in Linux In the Linux system, there is not any single dedicated command for renaming the files and directories. Mostly, the mv command is used extensively for the purpose...

3 minutes read.

top Command in Linux/Unix with Examples

The 'top' command in Linux is a real-time system monitoring tool that displays information about the running processes and system resource usage. The command provides a dynamic, scrolling view of...

4 minutes read.

Difference Between Unix and Linux

Unix vs Linux Unix and Linux both have been of great use in changing the world of operating systems. Unix was among the very few operating systems which were programmed for...

4 minutes read.

wget Command in Linux/Unix with Examples

The wget command is a command-line utility in Linux that is used to download files from the internet. It supports various protocols including HTTP, HTTPS, and FTP and can be...

5 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.

How to change the ApacheHTTP port in Linux

What is an Apache HTTP server in Linux? When we talk about the most commonly used web servers, one of the web servers that cannot be missed is Apache. With the...

4 minutes read.

cd Command in Linux/Unix with Examples

The 'cd' command in Linux is used to change the current working directory. The command stands for "change directory" and it is used to navigate the file system on a...

3 minutes read.

grep Command in Linux/Unix with Examples

The grep command in Linux is a command-line utility that searches for patterns in text files. It stands for "global regular expression print" and it is used to search for...

5 minutes read.

wc Command in Linux/Unix with Examples

The wc command in Linux is a command-line utility that is used to count the number of lines, words, and characters in a text file or standard input. The name...

4 minutes read.

ls Command in Linux/Unix with Examples

The ls command in Linux is a command-line utility that is used to list the files and directories in a directory. It is one of the most basic and commonly...

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 cd

Linux cd: As in GUI operating systems like Windows 10, we use the mouse to travel to various folders back and forth; similarly, we have the “cd” command in Linux...

4 minutes read.

Top 20 Linux Commands

1) clear command clear is a standard operating system command which is used to clear the terminal screen. This command brings the command line on the top of the terminal. By pressing the...

10 minutes read.

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...

4 minutes read.