×

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

addr2line Command in Linux with Examples

Use of addr2line Command The addr2line command is used to convert addresses to line numbers and file names. This command uses debugging info to check which line number and file name...

4 minutes read.

Linux Admin Tutorial

In publishing and graphic design, lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document without relying on meaningful content....

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

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.

df Command in Linux/Unix with Examples

The 'df' command in Linux is used to display the amount of disk space available on the file system. The command reports the amount of disk space used and available...

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.

touch Command in Linux/Unix with Examples

The "touch" command in Linux (and other Unix-like operating systems) is used to create new empty files or update the timestamps of existing files. The touch command is a simple...

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.

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.

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.

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.

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.

rename Command in Linux/Unix with Examples

The rename command in Linux is used to rename multiple files at once, using a specified pattern. The command is also known as rename, ren, and mv. This command is...

3 minutes read.

pwd Command in Linux/Unix with Examples

The "pwd" command in Linux (and other Unix-like operating systems) stands for "print working directory." When executed, it simply displays the current directory (or folder) that the user is currently...

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.

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.

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.

adduser Command in Linux with Examples

What is the adduser command? The adduser command creates a new user, and this command may be used to add apre-existing user to a group that already exists. How to use the...

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