🖥️du

➡️This is a command-line reference manual for commands and command combinations that you don’t use often enough to remember it. This cheatsheet explains the du command with important options and switches using examples.

▁ ▂ ▃ ▄ ꧁ 🔴☠ COMMANDLINE-KUNGFU WITH CHEATSHEETS ☠🔴꧂▅ ▃ ▂ ▁

#                ██████╗ ██╗   ██╗
#                ██╔══██╗██║   ██║
#                ██║  ██║██║   ██║
#                ██║  ██║██║   ██║
#                ██████╔╝╚██████╔╝
#                ╚═════╝  ╚═════╝ 
                                 
                                 
                               
# To sort directories/files by size
du -sk *| sort -rn

# To show cumulative humanreadable size
du -sh

#==============================#
# CMD DU - Discusage
#==============================##==============================#

# Howto get directory size in Linux
#########################################
du -sch /tmp/test/
du -ch /tmp 

# We will be using disk usage command ‘du’ and below of its options :

    -s : Display only summary of each element
    -h : Human readable format for size i.e. KB, MB, GB
    -c : Produce grand total i.e.  display total size

# Did you know most GNU programs can have their long options abbreviated? Ex: 'du --ap' same as 'du --apparent-size'. Must be unambiguous.

du -ma | sort -nr | head -n 20 
# List the 20 largest files or folders under the current working directory.

du -sm $(find /start/dir/* -type d -maxdepth 1 -xdev) | sort -g
# Directories and its size - which directories and trees take up all the diskspace?

du -ha /var | sort -n -r | head -n 10
# If you want more human readable output try:

du -h –max-depth=1 Command
# The below command outputs the size of sub-folders within the current directory, in human readable format.

du -ch –max-depth=1 * | sort -rh
#

du -h
# To show all directories size including sub directories, type

du -sh
# To calculate the current directory size you are in (-s stand for summary)

du -sh *
# To show all the 1 level sub directories size (which you are not interested at sub sub directories.)

du -sh /home
# To show the size of specific directory

du -sh /home/*
# To show the size of all sub directories of a specific directory

du --ap 
# Did you know most GNU programs can have their long options abbreviated? Long: du --apparent-size

du -sh */
#Space usage of directories only (This was the very first CLIMagic command posted on December 15th, 2009)

du -ma | sort -nr | head -n 20
# List the 20 largest files or folders under the current working directory.

du -sh */ | sort -h
# Show directory size and sort by human readable amount (MB, GB, etc.). Requires GNU sort for -h option.

du -h . | grep "^[0-9\.]\+G"
## Find out which of your directories(below the current directory) occupy at least 1GB of space.

du -cms .[^.]*/ */ | sort -rn | head
# Show the 10 largest directories at top level along with total usage. All in megabytes.

# Get an ordered list of subdirectory sizes This piece of code lists the size of every file and subdirectory of the current directory, much like du -sch ./* except the output is sorted by size, with larger files and directories at the end of the list. Useful to find where all that space goes. 
du -sk ./* | sort -n | awk 'BEGIN{ pref[1]="K"; pref[2]="M"; pref[3]="G";} { total = total + $1; x = $1; y = 1; while( x > 1024 ) { x = (x + 1023)/1024; y++; } printf("%g%s\t%s\n",int(x*10)/10,pref[y],$2); } END { y = 1; while( total > 1024 ) { total = (total + 1023)/1024; y++; } printf("Total: %g%s\n",int(total*10)/10,pref[y]); }'

du -sch /tmp/test/
# Howto get directory size in Linux

du -ch /tmp
# We will be using disk usage command ‘du’ and below of its options :
#   -s : Display only summary of each element
#   -h : Human readable format for size i.e. KB, MB, GB
#   -c : Produce grand total i.e.  display total size

du -ak afile shows file size in KB
# The above command will list files with their size in mb. Now you can apply logic to add the first column values to get total size using awk.

du -am afile shows file size in MB.
# likewise du -ag afile shows file size in GB.

du -s /tmp/* | sort -n
#

du -a /var | sort -n -r | head -n 10
#

##  How to find out which user consumes the maximum space?
du -s * | sort -nr | head -5
    1959288 user3
    906328  user2
    726000  user1
    560800  user4
# In my case, all the users home directory are under /home. So, I get into the /home directory, and fire the below command:
# du command gives the disk usage in kilo bytes.. When du is run on any given directory, it gives the disk usage of each and every file present in the directory and their sub-directories. The "-s" option in du gives only the usage in summary for a directory which is what ideally we need in this case. We need the summary of every directory's usage under /home and hence "*". sorting the output gives the top disk users in the beginning of the output. And 'head -5' gives us the top 5 users.

du -sh *
    4.1G    user3
    2.1G    user1
    100M    user2
# In order to get the disk usage in more understandable format, use -  The '-h' option gives the output in the form of KB, MB and GB.

du
# Subdirectory sizes

du -h
# Show sizes of subdirectories

du -h --max-depth=1
# Subdirectory sizes - Make the sizes more humanly readable - Display the sizes of just one level of subdirectories

du -h --summarize *
# Subdirectory sizes - Make the sizes more humanly readable - Summarize the elements of this directory, humanly

du --max-depth=1 -h | sort -hr
# Linux Sort Folders By Size

du -hsx * | sort -rh | head -6
# Explanation of commands and switches.
#    du – Estimate file space usages
#    -hsx – (-h) Human Readable Format, (-s) Summaries Output, (-x) One File Format, skip directories on other file format.
#    sort – Sort text file lines
#    -rh – (-r) Reverse the result of comparison, (-h) for compare human readable format.
#    head – output first n lines of file.

du -a /var/spool/ | sort -n -r | head -n 10
# find out top largest Files and Directories.

du -hsx * | sort -rh | head -10
# OR, If you want output in human readable format follow the below command.
#         du -h : Display sizes in human readable format.
#         du -s : Show total for each argument.
#         du -x : Skip directoried on different file system.
#         sort -r : Reverse the result of comparisons.
#         sort -h : Compare human readable numbers.
#         head -10 : Display the first 10 lines.
#         head -n10 : Display the first 10 lines.

du -h -d 2 ~ | awk '$1 ~ /G/' 
# -- show folders 2 levels deep over 1GB in the current user's home folder.

du -sh */ | sort -h 
# Show directory size and sort by human readable amount (MB, GB, etc.). Requires GNU sort for -h option.

du -sh [a-m]*/ --exclude=export/
# Measure the disk space used by directories starting with a-m, skip exports dir. #GNU version FIXED

du -sh -I export [a-m]*
# Measure the disk space used by directories AND FILES starting with a-m, skip export dir. #BSD version

du -shc *.png | tail -1 
# Display the total size used by PNG image files in the current directory

du --max-depth\=1 ~ | sort -n | tail -n10
# Bash onlinner of the day: list the 10 biggest folders under your home $  

du --max-depth=1 -x -k | sort -n | awk 'function human(x) { s="KMGTEPYZ"; while (x>=1000 && length(s)>1) {x/=1024; s=substr(s,2)} return int(x+0.5) substr(s,1,1)"iB" } {gsub(/^[0-9]+/, human($1)); print}'
# List sub dir, sort by size, the biggest at the end, with human presentation i'm using -x : -x, --one-file-system skip directories on different file systems so mounts points aren't walked trough

# Linux Sort Folders By Size
du --max-depth=1 -h | sort -hr

# This piece of code lists the size of every file and subdirectory of the current directory, much like du -sch ./* except the output is sorted by size, with larger files and directories at the end of the list. Useful to find where all that space goes.
du -sk ./* | sort -n | awk 'BEGIN{ pref[1]="K"; pref[2]="M"; pref[3]="G";} { total = total + $1; x = $1; y = 1; while( x > 1024 ) { x = (x + 1023)/1024; y++; } printf("%g%s\t%s\n",int(x*10) y = 1; while( total > 1024 ) { total = (total + 1023)/1024; y++; } printf("Total: %g%s\n",int(total*10)/10,pref[y]); }'

du -ma | sort -nr | head -n 20 
# List the 20 largest files or folders under the current working directory.

du -cms .[^.]*/ */ | sort -rn | head 
# Show the 10 largest directories at top level along with total usage. All in megabytes. Thx @listrophy

du -sh */ | sort -h 
# Show directory size and sort by human readable amount (MB, GB, etc.). Requires fairly recent version of GNU sort.

find / -size +100M -exec du -h {} \; 
# find all files larger than 100MB and display their human readable size.

# Calculate the total disk space used by a list of files or directories
du -s file1 dir1 | awk '{sum += $1} END {print sum}'
# Explanation: 
    # This is really simple, the first column is the size of the file or the directory, which we sum up with awk and print the sum at the end.
    # Use du -sk to count in kilobytes, du -sm to count in megabytes (not available in some systems)

## Alternative one-liners: Calculate the total disk space used by a list of files or directories
du -c
# Explanation: -c option of du prints the total size of the arguments

# Calculate the total disk space used by a list of files or directories
du -cshx ./a ./b
# Explanation: 
    # -s, --summarize; display only a total for each argument
    # -c, --total; produce a grand total
    # -x, --one-file-system; skip directories on different file systems
    # -h, --human-readable; print sizes in human readable format (e.g., 1K 234M 2G)

# Calculate the total disk space used by a list of files or directories
du -cshx ./a ./b
# Explanation: 
    # -s, --summarize; display only a total for each argument
    # -c, --total; produce a grand total
    # -x, --one-file-system; skip directories on different file systems
    # -h, --human-readable; print sizes in human readable format (e.g., 1K 234M 2G)

## Related one-liners

# Calculate the total disk space used by a list of files or directories
du -s file1 dir1 | awk '{sum += $1} END {print sum}'
# Explanation: 
    # This is really simple, the first column is the size of the file or the directory, which we sum up with awk and print the sum at the end.
    # Use du -sk to count in kilobytes, du -sm to count in megabytes (not available in some systems)

# Sort du output in Human-readable format
du -hsx * | sort -rh

# Explanation: sort supports -h for human readable number sorting.
# Limitations: Probably a newer GNU only option for sort. :D

find / -maxdepth 1 -mindepth 1 -type d \! -empty \! -exec mountpoint -q {} \; -exec du -xsh {} + | sort -h
# Shows space used by each directory of the root filesystem excluding mountpoints/external filesystems (and sort the output) Excludes other mountpoints with acavagni's "mountpoint" idea, but with -exec instead of piping to an xargs subshell. Then, calling "du" only once with -exec's "+" option. The first "\! -exec" acts as a test so only those who match are passed to the second "-exec" for du. Show Sample Output:

find / -maxdepth 1 -mindepth 1 -type d -exec du -skx {} \; | sort -n
# Shows space used by each directory of the root filesystem excluding mountpoints/external filesystems (and sort the output)
        # [root@docker ~]# grep -vE '^\s*(#|$)' /etc/nsswitch.conf
        # passwd:     files sss
        # shadow:     files sss
        # group:      files sss
        # hosts:      files dns myhostname
        # bootparams: nisplus [NOTFOUND=return] files
        # ethers:     files
        # netmasks:   files
        # networks:   files
        # protocols:  files
        # rpc:        files
        # services:   files sss
        # netgroup:   nisplus sss
        # publickey:  nisplus
        # automount:  files nisplus sss
        # aliases:    files nisplus

find / -maxdepth 1 -type d | xargs -I {} sh -c "mountpoint -q {} || du -sk {}" | sort -n
# Shows space used by each directory of the root filesystem excluding mountpoints/external filesystems (and sort the output) Useful to see at glance which directory under the root file is using most space Show Sample Output:
        # [root@server ~]# df -h
        # Filesystem               Size  Used Avail Use% Mounted on
        # /dev/mapper/vm-root       18G  7.8G   11G  44% /
        # devtmpfs                 1.3G     0  1.3G   0% /dev
        # tmpfs                    1.3G     0  1.3G   0% /dev/shm
        # tmpfs                    1.3G  9.5M  1.3G   1% /run
        # tmpfs                    1.3G     0  1.3G   0% /sys/fs/cgroup
        # /dev/sda1               1014M  287M  728M  29% /boot

        # [root@server ~]# find / -maxdepth 1 -type d | xargs -I {} sh -c "mountpoint -q {} || du -sk {}" | sort -n
        # 0       /mnt
        # 0       /srv
        # 0       /vbox_share
        # 212     /tmp
        # 256     /root
        # 48856   /etc
        # 72692   /media
        # 177668  /opt
        # 933676  /home
        # 2238568 /var
        # 4586604 /usr

du -sh *
# List the size (in human readable form) of all sub folders from the current location -s summary -h human slightly different : will display side by side the sizes of individual files and of directories in current dir will not display total size of './'

du -h -d1
# List the size (in human readable form) of all sub folders from the current location Simple and easy to remember. -h is human, -d1 = depth 1. disk usage, human, depth 1 Show Sample Output:

du -h --max-depth=1 --one-file-system /
# du command without showing other mounted file systems

# Linux Sort Folders By Size
du --max-depth=1 -h | sort -hr

#==============================##==============================#
# CMD du						       #
#==============================##==============================#
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░

  █║▌│║█║▌★ KALI ★ PARROT ★ DEBIAN 🔴 PENTESTING ★ HACKING ★ █║▌│║█║▌

              ██╗ ██╗ ██████╗  ██████╗ ██╗  ██╗███████╗██████╗
             ████████╗██╔══██╗██╔═══██╗╚██╗██╔╝██╔════╝██╔══██╗
             ╚██╔═██╔╝██║  ██║██║   ██║ ╚███╔╝ █████╗  ██║  ██║
             ████████╗██║  ██║██║   ██║ ██╔██╗ ██╔══╝  ██║  ██║
             ╚██╔═██╔╝██████╔╝╚██████╔╝██╔╝ ██╗███████╗██████╔╝
              ╚═╝ ╚═╝ ╚═════╝  ╚═════╝ ╚═╝  ╚═╝╚══════╝╚═════╝

               █║▌│║█║▌ WITH COMMANDLINE-KUNGFU POWER █║▌│║█║▌

░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░