Hello All… Today we are learning about DU command, du command is one of the important and useful commands for Linux, Unix, and AIX systems. the du command is used for checking disk usage.
Du is an abbreviation of disk usage for this command is used.
What you will learn after reading the article?
What is Du command and uses in the Linux/Unix systems?
du is a short form of disk usage. It is used for checking disk usage of specific files, file systems and directories. It is used to get the file details which is using most space.
du command syntax
du [OPTION]… [FILE]… (or)
du [OPTION]… –files0-from=F
du command in Linux, Unix, Aix, and PowerShell windows
du command in Linux and Unix Systems: du command will work similar in Linux and Unix systems.
AIX du command: It also works the same as Linux and Unix systems. If you want to learn more about AIX du command.
Du command in windows: Du command will not work in Windows Powershell. You can use git bash or other Linux software to use du command in windows.
What is the difference between du and df command?
It is one of the most common questions that will come to your mind, If you already know df command.
DF Command: It will provide you the complete details of free memory, used memory and details of file system in Linux systems. (DF Command in Complete details)
DU Command: It only gives you details about the how much memory is used by file, also very useful in getting details of largest file.
DU Command Options?
-0: It will end each output line with NUL, not the newline.
-a: This option is used for all, It will count all files not only directories.
-B: This option will scale the size in your desired format. like BM.
-b: This option give the size in bytes.
-c: This will give you a total.
-D: dereference only symlinks that are listed on the command line.
-d: It will print the total for a directory
-H: This option equivalent to –dereference-args (-D)
-h: It will give result in human readable format.
-k: Gives result in block size of 1k.
-L: dereference all symbolic links.
-l: count sizes many times if hard linked.
-m: It gives result in block size of 1m.
-P: It will not follow any symbolic links.
-S: –separate-dirs ; for directories do not include size of subdirectories.
–si; like -h, but use powers of 1000 not 1024.
-s: display only a total for each argument.
-t, –threshold=SIZE exclude entries smaller than SIZE if positive, or entries greater than SIZE if negative.
–time show time of the last modification of any file in thedirectory, 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, which can be:full-iso, long-iso, iso, or +FORMAT; FORMAT is interpreted like in ‘date’.
-X, –exclude-from=FILE exclude files that match any pattern in FILE.
–exclude=PATTERN exclude files that match PATTERN.
-x, –one-file-system skip directories on different file systems.
DU Command Examples:
Example 1: du command without any option
When you run the du command without any parameters and file name or directory name.
Output: It will list all the directories and their respective size of all the file systems starting from the current directory.
$ du
249 ./example1
148 ./thinkheights/test
153 ./thinkheights
Example 2: du -h for human-readable format
When you run the du command with h parameters du -h, Here lowercase h is used for human-readable format.
Output: It will list all the directories and their respective size in kb, Mb, and Gb size which is easily understandable.
$ du -h
2k ./example1
1m ./thinkheights/test
4k ./thinkheights
Example 3: du -a for getting details of all files
When you run the du command with h parameters du -a, Here lowercase a is used for all files. parameter h we combine for getting details in a human-readable format.
Output: It will list all the directories and files and their respective size in kb, Mb, and Gb size which is easily understandable.
$ du -ah
12K ./–newer=2020-09-09
4M ./.unix
2K ./.world
1M ./example1
1.0K ./log.txt
Example 4: du -s for getting total size
When you run the du command with h parameters du -sh, Here lowercase s is used for getting total size files.
Output: It will total the size of all the directories and files of a given path.
$ du -sh unix
47k unix
Example 5: du -k for getting details in block-size in 1k.
When you run the du command with h parameters du -kh, Here lowercase k is used for getting details in the block size of 1k.
Output: It will give details in block size of 1k.
$ du -kh
2K ./example1
4K ./thinkheights/test
4K ./thinkheights
47K .
Example 6: du -m for getting total size
When you run the du command with m parameters du -m, Here lowercase m is used for getting details in the block size of 1m.
Output: It will give details in block size of 1mb.
$ du -kh
2K ./example1
4K ./thinkheights/test
4M ./thinkheights
47M .
Example 7: du -c for getting details and total size
When you run the du command with c parameters du -c, Here lowercase c is used for getting details as well as total size. ( with s parameter, you will get only total size)
Output: Below is the result, you will get details of file and total also.
$ du -ch
4K ./example1
2M ./thinkheights/test
2M ./thinkheights
470M .
474M total
Example 8: du -ah --time
If you want to sort the files according to modification time you should use –time
Output: It will sort according to modification time.
$ du -ah –time
12K 2020-09-12 19:11 ./–newer=2020-09-09
0 2020-07-25 16:27 ./.unix
0 2020-07-25 16:27 ./.world
0 2020-07-25 15:09 ./example1
1.0K 2020-07-31 16:03 ./log.txt
12K 2020-09-10 17:12 ./log1.tar
1.0K 2020-07-27 02:48 ./log1.txt
1.0K 2020-07-13 21:42 ./log2.txt
1.0K 2020-07-13 21:42 ./log3.txt
0 2020-07-27 21:17 ./new_file
12K 2020-09-10 18:54 ./new_file.tar
1.0K 2020-07-27 21:18 ./new_file.txt
Example 9: du -ah --exclude 'pattern'
If you want to exclude the specific pattern files, you can use — exclude
Output: It will give details and calculate all files except the pattern we have given .tar
$ du -ah –exclude ‘*.tar’
12K ./–newer=2020-09-09
0 ./.unix
0 ./.world
0 ./example1
1.0K ./log.txt
1.0K ./log1.txt
1.0K ./log2.txt
1.0K ./log3.txt
0 ./new_file
1.0K ./new_file.txt
1.0K ./think.txt
This is all from our side, If you want, we will add more examples, let us know in the comment section. Happy learning and sharing.