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
rename 's/foo/bar/g' *
2009-02-06 06:45:41
User: terrasect
Functions: rename
10

Useful when you want to quickly rename a bunch of files.

netstat -plunt
2009-02-06 06:04:32
Functions: netstat
11

-p Tell me the name of the program and it's PID

-l that is listening

-u on a UDP port.

-n Give me numeric IP addresses (don't resolve them)

-t oh, also TCP ports

wget -qO - http://myip.dk/ | egrep -m1 -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'
curl -s http://whatismyip.org/
curl -s http://myip.dk/ | egrep -m1 -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'
seq -f"ftp://ftp.vim.org/pub/vim/patches/7.1/7.1.%03g" 176 240 | xargs -I {} wget -c {};
2009-02-06 03:19:06
User: liupeng
Functions: seq wget xargs
3

Seq allows you to define printf like formating by specified with -f, %03g is actually tells seq I got three digits, fill the blank digits with 0, and the range is from 176 to 240.

f="FILE";c="CMD";s="stat -f %m $f";t=`$s`;while [ 1 ];do if [ $t -eq `$s` ];then sleep 1;else echo `$c`;t=`$s`;fi;done
find DIR -name "FILE" -exec grep -IHn STRING {} \;
strace -f -s 512 -v ls -l
2009-02-06 02:45:33
User: mkc
Functions: ls strace
5

strace can be invaluable in trying to figure out what the heck some misbehaving program is doing. There are number of useful flags to limit and control its output, and to attach to already running programs. (See also 'ltrace'.)

kill -HUP `ps -A -ostat,ppid,pid,cmd | grep -e '^[Zz]' | awk '{print $2}'`
2009-02-06 02:42:14
User: liupeng
Functions: awk grep kill
-1

You cannot kill zombies, as they are already dead. But if you have too many zombies then kill parent process or restart service.

You can kill zombie process using PID obtained from the above command. For example kill zombie proces having PID 4104:

# kill -9 4104

Please note that kill -9 does not guarantee to kill a zombie process.

ps aux | awk '{ print $8 " " $2 " " $11}' | grep -w Z
cat -A
2009-02-06 02:37:51
User: mkc
Functions: cat
0

This is priceless for discovering otherwise invisible characters in files. Like, for example, that stray Control-M at the end of the initial hash bang line in your script, which causes it to generate a mysterious error even though it looks fine.

('od' is the last word, of course, but for many purposes it's much harder to read.)

cd
2009-02-06 02:37:17
User: YAK
Functions: cd
-9

Just type 2 characters and enter, you will be back.

echo '2^2^20' | time bc > /dev/null
2009-02-06 02:31:55
User: mkc
Functions: bc echo time
1

This is a quick and dirty way to generate a (non-floating-point) CPU-bound task to benchmark. Adjust "20" to higher or lower values, as needed. As a benchmark this is probably a little less bogus than bogomips, and it will run anywhere 'bc' does.

gpg --keyserver pgp.surfnet.nl --recv-key 19886493
2009-02-06 02:24:28
User: liupeng
Functions: gpg
3

get my GPG-key from pgp.surfnet.nl, key id is 19886493.

watch "df | grep /this/folder/"
sudo -u username bash
2009-02-06 00:52:18
User: troelskn
Functions: sudo
5

You need sudo privileges for this command.

Replace username with actual username.

svn merge -r 1337:1336 PATH PATH
2009-02-06 00:48:17
User: troelskn
Functions: merge
-1

Reverts the changes that were made in a particular revision, in the local working copy. You must commit the local copy to the repository to make it permanent.

This is very useful for undoing a change.

You can revert multiple changes by specifying numbers wider apart; Just remember to put the highest number first.

mount -t tmpfs tmpfs /mnt -o size=1024m
2009-02-06 00:33:08
User: ajrobinson
Functions: mount
171

Makes a partition in ram which is useful if you need a temporary working space as read/write access is fast.

Be aware that anything saved in this partition will be gone after your computer is turned off.

find . -name "*.txt" | xargs perl -pi -e 's/old/new/g'
2009-02-06 00:28:03
User: neztach
Functions: find perl xargs
6

syntax follows regular command line expression.

example: let's say you have a directory (with subdirs) that has say 4000 .php files.

All of these files were made via script, but uh-oh, there was a typo!

if the typo is "let's go jome!" but you meant it to say "let's go home!"

find . -name "*.php" | xargs perl -pi -e "s/let\'s\ go\ jome\!/let\'s\ go\ home\!/g"

all better :)

multiline: find . -name "*.php" | xargs perl -p0777i -e 's/knownline1\nknownline2/replaced/m'

indescriminate line replace: find ./ -name '*.php' | xargs perl -pi -e 's/\".*$\"/\new\ line\ content/g'

'ls -1 *<pattern>* | while read file; do scp $file user@host:/path/; if [[ $? -eq 0 ]]; then rm $file; fi; done'
ct mkelem -nc `find ./ -name "*" | xargs`
cleartool co -nc `cleartool ls -recurse | grep "hijacked" | sed s/\@\@.*// | xargs`
locate searchstring | xargs ls -l
kadmin -p admin@NOC.NBIRN.NET -q "ktadd -k /etc/krb5.keytab host/hostname"