Linux Commands – du

Linux Commands – du

Linux Commands – du

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed ps command in Linux which is used to display current processes.

https://cloudaffaire.com/linux-commands-ps/

In this blog post, we will discuss du command in Linux. du stands for disk usage and is used to get disk space utilization of files and directories. Normally the disk space is printed in the units of 1024 bytes, but this can be overridden with block size option. Non-integer quantities are rounded up to the next higher unit.

Linux Commands – du:

You can use du command to get the disk space usage of files and directories. With no arguments, du reports the disk space for the current directory. By default, du returns in 1024 bytes or 1 kilo-bytes format but this can be changed using –block-size option or DU_BLOCK_SIZE, BLOCK_SIZE and BLOCKSIZE environment variables.

You can use du -a or –all options to get disk space usage of all files and directories. By default, du prints only directory disk space usage.

You can use du -d DEPTH or –max-depth=DEPTH options to show the total for each directory (and file if -all) that is at most MAX_DEPTH levels down from the root of the hierarchy. The root is at level 0, so ‘du –max-depth=0’ is equivalent to ‘du -s’. The ‘-s’ or ‘–summarize’ option display only a total for each argument.

You can use du -c or –total options to print a grand total of all arguments after all arguments have been processed. This can be used to find out the total disk usage of a given set of files or directories.

You can use du -h or –human-readable options to append a size letter to each size, such as ‘M’ for mebibytes. Powers of 1024 are used, not 1000; ‘M’ stands for 1,048,576 bytes. This option is equivalent to ‘–block-size=human-readable’. Use the ‘–si’ option if you prefer powers of 1000.

You can use du –apparent-size or -b or –bytes options to print apparent sizes, rather than actual disk usage. apparent size is the actual size of the data in the file. For example, suppose a file contains only “hello” word, then the apparent size of the file is 6 (including newline) but when you want to save the file in the disk, it may require anywhere from 0 to 16 KiB or more of disk space, depending on the type and configuration of the file system on which the file resides. In general, apparent size is smaller than disk size in case of most files with some exception sparse files, internal fragmentation, indirect blocks, etc.

You can use du -B SIZE or –block-size=SIZE options to scale the size by SIZE before printing them. You can define SIZE in integers (1,1024 etc. or 1,1000 etc. unit in bytes) or in characters (K, M, G, T, P, E, Z, Y, si). You can also use du -k or -m options to display the size in KB or MB.

You can use du -S or –separate-dirs options to exclude size of any subdirectories when reporting the size of the directory itself.

You can use du –files0-from=FILE option to disallow processing files named on the command line, and instead process those named in file FILE; each name being terminated by a zero byte (ASCII NUL). This is useful when the list of filenames is so long that it may exceed a command line length limitation. In such cases, running ‘du’ via ‘xargs’ is undesirable because it splits the list into pieces and makes ‘du’ print with the ‘–total’ (‘-c’) option for each sublist rather than for the entire list. One way to produce a list of ASCII NUL terminated file names is with GNU ‘find’, using its ‘-print0’ predicate. If FILE is ‘-‘ then the ASCII NUL terminated file names are read from standard input.

You can use du -t SIZE or –threshold=SIZE options to exclude entries based on a given SIZE. The SIZE refers to used blocks in normal mode (*note Block size::), or inodes count in conjunction with the ‘–inodes’ option. If SIZE is positive, then du will only print entries with a size greater than or equal to that. If SIZE is negative, then ‘du’ will only print entries with a size smaller than or equal to that.

You can use du –exclude=PATTERN option to skip subdirectories or files matching PATTERN.

You can use du –exclude-from=FILE option is similar to –exclude option, except take the patterns to exclude from FILE, one per line. If FILE is ‘-‘, take the patterns from standard input.

You can use du –time or –time-style=STYLE options to display different timelines for the file or directories along with disk usage. –time-style=STYLE option is used with –time option to format the time using STYLE arguments which can be: full-iso, long-iso, iso, or +FORMAT; FORMAT is interpreted like in date command.

You can use du -D or -H or -L or -P options with -R option to control the behavior of the symbolic link.

  • -D or -H : follow the symbolic link provided as command-line argument, but do not follow any other symbolic link during traversing
  • -L : follow all symbolic links
  • -P : Do not follow any symbolic link

You can use du –inodes option to list inode usage information instead of block usage. This option is useful for finding directories that contain many files and therefore eat up most of the inodes space of a file system.

Hope you have enjoyed this article. In the next blog post, we will discuss df command in Linux.

 

Leave a Reply

Close Menu