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

All commands from sorted by
Terminal - All commands - 11,591 results
ps aux | awk '/name/ {print $2}'
2009-02-20 21:35:52
User: evil_otto
Functions: awk ps
-5

This finds a process id by name, but without the extra grep that you usually see. Remember, awk can grep too!

cd //
find ~ -mtime +365 -exec mv {} /tmp/mybackup \;
2009-04-11 12:16:48
User: kayowas
Functions: find mv
-5

This example uses the -exec option to move all matching files into a backup directory

perl -e 'map { $on=$_; s/\]/_/; rename($on, $_) or warn $!; } <*>;'
gcc -x c -o /tmp/out - -lgmp <<< '#include <stdlib.h> ... SEE SAMPLE OUTPUT FOR FULL COMMAND
2009-09-10 02:10:46
User: hank
Functions: c++ gcc
-5

It's hard to beat C. This is just slightly faster than the bc version on my machine.

real 0m26.856s

user 0m25.030s

sys 0m0.024s

Requirements: libgmp headers, gcc.

ps ax | grep <processname> | grep -v grep | awk '{print $1}' | sudo xargs kill -9
dd if=/dev/zero of=/dev/hda
alias pfind='ps aux | grep '
$ lynx -useragent=Opera -dump 'http://www.facebook.com/ajax/typeahead_friends.php?u=100003119823986&__a=1' |gawk -F'\"t\":\"' -v RS='\",' 'RT{print $NF}' |grep -v '\"n\":\"' |cut -d, -f2
dd if=/dev/urandom of=/dev/somedisk
2012-01-24 09:13:15
User: joedhon
Functions: dd
-5

Same game as #10096 . loop as many times as you like.

cat > /dev/null
2010-10-08 14:35:25
User: GouNiNi
Functions: cat
-5

In exemple, screen can bind keys to switch between windows. I like to use Ctrl + Arrow to move left or right window. So I bind like this in .screenrc :

bindkey ^[OD prev # Ctl-left, prev window

bindkey ^[OC next # Ctl-right, next window

rpm -qf /lib/modules/*
2010-03-01 05:00:56
User: megacoder
Functions: rpm
-5

Exploit that RPM database just sitting there, taking up space.

while read line; do echo $line; done <<< "$var"
2011-09-22 16:53:32
User: totti
Functions: echo read
-5

Consider the following simple situation [ reading something using while and read ]

[See script 1 in sample output]

---------------------------------------------------

The variable var is assigned with "nullll" at first. Inside the while loop [piped while] it is assigned with "whillleeee". [Onlly 2 assignments stmts]. Outside the loop the last assigned value for "var" [and no variable] inside the while can't be accessed [Due to pipe, var is executed in a sub shell].

In these type of situation variables can be accessed by modifying as follows.

[See script 2 in sample output]

___________________________

Vary helpful when reading a set of items, say file names, stored on a file [or variable] to an array an use it later.

Is there any other way 2 access variables inside and outside the loop ??

while true; do sleep 4 ; xdotool click 1 ; done
2011-09-23 09:23:02
User: rusino
Functions: sleep
-5

Automation click every 4 second on a macro slot bar to world of warcraft for prospecting item

enable auto loot and create macro, put mouse over slot on the bar

/cast Prospecting

/use Elementium Ore

history -r
poweriso convert image.daa -o image.iso -ot iso
for person in Alex Bruce Fred Kathy Vicki Victoria ; do say -v $person "Hello, my name is $person"; sleep 1; done
2009-03-09 15:42:13
User: haivu
Functions: sleep
Tags: Os X
-5

Listen to different voices in the system--useful for picking the voice you like

find /dir/ -name *name*
2009-02-09 20:24:10
User: cinan
Functions: find
-5

"*" is important if you don't know exact name of file. Check it out and you'll see

find . -exec chmod 777 {} \;
pkill $1
2009-07-20 12:47:41
User: svg
-5

There's already a proper command for what the former alternative tried to script

echo alias xkcd="gwenview `w3m -dump http://xkcd.com/|grep png | awk '{print $5}'` 2> /dev/null" >> .bashrc
2010-01-30 20:38:16
User: GinoMan2440
Functions: alias echo
-5

Add an alias to your .bashrc that allows you to issue the command xkcd to view (with gwenview) the newest xkcd comic... I know there are thousands of them out there but this one is at least replete with installer and also uses a more concise syntax... plus, gwenview shows you the downloading progress as it downloads the comic and gives you a more full featured viewing experience.

can(){ shift 2; sudo "$@"; }
lynx --dump http://ip.boa.nu|sed -e 's/^[[:space:]]*//' -e 's/*[[:space:]]$//'|grep -v ^$
2009-11-04 22:23:45
User: xeor
Functions: grep sed
-5

For those of us that still uses lynx :)

print -l $path
2009-08-27 16:33:04
User: Mikachu
Tags: zsh PATH
-5

This doesn't work in bash, but in zsh you can typeset -T to bind a scalar variable to an array. $PATH and $path behave this way by default.