du command in Linux with Useful Examples

du is the short of disk uses. Which means the du command calculates the size on disk used by a files. It is the basic Linux command frequently used the Linux system users.

Syntax:

du [OPTION]... [FILE]...

In this tutorial, you will learn the uses of du command in Linux with useful examples.

du Command Examples

du is an frequent used command by the Unix/Linux system users. It calculates the size of a file, directory on Unix like systems. It also allowed to check size of directory including all files recursively.

  1. Check single file size – Use the simple du command to check size of a specific file. This will show the total size in bytes.
    du /home/rahul/data/backup.tar.gz 
    
    165608  /home/rahul/data/backup.tar.gz
    

    Also run the du command with a directory. It will show you the size of the directory and sub directories in output.

    du /home/rahul/data 
    
    20      /home/rahul/data/mydumpadmin/python-mysql-backup
    44      /home/rahul/data/mydumpadmin
    180568  /home/rahul/data/
    
  2. Show summary of size The default du command displays size of all subdirectories. Use -s option to total size of main directories and do not display subdirectories sizes.
    du -s /home/rahul/data 
    
    180568  /home/rahul/data
    
  3. Check size in human readable format – Use -h or --human-readable option to print size in human readable format (like: K, M, G, T). The default du shows the size in bytes but you can print size in KB, MB, GB, which is easy to read.
    du -h /home/rahul/data/backup.tar.gz 
    
    162M    /home/rahul/data/backup.tar.gz
    
  4. Show total size – Use -c or --total option to show total size of all files at end. This will show you all files size individually as well as total size.
    du -c -h /home/rahul/data 
    
    20K     /home/rahul/data/mydumpadmin/python-mysql-backup
    44K     /home/rahul/data/mydumpadmin
    177M    /home/rahul/data
    177M    total
    
  5. Excluding some tiles – We can also exclude some files from calculating size. For example, do not calculate files with “.log” extension.
    du -s -h --exclude="*.log" /home/rahul/data 
    
    148M    /home/rahul/data
    

    The command also can read multiple pattern defined in a file and exclude them from size calculation. Add a list.txt with some file pattern to exclude from size calculation

    du -s -h --exclude-from=list.txt /home/rahul/data 
    
    142M    /home/rahul/data
    

Conclusion

In this tutorial, you have learned to uses of du command in Linux with examples.

The post du command in Linux with Useful Examples appeared first on TecAdmin.

command du General Articles TecAdmin