twitter
Categories:- Unix Commands

UNIX / Linux du Command Tutorial with Practical Examples

Overview :

 

du stands for disk usage. As the name suggests ,du command is used to calculate or summarize  the disk usage of file ,directories & file system in UNIX like operating system.

 

SYNTAX

 

# du [OPTION]... [FILE]...
# du [OPTION]... --files0-from=F

 

From the syntax it is clear that du command expects options and file name as arguments. Though these arguments are not mandatory but are required for getting disk usage of specific files in specific formats. Input file names can also be read from a separate file.

 

Options:

-a display an entry for each file contained in the current directory
-c display a grand total of the disk usage found by the other arguments
-d the depth at which summing should occur. -d 0 sums at the current level, -d 1 sums at the subdirectory, -d 2 at sub-subdirectories, etc.
-h display the result in human readable format
-k show sizes as multiples of 1024 bytes, not 512-byte
-L calculate disk usage for link references anywhere
-s report only the sum of the usage in the current directory, not for each file
-x only traverse files and directories on the device on which the pathname argument is specified.

-X ,

--exclude-from=FILE : 

exclude files that match any pattern in FILE

 

Example : 1 Disk usage of a Directory , when du command excuted without any options

 

# du  Pictures

12072 Pictures

 

In the above example , 'Pictures' is a directory and when du command is executed , it displays disk usage figures in Kilobytes (default block size) .

 

Example : 2 Display sizes in human readable format (e.g., 1K 250M , 2G)

 

# du -h Pictures

12M   Pictures

 

Example : 3  Change the default block size to produce output in megabytes or gigabytes.

 

This can be done through -B option followed by 'M' for megabyte and 'G' for gigabyte.

 

#  du -BM /var/lib/mysql/*

Above command shows disk usage in MB

 

# du -BG /var/lib/mysql/plist/

5G      /var/lib/mysql/plist/

 

To display the disk usage in in GB , use G option as shown above.

 

Example : 4 Display the Sum usage of all the  directories (-s) in human-readable format

 

# du -sh /var/log/
299M   /var/log/

 

Above comand shows sum of disk usage of all the files and directories under /var/log/.

 

Example : 5  Display disk usage counts for all the files

 

This can be done using -a option.

 

# du -a /var/log/httpd/

 

As shown in the above example , that disk usage of individual files (inside the directory '/var/log/httpd') is displayed in the output.

 

Example : 6  Dsiplay the  grand total of disk usage for individual files in a directory

 

This can be done through -c along with -a option

 

# du -ca /var/log/httpd/

 

Example : 7 Disk usage of all subdirectories and files including hidden files within the current directory (sorted by filesize)

 

#  du -sh .[!.]* * | sort -n

 

Example : 8 Disk usage of subdirectories under the root directory (-d 1, trailing /) with a sum total at the end (-c), all displayed in human-readable format (-h) without traversing into other filesystems (-x).

 

# du --max-depth=1 -c -h -x /

 

Example : 9 Estimate Disk usage by excluding particular file type

 

#  du -cbha –exclude="*.iso"

 

Above command will calculate the disk usage all the files and directories in the current directory by excluding .iso files

Post Your Comments
Name:
Email:
Comment:
Security Code  *
Enter Security Code  *
Can't read the image? click here to refresh