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.


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:



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.




All commands from sorted by
Terminal - All commands - 11,857 results
nc -v -l 80 < file.ext
2009-02-17 14:39:52
User: moz667

From the other machine open a web navigator and go to ip from the machine who launch netcat, http://ip-address/

If you have some web server listening at 80 port then you would need stop them or select another port before launch net cat ;-)

* You need netcat tool installed

history | awk '{print $2}' | sort | uniq -c | sort -rn | head
grep -c -e '^cpu[0-9]\+' /proc/stat
egrep -o '\b[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\b' access.log | sort -u
^J tput sgr0 ^J
2009-02-17 09:57:22
User: berta
Functions: tput

when your terminal session seems unrensponsive (this normally happen after outputting some binary data directly on your standard output) it may me saned by hitting:

CTRL+J tput sgr0 CTRL+J

Note: don't press the Enter key, just ctrl+j

curl http://root:PASSWORD@ROUTER_DYN_DNS/bwm/tomato_rstatsa001839ceb1d4.gz?_http_id=HTTPID > $HOME/Dropbox/Backups/tomato_stats.gz
2009-02-17 09:45:34
User: moritz

You must get your Backup Url from: http://ROUTER_DYN_DNS/admin-bwm.asp under "Backup".

I set it up in a curl

find . -type f|while read f; do mv $f `echo $f |tr '[:upper:]' '[ :lower:]'`; done
2009-02-17 09:44:38
User: berta
Functions: find mv read

or, to process a single directory:

for f in *; do mv $f `echo $f |tr '[:upper:]' '[:lower:]'`; done
find . -type f -name \*.c | while read f; do mv $f "`basename $f .c`".C; done
2009-02-17 09:30:43
User: berta
Functions: find mv read

or, for a single directory:

for f in *.c; do mv $f "`basename $f .c`".C; done
echo foo bar foobar barfoo && echo !$ !^ !:3 !* && echo /usr/bin/foobar&& echo !$:h !$:t
2009-02-17 09:10:17
User: lhb
Functions: echo

When expanding, bash output the command, so don't be affraid if you type the command.

Here is the details:

First examples:

echo foo bar foobar barfoo

First argument:

echo !$

echo barfoo


(Note that typing echo foo bar foobar barfoo && echo !$, bash substitute !$ with $:1)

Last argument:

echo foo bar foobar barfoo && echo !^

echo foo bar foobar barfoo && echo barfoo

foo bar foobar barfoo


All the arguments:

echo !*

echo foo bar foobar barfoo

foo bar foobar barfoo

The third argument:

echo foo bar foobar barfoo && echo !:3

echo foo bar foobar barfoo && echo foobar

foo bar foobar barfoo


You may want to add {} for large numbers: echo !:{11} for example

Now with path:

echo /usr/bin/foobar


For the head:

echo !$:h

echo /usr/bin


And the tail:

echo !$:t

echo foobar


You also may want to try !:h and !:t or !!3-4 for the third and the fourth (so !!:* == !!:1-$)

find ~/bin/ -name "*sh" -print0 | xargs -0t tar -zcvf foofile.tar.gz
2009-02-17 08:48:34
User: lhb
Functions: find tar xargs

tar options may change ;)

c to compress into a tar file, z for gzip (j for bzip) man tar

-print0 and -0t are usefull for names with spaces, \, etc.

for i in `seq -f %03g 5 50 111`; do echo $i ; done
2009-02-17 08:41:44
User: lhb
Functions: echo

seq allows you to format the output thanks to the -f option. This is very useful if you want to rename your files to the same format in order to be able to easily sort for example:

for i in `seq 1 3 10`; do touch foo$i ;done


ls foo* | sort -n






for i in `seq -f %02g 1 3 10`; do touch foo$i ;done


ls foo* | sort -n





find /directory/to/search/ -type f -print0 | xargs -0 grep "findtext"
2009-02-17 07:16:32
User: dingobytes
Functions: find grep xargs

this will find text in the directory you specify and give you line where it appears.

dd if=/dev/random of=bigfile bs=1024 count=102400
grep "processor" /proc/cpuinfo | wc -l
2009-02-17 05:39:49
User: jbcurtis
Functions: grep wc

/proc/cpuinfo contains information about the CPU.

Search for "processor" in the /proc/cpuinfo file

wc -l, counts the number of lines.

grep -sq "" /etc/lsb-release && lsb_release -rd
2009-02-17 05:21:20
User: int19h
Functions: grep

grep -sq "" filename && command

grep can be used in combination with && to run a command if a file exists.

START=20; END=50 echo $(($START+(`od -An -N2 -i /dev/random`)%($END-$START+1)))
2009-02-17 05:05:30
User: pyrho
Functions: echo

This commands lets you generate a random number between the range [$START; $END].

ps aux | grep "[s]ome_text"
2009-02-17 02:10:50
User: SiegeX
Functions: grep ps

The trick here is to use the brackets [ ] around any one of the characters of the grep string. This uses the fact that [?] is a character class of one letter and will be removed when parsed by the shell. This is useful when you want to parse the output of grep or use the return value in an if-statement without having its own process causing it to erroneously return TRUE.

cd /source/directory; tar cf - . | tar xf - -C /destination/directory
some_command | tee >(command1) >(command2) >(command3) ... | command4
2009-02-17 01:55:07
User: SiegeX
Functions: tee

Using process substitution, we can 'trick' tee into sending a command's STDOUT to an arbitrary number of commands. The last command (command4) in this example will get its input from the pipe.

2009-02-17 01:23:39
User: roliver

Files saved on a windows machine use different ascii characters for lines turns. When viewing such files in VI the will most often have a ^M(control-VM) character at the end of each line. This command will remove all occurrences of that character

for i in {001..999}; print $i
2009-02-16 23:03:53
User: karld

zsh only

I always hated resorting to using $(seq -w 1 99) to pad numbers. zsh provides a shortcut that couldn't be more intuitive. It also works in reverse {99..01}

tail -f /path/to/timestamped/files/file-*(om[1])
2009-02-16 22:55:16
User: karld
Functions: tail

zsh only

If you have this command in your history, you can always re-run it and have it reference the latest file.

The glob matches all timestamped files and then the resulting array is sorted by modification time (m) and then the first element in the sorted array is chosen (the latest)

nc localhost 10000 <<< "message"
2009-02-16 22:14:28
User: karld

zsh only - This avoids the need for echo "message" | which creates an entire subshell. Also, the text you are most likely to edit is at the very end of the line, which, in my opinion, makes it slightly easier to edit.

fortune -s -c -a | cowthink -d -W 45
2009-02-16 21:45:33
User: starchox

The popular fortune program telling by a cow (see sample).

- fortune

- cowsay

cut -d: -f1 /etc/passwd | sort
2009-02-16 21:42:34
User: neW1
Functions: cut

Easily list all users