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,610 results
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"
kadmin -p admin@NOC.NBIRN.NET -q "addprinc -randkey host/host"
set -o vi; ls -l jnuk<ESC>bCjunk
2009-02-05 22:58:51
User: jonty
Functions: ls set
1

If you spend all day editing in vi then switching your fingers to Emacs mode just for the command line can be difficult. Use set -o vi in your bash shell and enjoy the power of a real editor.

sudo netstat -punta
screen -xR
2009-02-05 22:22:10
User: stuart
Functions: screen
4

Have your screen session running in multiple places. (warning, things start to look weird if the terminal windows have different dimensions)

myLongScript && echo -e '\a' || (echo -e '\a'; sleep 1; echo -e '\a')
2009-02-05 22:13:43
User: stuart
Functions: echo sleep
1

This will ring the system bell once if your script exits successfully and twice if it fails. So you can go look at something else and it will alert you when done. Don't forget to use 'xset b [vol [pitch [duration]]]' to get the bell to sound the way you want.

pkill -U MYWIFE
2009-02-05 22:10:07
User: HansReiser
-2

Kills all processes owned by user MYWIFE (replace MYWIFE with username or ID of your choice)

(Thanks, porges, for the better command)