du – estimate and summarize file and directory space usage on Linux

This du command guide is a follow-up of my previous 90 Linux Commands frequently used by Linux Sysadmins article. Every week, or as time allows, I will publish articles on the ~ 90 commands geared toward Linux sysadmins and Linux power users. Let’s continue this series with the du command.

du (disk usage) command is a standard Unix program used to estimate file space used under a particular directory or files on a file system. du takes a single argument, specifying a pathname for du to work.  If the path is not given, the working directory is used. On Linux, some of the available du options are:

  • -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 internal fragmentation, indirect blocks, etc.
  • -B, --block-size=SIZE – scale sizes by SIZE before printing them. (e.g., ‘-BM’ prints sizes in units of 1,048,576 bytes.)
  • -b, --bytes – equivalent to ‘--apparent-size --block-size=1
  • -c, --total – produce a grand-total.
  • -D, --dereference-args – dereference only symlinks that are listed on the command line.
  • -d, --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.
  • --files0-from=F – summarize disk usage of the NUL-terminated file names specified in file F; if F is -, then read names from standard input.
  • -h, --human-readable – print sizes in human-readable format (e.g., 1K 234M 2G).
  • --inodes – list inode usage information instead of block usage.
  • -k like --block-size=1K
  • -L, --dereference – dereference all symbolic links.
  • -l, --count-links – count sizes many times if hard-linked.
  • -m like --block-size=1M
  • -P, --no-dereference – don’t follow any symbolic links (this is the default).
  • -S, --separate-dirs – for directories, do not include the size of subdirectories.

 

du command examples

du disk usage command examples

To list the sizes of a directory and any subdirectories in the given unit (B/KB/MB), use:

du -b|k|m path/to/directory

To list the sizes of a directory and any subdirectories in human-readable form (i.e., auto-selecting the appropriate unit for each size), use:

du -h path/to/directory

To show the size of a single directory, in human-readable units, use:

du -sh path/to/directory

To list the human-readable sizes of a directory and of all the files and directories within it, use:

du -ah path/to/directory

To list the human-readable sizes of a directory and any subdirectories, up to N levels deep, use:

du -h --max-depth=N path/to/directory

To list the human-readable size of all .jpg files in subdirectories of the current directory and show a cumulative total at the end, use:

du -ch */*.jpg

 

du command useful reading:

du alternatives and related commands:

  • ncdu – a disk utility for Unix systems.
  • df – Report disk usage by filesystems.

Tags: , ,

Discussion

  1. I think, du command makes more sense when used with sort command. I generally use it like that:

    du -had 1 | sort -hr

    or in long written form;

    du --human-readable --all --max-depth=1 | sort --human-numeric-sort --reverse

  2. Thanks for the tip. As you know, we can shorten it to du-sort:
    alias du-sort='du -had 1 | sort -hr'

    And also consider ncdu and gdu.



Top ↑