Softpanorama

May the source be with you, but remember the KISS principle ;-)
Home Switchboard Unix Administration Red Hat TCP/IP Networks Neoliberalism Toxic Managers
(slightly skeptical) Educational society promoting "Back to basics" movement against IT overcomplexity and  bastardization of classic Unix

du command tutorial

News Classic Unix Utilities Recommended Links Syntax Options Examples Reference
Find largest files in Linux sort tr cat  Pipes Humor Etc

Introduction

Disk Usage - report the amount of disk space used by the specified files and/or for each subdirectory. Compatibility between Unix versions is limited.  In linux there is also ncurses-based version ncdu. Here we will cover GNU version of du.

Default size reporting is in kilobytes. Utility du counts blocks so if a file is, say, 50 bytes du reports it as 4K if filesystem has this size of blocks.

With no arguments, 'du' reports the disk space for the current directory. Normally the disk space is printed in units of 1024 bytes, but this can be overridden.  Argument can be a shell-pattern (but not a regular expression).

For example

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

Option -c in the example above produces a grand total.  Option -m produces sizes in megabytes.

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

Option -h in the example above produces "human readable" sizes.

GNU version of du can work as filter. you need to translate EOL characters to \0 in order to use it this way.

For example:

cat file | tr '\n' '\0' | du --files0-from - 

 

Determining a size of the directory using du

The most important option is -s (summarize) which allow to get the size of particular directory. For example

du -sh /var 
du -sh *
The most important option is -s (summarize) which allow to get the size of particular directory

Syntax

du [options]... [file]...
du [OPTION]... --files0-from=F 

du is unable to read the list of files from stdin, but with option --files0-from can read the list from a file. this way you can list sizes (and with option -c summaries sizes) of arbitrary set of files, selected for example by find:

du -kc --files0-from <( find /root -type f -print0)

You can exclude some files from the selection

du -kc --exclude "/*[B-D]*" --files0-from <( find /root -type f -print0)

Options

Mandatory arguments to long options are mandatory for short options too.

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 holes in ('sparse') files, internal fragmentation, indirect blocks, and the like
-B, --block-size=SIZE  use SIZE-byte blocks
-b, --bytes
equivalent to '--apparent-size --block-size=1'
-c, --total
produce a grand total
-D, --dereference-args
dereference FILEs that are symbolic links
--files0-from=F 
summarize disk usage of the NUL-terminated file names specified in file F
-H
like --si, but also evokes a warning; will soon change to be equivalent to --dereference-args (-D)
-h, --human-readable
print sizes in human readable format (e.g., 1K 234M 2G)
--si
like -h, but use powers of 1000 not 1024
-k
like --block-size=1K
-l, --count-links
count sizes many times if hard linked
-m
like --block-size=1M
-L, --dereference
dereference all symbolic links
-P, --no-dereference
don't follow any symbolic links (this is the default)
-0, --null
end each output line with 0 byte rather than newline
-S, --separate-dirs
do not include size of subdirectories
-s, --summarize
display only a total for each argument
-x, --one-file-system
skip directories on different file systems
-X FILE, --exclude-from=FILE 
Exclude files that match any pattern in FILE.
--exclude=PATTERN  Exclude files that match PATTERN.
--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
--time
show time of the last modification of any file in the directory, or any of its subdirectories
--time=WORD 
show time as WORD instead of modification time: atime, access, use, ctime or status
--time-style=STYLE  show times using style STYLE:
full-iso, long-iso, iso, +FORMAT FORMAT is interpreted like 'date'
--help
display this help and exit
--version
output version information and exit

SIZE may be (or may be an integer optionally followed by) one of following: kB 1000, K 1024, MB 1000*1000, M 1024*1024, and so on for G, T, P, E, Z, Y.

Examples

Note: In most examples the result of du command are sorted in reverse numeric key order (options -rn of Unix sort).

Get total space of home directory

# du -ks /home
314476  /home

Get total amount of space used in all filesystems of the server:

du -ak --max-depth 0 / 
34842333 /

To list all the directories with above 1M size

du -h | grep "[0-9]M" | sort -rn

List files above 1G

find / -type f -exec du -h {} \; | egrep "^([0-9]+\.)?[0-9]+G"

List the total  sizes of all root directories

du -ak --max-depth 1 / | sort -rn
34842269        /
17663172        /backup
5605112 /opt
5516632 /var
3093684 /usr
1097148 /u01
899349  /media
333720  /dev
232084  /lib
167384  /home
152676  /etc
36808   /sbin
22492   /lib64
12180   /boot
7956    /bin
1548    /root
176     /tmp
100     /tftpboot
16      /lost+found
8       /srv
8       /selinux
8       /mnt
0       /sys
0       /proc
0       /net
0       /misc
0       /.autorelabel
0       /.autofsck
PATTERN is a shell pattern (not a regular expression). The pattern ? matches any one character, whereas * matches any string (composed of zero, one or multiple characters). For example, *.o will match any files whose names end in .o. Therefore, the command
du --exclude=aq*.oaq

will skip all files and subdirectories ending in .o (including the file .o itself).


Top Visited
Switchboard
Latest
Past week
Past month

NEWS CONTENTS

Old News ;-)

[Mar 26, 2015] Tip How to sort folders by size with one command line in Linux

May 14th, 2006 | MDLog-sysadmin
du --max-depth=1 /home/ | sort -n -r
Jassim Mishal
du -sh * | sort -h
David Sveningsson
du -h --summarize * | sort -h

I do not know when coreutils added --summarize, but it displays the total size for the target. sort also has a -h flag to sort "human readable numbers" e.g. 1.4G.

I know this is quite an old post but I think people should learn about the new flags.

phenno
du -h --max-depth=1 /home/ | sort -h -r 
will print the total size of each folder
under the /home/directory in human readable values (K, M, G) and sort -h -r
will sort them taking human readable values into account and will display the
results in reverse order from largest fodler to smallest. If u want largest folders
last use only sort -h without -r

deice

I'd been wondering how to get a sorted, human readable listing with du, and finally figured out a fairly short way to do it:
du -s /* | sort -n | cut -f 2- | while read a; do du -sh "$a"; done
This prints a sorted list in human-readable format, only problem with it is that the du is essentially done twice. That is not a major problem however as caching makes the second run really fast.

Jason

du -sh * |grep G
 or 
du -sh * |grep M
gives fast result
Jason.
http://linux-tweaks.blogspot.c...

Marcos

I usually use something similar, but smaller to type:
du -sc /* | sort -nr
It is important to have the * because of the -s flag.

But remember that with this construction you miss the hidden files (files starting with a dot), because the shell-expansion of * doesn't catch them.

[Mar 26, 2015] Total size of the contents of all the files in a directory

Dec 28, 2012 | Stack Overflow

Q: When I use ls or du I get the amount of disk space each file is occupying. I need the sum total of all the data in files and subdirectories I would get if I opened each file and counted the bytes. Bonus points If i can get this with out opening each file and counting.

Arthur Ulfeldt

ls actually shows the number of bytes in each file, not the amount of disk space. Is this sufficient for your needs? – Greg Hewgill Aug 6 '09 at 22:21

If you want the 'apparent size' (that is the number of bytes in each file), not size taken up by files on the disk, use the -b or --bytes option (if you got a Linux system with GNU coreutils):

% du -shb <directory>

Arkady

works on my newer red hat boxes, unfortunately not on my embedded Dev box. – Arthur Ulfeldt Aug 6 '09 at 22:34


2

Is there an easy way to show the "apparent size" in human-readable format? When using du -shb (as suggested by this answer), the -b setting seems to override the -h setting. – Mathias Bynens Aug 1 '12 at

@MathiasBynens Reverse the order of the flags (i.e. du -sbh <dir>). Works for me. – Luis E. May 30 '13 at 7:52

@MathiasBynens

du -sh --apparent-size /dir/

Jongosi Mar 15 at 14:41

use du -sb
du -sb DIR

Optionally add the h option for more user-friendly output
du -sbh DIR

Just an alternative:
$ ls -lR | grep -v '^d' | awk '{total += $5} END {print "Total:", total}'

grep -v '^d' will exclude the directories.


shareimprove this answer


answered Aug 12 '09 at 16:20


Barun

Perfect, also add the -a param to get "hidden files" (anything starting with a period) – Nicholi Apr 20 '11 at 20:02

Isolated to a specific file type (in this case, PNG) and expressed in MB for more readability: ls -lR | grep '.png$' | awk '{total += $5} END {print "Total:", total/1024/1024, "MB"}' –

MusikPolice Sep 9 '14 at 15:11

stat's "%s" format gives you the actual number of bytes in a file.

find . -type f | xargs stat --format=%s | awk '{s+=$1} END {print s}'
Feel free to substitute your favourite method for summing numbers. shareimprove this answer answered Aug 6 '09 at 22:16 Nelson

   

Preferably use "find . -type f -print0 | xargs -0 ..." to avoid problems with certain file names (containing spaces etc). – hlovdal Aug 6 '09 at 22:23

yeah, good point. if it wasn't in bsd 4.2 I don't remember to use it :-( – Nelson Aug 6 '09 at 22:24


1

find -print0 and xargs -0 are needed for filenames with spaces.

arsane

How about:

$ du -ckx | grep total | awk '{print $1}'


Where is the directory you want to inspect.

The '-c' gives you grand total data which is extracted using the 'grep total' portion of the command, and the count in Kbytes is extracted with the awk command.

The only caveat here is if you have a subdirectory containing the text "total" it will get spit out as well.


shareimprove this answer

bash - Get total size of folders with find & du

Stack Overflow

I'm trying to get the size of the directories named "bak" with find and du.

I do that :

find -name bak -type d -exec du -ch '{}' \;

But it returns the size for each folder named "bak" not the total.

Anyway to get them ? Thanks :)

Piokaz

I recommend using awk to compute the end sum (using du without -h) – Alex Mar 20 '12 at 21:01

@Alex, why would that be preferred? – Carl Norum Mar 20 '12 at 21:15

Use xargs(1) instead of -exec:

find . -name bak -type d | xargs du -ch

-exec executes the command for each file found (check the find(1) documentation).

Piping to xargs lets you aggregate those filenames and only run du once.

You could also do:

find -name bak -type d -exec du -ch '{}' \; +

If your version of find supports it.

[Mar 26, 2015] How to find - Size of a directory & Free disk space

Tips For Linux

$ du
Typing the above at the prompt gives you a list of directories that exist in the current directory along with their sizes. The last line of the output gives you the total size of the current directory including its subdirectories. The size given includes the sizes of the files and the directories that exist in the current directory as well as all of its subdirectories. Note that by default the sizes given are in kilobytes.

... ... ...

$ du -c
This gives you a grand total as the last line of the output. So if your directory occupies 30MB the last 2 lines of the output would be

30M .
30M total

The first line would be the default last line of the 'du' output indicating the total size of the directory and another line displaying the same size, followed by the string 'total'. This is helpful in case you this command along with the grep command to only display the final total size of a directory as shown below.


$ du -ch | grep total
This would have only one line in its output that displays the total size of the current directory including all the subdirectories.

Note : In case you are not familiar with pipes (which makes the above command possible) refer to Article No. 24 . Also grep is one of the most important commands in Unix. Refer to Article No. 25 to know more about grep.


$ du -s
This displays a summary of the directory size. It is the simplest way to know the total size of the current directory.


$ du -S
This would display the size of the current directory excluding the size of the subdirectories that exist within that directory. So it basically shows you the total size of all the files that exist in the current directory.


$ du --exclude=mp3
The above command would display the size of the current directory along with all its subdirectories, but it would exclude all the files having the given pattern present in their filenames. Thus in the above case if there happens to be any mp3 files within the current directory or any of its subdirectories, their size would not be included while calculating the total directory size.

[Jan 31, 2015] Linux Find Out Directory Size Command

cyberciti.biz

Pass the -s option to see the total disk space used by a directory:
du -sh
du -sh /etc/
du -sh /etc /home/ /securebackup/

Sample outputs:

4.1M	/etc
152K	/home/
902M	/securebackup/

Pass the -c to see a grand total for all of the files, type:
du -csh /root/ /etc/ /home/
Sample outputs:

2.4M	/root/
4.1M	/etc/
152K	/home/
6.6M	total
See also

[Dec 05, 2013] Disk usage analysis and cleanup tools By Mayank Sharma

February 01, 2006 | linux.com

You can use the du command to determine which files or directories need to be deleted -- or at least trimmed. A simple du will print usage for the present working directory and its subdirectories, along with the size of each directory.

If you want the size of an particular directory, specify it with du directoryname . For instance, du -h /home/bodhi/podcasts will print the size of the podcasts directory in a more readable format than the kilobytes used by default. The -c option prints the grand total size of the directory at the end. The -a option also displays the file names along with directories and can be of use when you want to see a list of files in a particular directory. The -s option will display a summary, without showing all of the subdirectories.

Running du -ch | grep total prints just one line with the total size of the directory. If there's a particular type of file that you would like to be excluded while calculating a directory's usage, specify it with the --exclude=type option. Here we'll check the disk usage of the current directory, and display all file names with their disk usage, and then sort them numerically using the sort utility:

$ du -ah | sort -n4.2M
       ./eweek.10.28.05.mp34.5M
       ./LQ-Podcast-101105.mp34.8M
       ./LQ-Podcast-110905.mp319M
         ./LQRadio-Episode3.mp320M
         ./LQRadio-Searls.mp336M
         ./LQRadio-HiserAndAdelstein.mp3197M
       .

[Feb 11, 2011] Linux Shell Script Monitoring Activities

Packt Publishing Technical & IT Book Store

Finding the 10 largest size files from a given directory

Finding large-size files is a regular task we come across. We regularly require to delete those huge size files or move them. We can easily find out large-size files using du and sort commands. The following one-line script can achieve this task:

$ du -ak SOURCE_DIR | sort -nrk 1 | head

Here -a specifies all directories and files. Hence du traverses the SOURCE_DIR and calculates the size of all files. The first column of the output contains the size in Kilobytes since -k is specified and the second column contains the file or folder name.

sort is used to perform numerical sort with column 1 and reverse it. head is used to parse the first 10 lines from the output.

For example:

$ du -ak /home/slynux | sort -nrk 1 | head -n 4
50220 /home/slynux
43296 /home/slynux/.mozilla
43284 /home/slynux/.mozilla/firefox
43276 /home/slynux/.mozilla/firefox/8c22khxc.default

One of the drawbacks of the above one-liner is that it includes directories in the result. However, when we need to find only the largest files and not directories we can improve the one-liner to output only the large-size files as follows:

find . -type f -exec du -k {} \; | sort -nrk 1 | head

We used find to filter only files to du rather than allow du to traverse recursively by itself.


Ncdu a NCurses Based Disk Usage Analyzer and Tracker by Ravi Saive

May 7, 2013

ncdu (NCurses Disk Usage) is a command line version of the most popular "du command". It is based on ncurses and provides a fastest way to analyse and track what files and directories are using your disk space in Linux. It provides an excellent ncurses based interface to display the information in more intuitive way like columns for how much disk space used in megabytes, gigabytes and graphical bar usage, file/directory names, file deletion, refresh, etc. ncdu aims to be simple, fast and easy to use program and runs on any minimal Linux/Unix based system with ncurses installed.

... ... ...

The "ncdu" package is not available under RHEL, CentOS, Fedora, Scientific Linux distributions, you must have epel repository enabled on your system to install it using yum command.

... ... ...

Once, scanning completes, it will present the tree structure of files and folders along with their disk usage in human readable format with graphical bar presentation.

...Once, scanning completes, it will present the tree structure of files and folders along with their disk usage in human readable format with graphical bar presentation.

...Press "-d" to delete selected file or directory, before deleting it will prompt you for confirmation. Press "Yes" or "No".

...Press "Shift+?" to see help window with ncdu available options. You can use arrow keys to move up and down for more options.

Recommended Links

Google matched content

Softpanorama Recommended

Top articles

Sites




Etc

Society

Groupthink : Two Party System as Polyarchy : Corruption of Regulators : Bureaucracies : Understanding Micromanagers and Control Freaks : Toxic Managers :   Harvard Mafia : Diplomatic Communication : Surviving a Bad Performance Review : Insufficient Retirement Funds as Immanent Problem of Neoliberal Regime : PseudoScience : Who Rules America : Neoliberalism  : The Iron Law of Oligarchy : Libertarian Philosophy

Quotes

War and Peace : Skeptical Finance : John Kenneth Galbraith :Talleyrand : Oscar Wilde : Otto Von Bismarck : Keynes : George Carlin : Skeptics : Propaganda  : SE quotes : Language Design and Programming Quotes : Random IT-related quotesSomerset Maugham : Marcus Aurelius : Kurt Vonnegut : Eric Hoffer : Winston Churchill : Napoleon Bonaparte : Ambrose BierceBernard Shaw : Mark Twain Quotes

Bulletin:

Vol 25, No.12 (December, 2013) Rational Fools vs. Efficient Crooks The efficient markets hypothesis : Political Skeptic Bulletin, 2013 : Unemployment Bulletin, 2010 :  Vol 23, No.10 (October, 2011) An observation about corporate security departments : Slightly Skeptical Euromaydan Chronicles, June 2014 : Greenspan legacy bulletin, 2008 : Vol 25, No.10 (October, 2013) Cryptolocker Trojan (Win32/Crilock.A) : Vol 25, No.08 (August, 2013) Cloud providers as intelligence collection hubs : Financial Humor Bulletin, 2010 : Inequality Bulletin, 2009 : Financial Humor Bulletin, 2008 : Copyleft Problems Bulletin, 2004 : Financial Humor Bulletin, 2011 : Energy Bulletin, 2010 : Malware Protection Bulletin, 2010 : Vol 26, No.1 (January, 2013) Object-Oriented Cult : Political Skeptic Bulletin, 2011 : Vol 23, No.11 (November, 2011) Softpanorama classification of sysadmin horror stories : Vol 25, No.05 (May, 2013) Corporate bullshit as a communication method  : Vol 25, No.06 (June, 2013) A Note on the Relationship of Brooks Law and Conway Law

History:

Fifty glorious years (1950-2000): the triumph of the US computer engineering : Donald Knuth : TAoCP and its Influence of Computer Science : Richard Stallman : Linus Torvalds  : Larry Wall  : John K. Ousterhout : CTSS : Multix OS Unix History : Unix shell history : VI editor : History of pipes concept : Solaris : MS DOSProgramming Languages History : PL/1 : Simula 67 : C : History of GCC developmentScripting Languages : Perl history   : OS History : Mail : DNS : SSH : CPU Instruction Sets : SPARC systems 1987-2006 : Norton Commander : Norton Utilities : Norton Ghost : Frontpage history : Malware Defense History : GNU Screen : OSS early history

Classic books:

The Peter Principle : Parkinson Law : 1984 : The Mythical Man-MonthHow to Solve It by George Polya : The Art of Computer Programming : The Elements of Programming Style : The Unix Hater’s Handbook : The Jargon file : The True Believer : Programming Pearls : The Good Soldier Svejk : The Power Elite

Most popular humor pages:

Manifest of the Softpanorama IT Slacker Society : Ten Commandments of the IT Slackers Society : Computer Humor Collection : BSD Logo Story : The Cuckoo's Egg : IT Slang : C++ Humor : ARE YOU A BBS ADDICT? : The Perl Purity Test : Object oriented programmers of all nations : Financial Humor : Financial Humor Bulletin, 2008 : Financial Humor Bulletin, 2010 : The Most Comprehensive Collection of Editor-related Humor : Programming Language Humor : Goldman Sachs related humor : Greenspan humor : C Humor : Scripting Humor : Real Programmers Humor : Web Humor : GPL-related Humor : OFM Humor : Politically Incorrect Humor : IDS Humor : "Linux Sucks" Humor : Russian Musical Humor : Best Russian Programmer Humor : Microsoft plans to buy Catholic Church : Richard Stallman Related Humor : Admin Humor : Perl-related Humor : Linus Torvalds Related humor : PseudoScience Related Humor : Networking Humor : Shell Humor : Financial Humor Bulletin, 2011 : Financial Humor Bulletin, 2012 : Financial Humor Bulletin, 2013 : Java Humor : Software Engineering Humor : Sun Solaris Related Humor : Education Humor : IBM Humor : Assembler-related Humor : VIM Humor : Computer Viruses Humor : Bright tomorrow is rescheduled to a day after tomorrow : Classic Computer Humor

The Last but not Least Technology is dominated by two types of people: those who understand what they do not manage and those who manage what they do not understand ~Archibald Putt. Ph.D


Copyright © 1996-2021 by Softpanorama Society. www.softpanorama.org was initially created as a service to the (now defunct) UN Sustainable Development Networking Programme (SDNP) without any remuneration. This document is an industrial compilation designed and created exclusively for educational use and is distributed under the Softpanorama Content License. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

FAIR USE NOTICE This site contains copyrighted material the use of which has not always been specifically authorized by the copyright owner. We are making such material available to advance understanding of computer science, IT technology, economic, scientific, and social issues. We believe this constitutes a 'fair use' of any such copyrighted material as provided by section 107 of the US Copyright Law according to which such material can be distributed without profit exclusively for research and educational purposes.

This is a Spartan WHYFF (We Help You For Free) site written by people for whom English is not a native language. Grammar and spelling errors should be expected. The site contain some broken links as it develops like a living tree...

You can use PayPal to to buy a cup of coffee for authors of this site

Disclaimer:

The statements, views and opinions presented on this web page are those of the author (or referenced source) and are not endorsed by, nor do they necessarily reflect, the opinions of the Softpanorama society. We do not warrant the correctness of the information provided or its fitness for any purpose. The site uses AdSense so you need to be aware of Google privacy policy. You you do not want to be tracked by Google please disable Javascript for this site. This site is perfectly usable without Javascript.

Last modified: March, 29, 2020