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 using du from sorted by
Terminal - Commands using du - 196 results
du -sm *| sort -nr | awk '{ size=4+5*int($1/5); a[size]++ }; END { print "size(from->to) number graph"; for(i in a){ printf("%d %d ",i,a[i]) ; hist=a[i]; while(hist>0){printf("#") ; hist=hist-5} ; printf("\n")}}'
2014-08-19 14:43:20
User: higuita
Functions: awk du sort
Tags: awk
0

This command makes a small graph with the histogram of size blocks (5MB in this example), not individual files. Fine tune the 4+5*int($1/5) block for your own size jumps : jump-1+jump*($1/jump)

Also in the hist=hist-5 part, tune for bigger or smaller graphs

bkname="test"; tobk="*" ; totalsize=$(du -csb $tobk | tail -1 | cut -f1) ; tar cvf - $tobk | tee >(sha512sum > $bkname.sha512) >(tar -tv > $bkname.lst) | mbuffer -m 4G -P 100% | pv -s $totalsize -w 100 | dd of=/dev/nst0 bs=256k
2014-07-22 15:47:50
User: johnr
Functions: cut dd du tail tar tee
0

This will write to TAPE (LTO3-4 in my case) a backup of files/folders. Could be changed to write to DVD/Blueray.

Go to the directory where you want to write the output files : cd /bklogs

Enter a name in bkname="Backup1", enter folders/files in tobk="/home /var/www".

It will create a tar and write it to the tape drive on /dev/nst0.

In the process, it will

1) generate a sha512 sum of the tar to $bkname.sha512; so you can validate that your data is intact

2) generate a filelist of the content of the tar with filesize to $bkname.lst

3) buffer the tar file to prevent shoe-shining the tape (I use 4GB for lto3(80mb/sec), 8gb for lto4 (120mb/sec), 3Tb usb3 disks support those speed, else I use 3x2tb raidz.

4) show buffer in/out speed and used space in the buffer

5) show progress bar with time approximation using pv

ADD :

To eject the tape :

; sleep 75; mt-st -f /dev/nst0 rewoffl

TODO:

1) When using old tapes, if the buffer is full and the drive slows down, it means the tape is old and would need to be replaced instead of wiping it and recycling it for an other backup. Logging where and when it slows down could provide good information on the wear of the tape. I don't know how to get that information from the mbuffer output and to trigger a "This tape slowed down X times at Y1gb, Y2gb, Y3gb down to Zmb/s for a total of 30sec. It would be wise to replace this tape next time you want to write to it."

2) Fix filesize approximation

3) Save all the output to $bkname.log with progress update being new lines. (any one have an idea?)

4) Support spanning on multiple tape.

5) Replace tar format with something else (dar?); looking at xar right now (https://code.google.com/p/xar/), xml metadata could contain per file checksum, compression algorithm (bzip2, xv, gzip), gnupg encryption, thumbnail, videopreview, image EXIF... But that's an other project.

TIP:

1) You can specify the width of the progressbar of pv. If its longer than the terminal, line refresh will be written to new lines. That way you can see if there was speed slowdown during writing.

2) Remove the v in tar argument cvf to prevent listing all files added to the archive.

3) You can get tarsum (http://www.guyrutenberg.com/2009/04/29/tarsum-02-a-read-only-version-of-tarsum/)

and add >(tarsum --checksum sha256 > $bkname_list.sha256) after the tee to generate checksums of individual files !

du -sch .[!.]* * |sort -h
2014-07-17 21:45:46
User: krizzo
Functions: du sort
0

This command sorts the content including hidden files in human readable format of the current directory.

du -ch `cat test_file.txt`
2014-06-26 08:59:05
User: mase
Functions: du
0

in test_file.txt:

/var/img/1/368/ID_2012MTH03DT15.zip

/var/img/1/368/ID_2012MTH03DT16.zip

/var/img/1/368/ID_2012MTH03DT16N.zip

/var/img/1/368/ID_2012MTH03DT16E.zip

/var/img/1/368/ID_2012MTH03DT17M.zip

/var/img/1/368/ID_2012MTH03DT17N.zip

/var/img/1/368/ID_2012MTH03DT17E.zip

/var/img/1/368/ID_2012MTH03DT18M.zip

/var/img/1/368/ID_2012MTH03DT18N.zip

/var/img/1/368/ID_2012MTH03DT19M.zip

/var/img/1/368/ID_2012MTH03DT19N.zip

/var/img/1/368/ID_2012MTH03DT19E.zip

/var/img/1/368/ID_2012MTH03DT20M.zip

/var/img/1/368/ID_2012MTH03DT20N.zip

/var/img/1/368/ID_2012MTH03DT20E.zip

/var/img/1/368/ID_2012MTH03DT21M.zip

du -a | sort -nr | head -10
find . -type d -d 1 -print0 | xargs -0 du -sm | sort -nr
du -hs *
du -hs file-name
du -csh --time *|sort -n|tail
du -hsx * | sort -rh | head -10
find . -type d| while read i; do echo $(ls -1 "$i"|wc -m) $(du -s "$i"); done|sort -s -n -k1,1 -k2,2 |awk -F'[ \t]+' '{ idx=$1$2; if (array[idx] == 1) {print} else if (array[idx]) {print array[idx]; print; array[idx]=1} else {array[idx]=$0}}'
2014-02-25 22:50:09
User: knoppix5
Functions: awk du echo find ls read sort wc
1

Very quick! Based only on the content sizes and the character counts of filenames. If both numbers are equal then two (or more) directories seem to be most likely identical.

if in doubt apply:

diff -rq path_to_dir1 path_to_dir2

AWK function taken from here:

http://stackoverflow.com/questions/2912224/find-duplicates-lines-based-on-some-delimited-fileds-on-line

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.

find . -name "pattern" -type f -exec du -ch {} + | tail -n1
du -xm --max-depth 2 /var/log | sort -rn | head
2013-12-16 13:29:33
User: srvesh
Functions: du sort
0

this will give u the details in MB's; from high to low....

du -Sh | sort -h | tail
2013-11-27 17:50:11
User: pdxdoughnut
Functions: du sort
1

I added -S to du so that you don't include /foo/bar/baz.iso in /foo, and change sorts -n to -h so that it can properly sort the human readable sizes.

du -ah | sort -h | tail
du -hs *|sort -h
du -k ~/* | awk '$1 > 50000' | sort -nr
find /Users/jpn/.ievms/ -type f -print0| xargs -0 du -sh
2013-10-16 09:54:19
Functions: du find xargs
-3

When you do a ls -1 | xargs rm it wouldn't workd because those files have spaces. So you must use

find -print0 and xargs -0

du -ah | sort -hr | head -n50 | cat -n
du -hd1 | sort -hr
brew cleanup -n | awk '{print $3}' | xargs du -s | awk '{s+=$1} END {print s}'
find -name *.bak -print0 | du -hc --files0-from=-
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}'
ls | xargs -I{} du -sh {}