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.
DU syntax is given below:
du [OPTION]... [FILE]...
du [OPTION]... --files0-from=F
-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.
-X,
--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:
The DU command can be used independently without any of its options.
The command will take the below form:
$ du
This command simply lists down the disk usage of all the files and directories present on the system.
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
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.
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.
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
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
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.
While du reports files and directories disk usage, df reports how much disk space your filesystem is using.
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
du -h /tmp
du -h /home/user
The du command displays the files and directory sizes in a recursive manner.