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 by depesz from sorted by
Terminal - Commands by depesz - 37 results
perl -le 'print$_%3?$_%5?$_:"Buzz":$_%5?"Fizz":"FizzBuzz"for 1..100'
ack; pcregrep
2011-08-14 18:01:36
User: depesz
Tags: perl grep regex
2

Much better alternatives - grep-alikes using perl regexps. With more options, and nicer outputs.

find public_html/ -type d -exec chmod 755 {} +
2011-07-25 15:25:36
User: depesz
Functions: chmod find
7

+ at the end means that many filenames will be passed to every chmod call, thus making it faster. And find own {} makes sure that it will work with spaces and other characters in filenames.

lsb_release -ri
find . -depth \( -path '*/*.java.orig' -o -path '*/*.java.orig/*' \) -delete
echo $(( $( date +%s ) - $( stat -c %Y * | sort -nr | head -n 1 ) ))
echo "shutdown -h now" | sudo at now + 4 hours
2011-05-02 15:29:48
User: depesz
Functions: at echo sudo
-2

Benefit is that it doesn't make you keep the terminal open.

mkdir copy{1,2}; gzip -dc file.tar.gz | tee >( tar x -C copy1/ ) | tar x -C copy2/
2011-04-14 17:02:05
User: depesz
Functions: gzip mkdir tar tee
Tags: bash tee tar
-1

Sometimes you might need to have two copies of data that is in tar. You might unpack, and then copy, but if IO is slow, you might lower it by automatically writing it twice (or more times)

MyVAR=85 awk '{ print ENVIRON["MyVAR"] }'
2011-04-14 16:46:23
User: depesz
Functions: awk
Tags: bash awk print
3

Alternatively:

export MyVAR=84; awk '{ print ENVIRON["MyVAR"] }'

ack hoge .
grep -r hoge .
find -L . -type l
find . -type d -name .svn -prune -o -type f -print0 | xargs -r0 ...
ps -C apache o pid= | sed 's/^/-p /' | xargs strace
dpkg --get-selections | awk '$2=="install" {print $1}' | sort
find . -type f -name '*.pm' -printf '%6s %p\n' | sort -nr | head -n 50
2011-02-09 16:19:11
User: depesz
Functions: find head sort
0

Will work with filenames with spaces inside. Will not break in case of someone making directory that matches *.pm. And sorts from largest. Where largest is file size, not line count.

ps -o comm= -p $$
2011-02-05 11:54:42
User: depesz
Functions: ps
0

First version was "ps uw -p $$", but current "ps -o comm= -p $$" just gives shell name

perl -le '$,=".";print map int rand 256,1..4'
perl -le '$,=".";print map int rand 256,1..4'
perl -le 'print time()'
find -type f -exec du -sh {} + | sort -rh | head
find . -type f -size +20000k -print0 | xargs -0 du -h | awk -F"\t" '{printf "%s : %s\n", $2, $1}'
2010-12-15 17:51:09
User: depesz
Functions: awk du find xargs
0

Output made so that it will match initial suggestion for this task. Personally, I think that output of du -h is more readable.

perl -ane 'print unless $x{$F[0]}++' infile > outfile
shopt -s globstar; rm -rfv **/.svn
(netstat -atn | awk '{printf "%s\n%s\n", $4, $4}' | grep -oE '[0-9]*$'; seq 32768 61000) | sort -n | uniq -u | head -n 1