In this article, we are going to learn how to use Linux du command. du command is a powerful command in Linux and Unix Operating System used to check disk usage of files or directories.
Refer to the below Linux du command with examples:
To check the installed Linux du command package version we can use the du command with option – -version.
[root@localhost ~]# du --version # To check Installed du Package Version du (GNU coreutils) 5.97 Copyright (C) 2006 Free Software Foundation, Inc. This is free software. You may redistribute copies of it under the terms of the GNU General Public License <http://www.gnu.org/licenses/gpl.html>. There is NO WARRANTY, to the extent permitted by law. Written by Torbjorn Granlund, David MacKenzie, Paul Eggert, and Jim Meyering.
To check disk usage of a particular file uses the below command.
[root@localhost data]# du data.zip # Check disk usage or size of a Particular file 1350084 data.zip
To check disk usage of contents of a directory you can use the below command.
[root@localhost ~]# du data/* # Checking Disk Usage of contents of a Directory 1350084 data/data.zip 2727652 data/kali-linux-2017.1-amd64.iso 668740 data/ubuntu-16.04-server-amd64.iso
OR you can use the du command with argument -a to check disk usage of all contents of a directory. Refer to the below sample output.
[root@localhost ~]# du -a data/ # Checking Disk Usage of all Contents of a Directory 1350084 data/data.zip 2727652 data/kali-linux-2017.1-amd64.iso 668740 data/ubuntu-16.04-server-amd64.iso 4746480 data/
Normally du command print the size of the files and directories in Disk Blocks which is cannot be understood easily. so to check the disk usage of files and directories in Human Readable format (KB, MB, GB..etc..) use the du command with argument -h.
[root@localhost data]# du -h kali-linux-2017.1-amd64.iso # Checking Size of the File in Human Readable Format 2.7G kali-linux-2017.1-amd64.iso
From here I am going to explain all du related arguments with Human Readable Format.
To check the size of all content of a directory in Human Readable format we can use the du command with argument -ah.
[root@localhost ~]# du -ah data/ # Checking all content of a Directory in Human Readable Format 1.3G data/data.zip 2.7G data/kali-linux-2017.1-amd64.iso 654M data/ubuntu-16.04-server-amd64.iso 4.6G data/
To check the total size of a directory in Human Readable Format refer to the below Linux du command.
[root@localhost ~]# du -h data/ # Checking Full Size of a Directory in Human Readable Format 4.6G data/
If you trying to check disk usage of a directory which contains so many contents and contains a maximum number of files and directories which is not viewable on a single page of the terminal then you can use less command by using a filter with du command. Refer to the command below.
[root@localhost ~]# du -h /etc/ | less # Using less command with du command 8.0K /etc/pcmcia 8.0K /etc/portreserve 172K /etc/pki/java 32K /etc/pki/rpm-gpg 4.0K /etc/pki/rsyslog 4.0K /etc/pki/CA/private 4.0K /etc/pki/CA/certs 4.0K /etc/pki/CA/crl 4.0K /etc/pki/CA/newcerts 20K /etc/pki/CA 180K /etc/pki/ca-trust/extracted/java
du command with argument -c will print the grand total size of the complete content of directory or files.
[root@localhost data]# du -ch * # Checking Grand Total Size of a Directory with It's Content 1.3G data.zip 2.7G kali-linux-2017.1-amd64.iso 654M ubuntu-16.04-server-amd64.iso 4.6G total
Let’s check the grand total disk usage of all content of a directory in Human Readable format using du command with argument -ach.
[root@localhost ~]# du -ach data/ # Checking Grand Total Size of All content of a Directory 1.3G data/data.zip 2.7G data/kali-linux-2017.1-amd64.iso 654M data/ubuntu-16.04-server-amd64.iso 4.6G data/ 4.6G total
To check the disk usage in bytes we can use the du command with argument -b. Refer to the sample output below.
[root@localhost ~]# du -b data/* # Checking Size in Bytes 1382479012 data/data.zip 2794307584 data/kali-linux-2017.1-amd64.iso 686817280 data/ubuntu-16.04-server-amd64.iso
du command with argument -k will print the size of files and directories in Kilobyte (KB).
[root@localhost ~]# du -k data/* # Checking Size in KB 1350084 data/data.zip 2727652 data/kali-linux-2017.1-amd64.iso 668740 data/ubuntu-16.04-server-amd64.iso
OR you can use the du command with argument -BK to check the disk usage in KB.
[root@localhost data]# du -BK * # Checking Size in KB 1350084K data.zip 2727652K kali-linux-2017.1-amd64.iso 668740K ubuntu-16.04-server-amd64.iso
To check disk usage of files and directories in MB you can use the du command with argument -m.
[root@localhost data]# du -m * # Checking Size in MB 1319 data.zip 2664 kali-linux-2017.1-amd64.iso 654 ubuntu-16.04-server-amd64.iso
OR you can use the du command with argument -BM to check the size of files and directories in MB.
[root@localhost data]# du -BM * # Checking Size in MB 1319M data.zip 2664M kali-linux-2017.1-amd64.iso 654M ubuntu-16.04-server-amd64.iso
Like that to check disk usage in GigaByte you can use the argument -BG.
du command with argument – -time will print the Last modification Date & Time of files and directories.
[root@localhost ~]# du --time data/* # Checking Last Modified date and time using du Command 4 2017-05-09 01:26 data/file1.txt 4 2017-05-09 01:26 data/file2.txt 4 2017-05-09 01:26 data/file3.txt 4 2017-05-09 01:26 data/file4.txt 4 2017-05-09 01:26 data/file5.txt
Suppose you have so many files or directories and you want to check disk usage by excluding some files which you don’t want to check then you can do the same by du command with argument – -exclude. For example, Here I have a directory with some .iso and .zip files. Now I want to check the size of only .zip files and want to exclude .iso files then to do so we can use the below command.
[root@localhost data]# ls data.zip kali-linux-2017.1-amd64.iso mydoc.zip ubuntu-16.04-server-amd64.iso [root@localhost data]# du -ah --exclude="*.iso" # Checking Disk Usage by excluding .iso Files 1.3G ./mydoc.zip 1.3G ./data.zip 2.6G .
OR if you want to check the size of the .iso file and want to exclude the .zip file then refer to the below command.
[root@localhost data]# du -ah --exclude="*.zip" # Checking Disk Usage by excluding .zip Files 2.7G ./kali-linux-2017.1-amd64.iso 654M ./ubuntu-16.04-server-amd64.iso 3.3G .
For du command related options you can use the below command.
[root@localhost ~]# du --help # For more du command related options Usage: du [OPTION]... [FILE]... or: du [OPTION]... --files0-from=F Summarize disk usage of each FILE, recursively for directories. Mandatory arguments to long options are mandatory for short options too. -a, --all write counts for all files, not just 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, indirect blocks, and the like -B, --block-size=SIZE use SIZE-byte blocks -b, --bytes equivalent to `--apparent-size --block-size=1' -c, --total produce a grand total -D, --dereference-args dereference FILEs that are symbolic links --files0-from=F summarize disk usage of the NUL-terminated file names specified in file F -H like --si, but also evokes a warning; will soon change to be equivalent to --dereference-args (-D) -h, --human-readable print sizes in human readable format (e.g., 1K 234M 2G) --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 FILE, --exclude-from=FILE Exclude files that match any pattern in FILE. --exclude=PATTERN Exclude files that match PATTERN. --max-depth=N print the total for a directory (or file, with --all) only if it is 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, +FORMAT FORMAT is interpreted like `date' --help display this help and exit --version output version information and exit SIZE may be (or may be an integer optionally followed by) one of following: kB 1000, K 1024, MB 1000*1000, M 1024*1024, and so on for G, T, P, E, Z, Y. Report bugs to <bug-coreutils@gnu.org>.
OR refer below command for more deep information related to du command.
[root@localhost ~]# man du # For more du command Related Informations
Also Read – Best chattr command to change File Attributes – Making Important Files Immutable
That’s all, In this article, we have explained Best Linux Du Command With Examples. I hope you enjoy this article. If you like this article, then just share it. If you have any questions about this article, please comment.
very useful . Thanks for sharing
Very detailed and clear information.