The DU or the disk usage command is used for monitoring the disk space usage of each file or directory of your Linux system. With this command, you can instantly get to know which files or directories of your system are consuming more storage resources so that you can manage them accordingly.
Here at LinuxAPT, we shall look into how to use DU Command in Linux.
What is the syntax of DU Command ?
DU syntax is given below:
du [OPTION]... [FILE]...
du [OPTION]... --files0-from=F
DU Command is given below:
-a, --all - Write counts for all files, including directories.
--apparent-size- Print apparent sizes, rather than disk usage; although the apparent size is usually smaller, it may be larger due to holes in ('sparse') files, internal fragmentation, and indirect blocks.
-B, --block-size=SIZE- Scale sizes by SIZE before printing them. For example, '-BM' prints sizes in units of 1,048,576 bytes. (see SIZE format below).
-b, --bytes - Equivalent to '--apparent-size --block-size=1'.
-c, --total - Display a grand total.
-D, --dereference-args - Dereference only symlinks that are listed on the command line.
--files0-from=F- Summarize disk usage of the null-terminated file names specified in file F; If F is "-" then read names from standard input.
-H - Equivalent to --dereference-args (-D).
-h, --human-readable - Print sizes in human readable format, rounding values and using abbreviations. For example, "1K", "234M", "2G", etc.
--si- Like -h, but use powers of 1000, not 1024.
-k- Like --block-size=1K.
-l, --count-links - Count sizes many times if hard-linked.
-m - Like --block-size=1M.
-L, --dereference - Dereference all symbolic links.
-P, --no-dereference - Don't follow any symbolic links (this is the default).
-0, --null - End each output line with 0 byte rather than newline.
-S, --separate-dirs - Do not include size of subdirectories.
-s, --summarize- Display only a total for each argument.
-x, --one-file-system- Skip directories on different file systems.
--exclude-from=FILE - Exclude files that match any pattern in FILE.
--exclude=PATTERN - Exclude files that match PATTERN.
-d, --max-depth=N - Print the total for a directory (or file, with --all) only if it's N or fewer levels below the command line argument; --max-depth=0 - is the same as --summarize.
--time- Show time of the last modification of any file in the directory, or any of its subdirectories.
--time=WORD - Show time as WORD instead of modification time: atime, access, use, ctime or status.
--time-style=STYLE - Show times using style STYLE: full-iso, long-iso, iso, or +FORMAT. (FORMAT is interpreted like the format of 'date'.)
--help- Display a help message and exit.
--version - Output version information and exit.
You can Run the below command to find out more about the DU command:
$ du -- help
From the output, you will see all the different options that can be used with this command to serve different purposes:
Examples of Using the DU Command
1. Using the DU Command Without any Options
The DU command can be used independently without any of its options.
The command will take the below form:
This command simply lists down the disk usage of all the files and directories present on the system.
2. Using the DU Command to Print the Output of the DU Command in Human-Readable Format
The disk usage is usually not in human-readable format, i.e., without any measurement units. Therefore, to print the disk usage in human-readable format, you have to use the DU command below:
$ du –h
3. Using the DU Command to Print the Total Disk Usage With the DU Command
We can also use the DU command to print the total disk usage once it has printed the disk usage of the individual files and directories. To do this, simply run the below command:
$ du –c
The total disk usage will be displayed at the end of the output.
4. Using the DU Command to Print the Output of the DU Command up to a Certain Specified Level
The DU command prints the disk usage while going deep inside the directories of your system recursively. However, you can restrict this output to a certain specified level by using the following variant of the DU command in Linux:
$ du –d 1
This modified DU command will print the disk usage on the terminal only up to the first level, i.e., it will not go deep down beyond the first level.
This article covers the usage of the DU command in Linux. In fact, the du command makes it convenient for Linux users to stay aware of their disk consumption and make an upgrade if required.
How to sort du -h command output by size ?
The sort command provides -h parameter allows to compare human-readable numbers (e.g., 10K 15M 1G etc). This helps up to compare the results of `du -h` and short them.
It takes the below form:
$ du -h * | sort -h
This will display the results in the ascending order by size.
You can also reverse this using -r to show results in descending order with the below command:
$ du -h * | sort -rh
What is the du -sh * command ?
The du -sh * command will display only a total for each argument as well as print sizes in human readable format (e.g., 1K 234M 2G).
The command takes the below form:
$ du -sh
What is df command ?
The df command displays the amount of disk space available on the filesystem with each file name's argument.
The df command can be run by any user. Like many Linux commands, df uses the following structure:
$ df [OPTION]... [FILE]...
The df command primarily checks disk usage on a mounted filesystem. If you don't include a file name, the output shows the space available on all currently mounted filesystems.
What is the differences between the df and du command in Linux ?
While du reports files and directories disk usage, df reports how much disk space your filesystem is using.
How to use the du Command in GB / MB / TB format ?
The du command is used to estimate file space usage. To print sizes in human readable format such as 5K, 400M, or 3G, type the following commands:
du -h /tmp
du -h /home/user
How to use du (Disk Usage) Commands to Find Disk Usage of Files and Directories in Linux ?
The du command displays the files and directory sizes in a recursive manner.
- To find out the disk usage summary of a /home/user directory tree and each of its subdirectories. Run the command as: # du /home/folderName
- You can use the "-h" option with the "du" command to print results in "Human Readable Format". This means you can see sizes in Bytes, Kilobytes, Megabytes, Gigabytes, etc: # du -h /home/folderName
- You can get the summary of a grand total disk usage size of a directory using the option "-s": # du -sh /home/folderName
- You can use the "-a" flag with the "du" command displays the disk usage of all the files and directories: # du -a /home/folderName
- You can use the "-a" flag along with "-h" displays disk usage of all files and folders in a human-readable format: # du -ah /home/folderName
- You can find out the disk usage of a directory tree with its subtree in Kilobyte blocks using the "-k" (displays size in 1024 bytes units): # du -k /home/folderName
- You can get the summary of disk usage of directory tree along with its subtrees in Megabytes (MB) only using the "-mh" option : # du -mh /home/folderName
- You can use the "-c" flag to get a grand total usage disk space at the last line of the output: # du -ch /home/folderName