The du command in Linux is a powerful utility that allows users to analyze and report on disk usage within directories and files. Whether you’re trying to identify space-hogging directories, manage disk space efficiently, or simply gain insights into storage consumption, the du command provides valuable information.
Here, we’ll explore the du command, its basic syntax, and various examples to demonstrate its functionality.
Syntax
du [options] [directory/file]
Here,
- [options]: various flags that modify the behavior of the command
- [directory/file]: the target directory or file for which disk usage information is desired. If no directory or file is specified, du will default to the current directory.
Basic du Command Example
du /home/mandeep/test
Output:
44 /home/mandeep/test/data
2012 /home/mandeep/test/system design
24 /home/mandeep/test/table/sample_table/tree
28 /home/mandeep/test/table/sample_table
32 /home/mandeep/test/table
100104 /home/mandeep/test
Options available in du command in Linux
The du command offers a range of options that enhance its functionality. Let’s explore some commonly used options:
| Option |
Description |
| -0, –null |
End each output line with NULL. |
| -a or –all |
Displays disk usage information for all files and directories, including hidden ones. |
| –apparent-size |
Print apparent sizes, rather than disk usage. |
| -B, –block-size=SIZE |
Scale sizes to SIZE before printing on console. |
| -c or –total |
Shows the total disk usage in addition to individual usage for directories and files. |
| -d, –max-depth=N |
Print total for directory only if it is N or fewer levels below the command line argument. |
| -h or –human-readable |
Displays sizes in human-readable format (KB, MB, GB), making it easier to interpret the usage. |
| -S, –separate-dirs |
For directories, don’t include size of subdirectories. |
| -s or –summarize |
Provides a summary of the disk usage for the specified directory or file, without subdirectory details. |
| –time |
Show the time of last modification of any file or directory. |
| –exclude |
Excludes specific directories or files from disk usage calculation based on patterns or names. |
du Command Examples
Example 1. -h option in du command
If we want to print sizes in human readable format(K, M, G), use -h option
du -h /home/Mandeep/test
Output:
44K /home/mandeep/test/data
2.0M /home/mandeep/test/system design
24K /home/mandeep/test/table/sample_table/tree
28K /home/mandeep/test/table/sample_table
32K /home/mandeep/test/table
98M /home/mandeep/test
44K /home/mandeep/test/data
2.0M /home/mandeep/test/system design
24K /home/mandeep/test/table/sample_table/tree
28K /home/mandeep/test/table/sample_table
32K /home/mandeep/test/table
98M /home/mandeep/test
Example 2. To display all files, including directories, with their sizes
Use -a option for printing all files including directories.
du -a -h /home/mandeep/test
Output: This is partial output of above command.
4.0K /home/mandeep/test/blah1-new
4.0K /home/mandeep/test/fbtest.py
8.0K /home/mandeep/test/data/4.txt
4.0K /home/mandeep/test/data/7.txt
4.0K /home/mandeep/test/data/1.txt
4.0K /home/mandeep/test/data/3.txt
4.0K /home/mandeep/test/data/6.txt
4.0K /home/mandeep/test/data/2.txt
4.0K /home/mandeep/test/data/8.txt
8.0K /home/mandeep/test/data/5.txt
44K /home/mandeep/test/data
4.0K /home/mandeep/test/notifier.py
Example 3. To calculate the total size of a directory and its subdirectories
Use -c option to print total size
du -c -h /home/mandeep/test
Output:
44K /home/mandeep/test/data
2.0M /home/mandeep/test/system design
24K /home/mandeep/test/table/sample_table/tree
28K /home/mandeep/test/table/sample_table
32K /home/mandeep/test/table
98M /home/mandeep/test
98M total
Example 4. -d option in du command
To print sizes to particular level, use -d option with level no.
du -d 1 /home/mandeep/test
Output:
44 /home/mandeep/test/data
2012 /home/mandeep/test/system design
32 /home/mandeep/test/table
100104 /home/mandeep/test
Now try with level 2, you will get some extra directories
du -d 2 /home/mandeep/test
Output:
44 /home/mandeep/test/data
2012 /home/mandeep/test/system design
28 /home/mandeep/test/table/sample_table
32 /home/mandeep/test/table
100104 /home/mandeep/test
Example 4. To obtain the disk usage summary for a directory
Get summary of file system using -s option
du -s /home/mandeep/test
Output:
100104 /home/mandeep/test
Example 5. To view the timestamp of the last modification of files and directories
Get the timestamp of last modified using –time option
du --time -h /home/mandeep/test
Output:
44K 2018-01-14 22:22 /home/mandeep/test/data
2.0M 2017-12-24 23:06 /home/mandeep/test/system design
24K 2017-12-30 10:20 /home/mandeep/test/table/sample_table/tree
28K 2017-12-30 10:20 /home/mandeep/test/table/sample_table
32K 2017-12-30 10:20 /home/mandeep/test/table
98M 2018-02-02 17:32 /home/mandeep/test
Conclusion
here, we have discussed the du command in Linux which provides us essential insights into file and directory space usage, enabling users to effectively manage their storage. We also discussed the variety of options available. Overall, we can say that mastering du command and its options, a user can efficiently analyze disk usage, identify storage areas, and optimize their system’s storage utilization.
du Command in Linux with examples – FAQs
What is the du command in Linux, and how does it differ from the df command?
The du command in Linux is used to estimate file space usage. It displays the sizes of directories and files, helping users identify which directories are consuming the most disk space.
How can I use the du command to find the largest directories on my system?
To find the largest directories, you can use the du command with the –max-depth option.
du --max-depth=1 /path
Is there a way to display disk usage in a human-readable format with the du command?
Yes, the du command supports the -h option to display sizes in a human-readable format (e.g., KB, MB, GB).
du -h /path
It will provide a more readable output.
Can I use the du command to find large files within a directory?
You can combine the find command with du to identify large files.
find /path -type f -exec du -h {} + | sort -rh | head -n 10
It will list the top 10 largest files under the specified path.
How do I exclude certain directories or files from the du command output?
To exclude specific directories, use the –exclude option with the du command.
du -h --exclude=/path/to/exclude /path
Similar Reads
How to Compare Files Line by Line in Linux | diff Command
In the world of Linux, managing and comparing files is a common task for system administrators and developers alike. The ability to compare files line by line is crucial for identifying differences, debugging code, and ensuring the integrity of data. One powerful tool that facilitates this process i
9 min read
diff3 command in Linux with examples
diff3 command is used to compare the three files line by line. It internally uses the diff command to compare. When three files are compared then the following output may come which have their own meaning: ==== : It means all the files are different. ====1 : File 1 is different. ====2 : File 2 is di
3 min read
dir command in Linux with examples
The dir command in Linux is used to list the contents of a directory, providing an overview of the files and folders within it. How is the dir command different from ls?dir command differs from the ls command in the format of listing contents that is in default listing options. By default, dir comma
5 min read
Dirname Command in Linux with Examples
dirname is a command in Linux that is used to remove the trailing forward slashes "/" from the NAME and print the remaining portion. If the argument NAME does not contain the forward slash "/" then it simply prints dot ".". In other words, we can say that the 'dirname' command is a useful tool for e
3 min read
dirs command in Linux with examples
dirs command shell builtin is used to display the list of currently remembered directories. By default, it includes the directory you are currently in. A directory can get into the list via pushd command followed by the dir name and can be removed via popd command. Syntax: dirs [-clpv] [+N] [-N] It
1 min read
enable and disable command in Linux
Enables and disables are the built-in shell commands. enable command is used to start the printers or classes whereas the disable command is used to stop the printers or classes. Syntax For enable Command: enable [-a] [-dnps] [-f filename][name ...] Syntax For disable Command: disable [-c] [-W] [ -r
1 min read
dmesg command in Linux for driver messages
dmesg command also called “driver message” or “display message” is used to examine the kernel ring buffer and print the message buffer of the kernel. The output of this command contains the messages produced by the device drivers. Usage of dmesg:When the computer boots up, there are lot of messages(
6 min read
dmidecode command in Linux with Examples
The 'dmidecode' command, short for Desktop Management Interface table decoder, is a powerful utility in Linux that retrieves detailed information about your system's hardware components. This tool reads the DMI table and presents the data in a human-readable format, making it invaluable for system a
4 min read
domainname Command in Linux With Examples
domainname command in Linux is used to return the Network Information System (NIS) domain name of the host. You can use hostname -d command as well to get the host domainname. If the domain name is not set up in your host then the response will be "none". In networking terminology, the domain name i
3 min read
dos2unix and unix2dos commands
Sometimes, you will need to move files between windows and unix systems. Window files use the same format as Dos, where the end of line is signified by two characters, Carriage Return or CR or \r followed by Line Feed or LF or \n. Unix files, on the other hand, use only Line Feed (\n). unix2dos is a
4 min read
dosfsck command in Linux with Examples
dosfsck stands for DOS File System Consistency Check and is part of the dosfstools package in Unix-like operating systems. This command diagnoses and repairs MS-DOS filesystems (FAT12, FAT16, FAT32). Before running dosfsck, it is crucial to unmount the filesystem to avoid data corruption. Syntax:dos
4 min read
dstat Command in Linux With Examples
dstat is a tool that is used to retrieve information or statistics form components of the system such as network connections, IO devices, or CPU, etc. It is generally used by system administrators to retrieve a handful of information about the above-mentioned components of the system. It itself perf
3 min read
du Command in LINUX
The du command in Linux is a powerful utility that allows users to analyze and report on disk usage within directories and files. Whether you’re trying to identify space-hogging directories, manage disk space efficiently, or simply gain insights into storage consumption, the du command provides valu
5 min read
dump command in Linux with examples
The dump command in Linux is used for backing up the filesystem to a storage device. Unlike commands that back up individual files, dump operates on entire filesystems, transferring data to tape, disk, or other storage media for safekeeping. One of its key advantages is the ability to perform increm
6 min read
dumpe2fs command in Linux with examples
dumpe2fs command is used to print the super block and blocks group information for the filesystem present on device. Can be used with ext2/ext3/ext4 filesystem for information. The printed information may be old or inconsistent when it is used with a mounted filesystem. Don't forget to unmount your
2 min read
dumpkeys command in Linux with examples
'dumpkeys' command in Linux is used for the dump keyboard translation tables. It writes to the standard output and the current contents of the keyboard driver's translation tables in the format specified by Keymaps. Here, we’ll break down the syntax, options, and practical examples of using the dump
3 min read
echo command in Linux with Examples
The echo command in Linux is a built-in command that allows users to display lines of text or strings that are passed as arguments. It is commonly used in shell scripts and batch files to output status text to the screen or a file. Syntax of `echo` command in Linuxecho [option] [string]Here, [option
3 min read
ed command in Linux with examples
Linux 'ed' command is used to start the ed text editor, a very minimal fronted line-based text editor. The lines are very simple in relation to other text files, hence easy to work on, for creating as well as editing, display, and manipulation of files. Since it was the first editor that was include
4 min read
egrep command in Linux with examples
The 'egrep' command in Linux is a powerful pattern-searching utility that belongs to the family of grep functions. It functions similarly to 'grep -E' by treating patterns as extended regular expressions, allowing more complex pattern matching without the need to escape special characters. This comm
3 min read
eject command in Linux with examples
eject allows ejecting a removable media (typically a CD-ROM, floppy disk, tape, or JAZ or ZIP disk) using the software. You can also control some multi-disc CD-ROM changers, the auto-eject feature (supported by some devices), and close the disc tray of some CD-ROM drives. Syntaxeject [...OPTIONS]The
4 min read
emacs command in Linux with examples
Introduction to Emacs Editor in Linux/Unix Systems: The Emacs is referred to a family of editors, which means it has many versions or flavors or iterations. The most commonly used version of Emacs editor is GNU Emacs and was created by Richard Stallman. The main difference between text editors like
5 min read
env command in Linux with Examples
The 'env' command in Linux is a versatile tool used to manage environment variables. It can print all the current environment variables, set new ones, or run a command within a custom environment. Additionally, 'env' is commonly used in shell scripts to launch the correct interpreter, ensuring the s
3 min read
eval command in Linux with Examples
eval is a built-in Linux command that executes arguments as a shell command. It combines arguments into a single string, uses it as input to the shell, and executes the commands. Syntaxeval [arg ...]Here, arg can be one or more arguments that eval will process and combine into a single shell command
2 min read
ex command in Linux with examples
ex (stands for extended) is a text editor in Linux which is also termed as the line editor mode of the vi editor. This editor simply provided some editing commands which has greater mobility. With the help of this editor, a user can easily move between files. Also, he/she has a lot of ways to transf
4 min read
exec command in Linux with examples
The 'exec' command in Linux is a powerful shell built-in used to replace the current shell process with a new command process. Unlike typical commands that create a new process, 'exec' transforms the existing process, directly impacting the efficiency and behavior of scripts. What is the exec Comman
3 min read