Hide

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.


If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Hide

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for:

Hide

News

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

Commands tagged du from sorted by
Terminal - Commands tagged du - 51 results
du -g | perl -ne 'print if (tr#/#/# == <maximum depth>)'
2014-02-15 07:33:36
User: RAKK
Functions: du perl
Tags: perl du unix aix
0

Lists directory size up to a maximum traversal depth on systems like IBM AIX, where the du command doesn't have Linux's --max-depth option. AIX's du uses -g to display directory size on gigabytes, -m to use megabytes, and -k to use kilobytes. tr### is a Perl function that replaces characters and returns the amount of changed characters, so in this case it will return how many slashes there were in the full path name.

du -Lsbc * |awk 'function hr(bytes){hum[1024**4]="TiB";hum[1024**3]="GiB";hum[1024**2]="MiB";hum[1024]="kiB";for(x=1024**4;x>=1024;x/=1024){if(bytes>=x){return sprintf("%8.3f %s",bytes/x,hum[x]);}}return sprintf("%4d B",bytes);}{print hr($1) "\t" $2}'
find . -type d -print0 | xargs -0 du -s | sort -n | tail -10 | cut -f2 | xargs -I{} du -sh {} | sort -rn
du -mx [directory] | grep -P '^\d{4}' | sort -rn
2013-05-24 09:52:41
User: mc0e
Functions: du grep sort
Tags: bash Linux du
0

I don't like doing a massive sort on all the directory names just to get a small set of them. the above shows a sorted list of all directories over 1GB. use head as well if you want.

du's "-x" flag limits this to one file system. That's mostly useful when you run it on "/" but don't want "/proc" and "/dev" and so forth. Remember though that it will also exclude "/home" or "/var" if those are separate partitions.

the "-a" option is often useful too, for listing large files as well as large directories. Might be slower.

du -xB M --max-depth=2 /var | sort -rn | head -n 15
largest() { dir=${1:-"./"}; count=${2:-"10"}; echo "Getting top $count largest files in $dir"; du -sx "$dir/"* | sort -nk 1 | tail -n $count | cut -f2 | xargs -I file du -shx file; }
2013-01-21 09:45:21
User: jhyland87
Functions: cut du echo file sort tail xargs
1

You can simply run "largest", and list the top 10 files/directories in ./, or you can pass two parameters, the first being the directory, the 2nd being the limit of files to display.

Best off putting this in your bashrc or bash_profile file

du -hd1 |sort -h
du -sm /home/* | sort -n | tail -10
echo $(find <directory> -name '*.<extension>' -exec du -s {} \; | tee $(tty) | cut -f1 | tr '\n' '+') 0 | bc
2012-09-17 22:46:50
User: ysangkok
Functions: cut du echo find tee tr
-1

Also shows files as they are found. Only works from a tty.

sudo -s du -sm /Users/* | sort -nr | head -n 10
2012-09-13 10:15:23
User: mematron
Functions: du head sort sudo
0

In OSX you would have to make sure that you "sudo -s" your way to happiness since it will give a few "Permission denied" errors before finally spitting out the results. In OSX the directory structure has to start with the "Users" Directory then it will recursively perform the operation.

Your Lord and master,

Mematron

du -sh /home/*|sort -rh|head -n 10
2012-09-12 11:54:06
User: toaster
Functions: du head sort
0

the -h option of du and sort (on appropriate distrib) makes output "Human" readable and still sorted by "reversed size" (sort -rh)

du -sm /home/* | sort -nr | head -n 10
for i in G M K; do du -hx /var/ | grep [0-9]$i | sort -nr -k 1; done | less
2012-06-26 22:57:17
User: jlaunay
Functions: du grep sort
Tags: du
1

This command give a human readable result without messing up the sorting.

du -x / | sort -rn | less
2012-06-26 15:29:26
User: harpo
Functions: du sort
Tags: du
2

I had the problem that our monitoring showed that the "/" filesystem is >90% full. This command helped me to find out fast which subdirs are the biggest. The system has many NFS-mounts therefore the -x.

find . -type f -print0 | xargs -0 du -h | sort -hr | head -20
2012-03-30 10:21:12
User: flatcap
Functions: du find head sort xargs
7

Search for files and list the 20 largest.

find . -type f

gives us a list of file, recursively, starting from here (.)

-print0 | xargs -0 du -h

separate the names of files with NULL characters, so we're not confused by spaces

then xargs run the du command to find their size (in human-readable form -- 64M not 64123456)

| sort -hr

use sort to arrange the list in size order. sort -h knows that 1M is bigger than 9K

| head -20

finally only select the top twenty out of the list

du -s $(ls -l | grep '^d' | awk '{print $9}') | sort -nr
du -sch *
2011-12-06 18:38:20
User: anarcat
Functions: du
Tags: space du disk
-1

All folders, human-readable, no subfolder, with a total. Even shorter.

du -sh * | sort -rh | head
2011-11-16 06:01:02
User: sirex
Functions: du sort
Tags: du
3

This command simply outputs 10 files in human readable, that takes most space on your disk in current directory.

du -h --max-depth=1 |sort -rh
2011-11-15 20:30:00
User: jambino
Functions: du sort
12

In this case I'm just grabbing the next level of subdirectories (and same level regular files) with the --max-depth=1 flag. leaving out that flag will just give you finer resolution. Note that you have to use the -h switch with both 'du' and with 'sort.'

du -h | sort -hr
du -sh `pwd`
2011-10-30 08:47:23
User: djkee
Functions: du
Tags: size du pwd
0

Shows the size of the directory the command is ran in.

The size is in MB and GB.

There is no need to type the path, its the current working directory.

du -scb
2011-06-27 14:20:11
User: bbbco
Functions: du
Tags: du
2

Even simpler! Use du ... the -s and -c flags summarize and print a grand total of all files recursively. The -b flag prints in byte format. You can use the -h flag instead to print in human readable format.

du --max-depth=1 -B M |sort -rn
2011-04-12 15:01:12
Functions: du sort
Tags: bash du sh
0

If you're only using -m or -k, you will need to remember they are either in Megabyte or kilobyte forms. So by using -B, it gives you the unit of the size measurement, which helps you from reading the result faster. You can try with -B K as well.

du -h --max-depth=1 | sort -hr
2011-04-07 18:01:18
User: splante
Functions: du sort
Tags: sort du
0

Credit goes to brun65i but he posted it as a comment instead as an alternative. I hadn't noticed the -h option on sort before and this seems like the cleanest alternative. Thanks Brun65i!

find . -name '*.xml' -type f -print | xargs du -ch
2011-03-22 00:47:42
User: nathwill
Functions: du find xargs
Tags: find du type
0

print sum of disk usage for filetype within current dir and subdirs