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 using echo from sorted by
Terminal - Commands using echo - 1,263 results
echo *.log | xargs <command>
2009-02-22 11:32:55
User: mikeda
Functions: echo xargs
5
grep ERROR *.log

-bash: /bin/grep: Argument list too long

echo *.log | xargs grep ERROR /dev/null

20090119.00011.log:DANGEROUS ERROR

read VAR1 VAR2 VAR3 < <(echo aa bb cc); echo $VAR2
echo -en "stats\r\n" "quit\r\n" | nc localhost 11211 | tr -s [:cntrl:] " "| cut -f42,48 -d" " | sed "s/\([0-9]*\)\s\([0-9]*\)/ \2\/\1*100/" | bc -l
doloop() { DONT=/tmp/do-run-run-run; while true; do touch $DONT; (sleep 30; rm $DONT;) & $1 ; if [ -e $DONT ]; then echo restarting too fast; return ; fi ; done }
2009-02-21 02:11:18
User: evil_otto
Functions: echo return rm sleep touch
-1

This runs a command continuously, restarting it if it exits. Sort of a poor man's daemontools. Useful for running servers from the command line instead of inittab.

HOST=127.0.0.1;for((port=1;port<=65535;++port)); do echo -en "$port ";if echo -en "open $HOST $port\nlogout\quit" | telnet 2>/dev/null | grep 'Connected to' > /dev/null; then echo -en "\n\nport $port/tcp is open\n\n";fi;done | grep open
echo "hello world" | festival --tts
2009-02-20 14:00:50
Functions: echo
7

The Festival Speech Synthesis System converts text into sound.

Or: links -dump http://youfavoritewebsite.com | festival --tts

FLOOR=0; RANGE=10; number=0; while [ "$number" -le $FLOOR ]; do number=$RANDOM; let "number %= $RANGE"; done; echo $number
2009-02-20 09:33:56
User: raphink
Functions: echo
Tags: bash
-1

This one-liner outputs a random number between the values given for FLOOR and RANGE.

/lib/ld-linux.so.2 =(echo -e '#include <stdio.h>\nint main(){printf("c one liners\\n");}' | gcc -x c -o /dev/stdout -)
2009-02-20 06:06:29
User: mrtheplague
Functions: c++ echo gcc
6
/lib/ld-linux.so.2

is the runtime linker/loader for ELF binaries on Linux.

=(cmd) is a zsh trick to take the output for the command "inside" it and save it to a temporary file.

echo -e 'blah' | gcc -x c -o /dev/stdout -

pipes the C source to gcc. -x c tells gcc that it's compiling C (which is required if it's reading from a pipe). -o /dev/stdout - tells it to write the binary to standard output and read the source from standard input.

because of the the =() thing, the compiled output is stashed in a tempfile, which the loader then runs and executes, and the shell tosses the tempfile away immediately after running it.

echo 1 > /proc/sys/kernel/sysrq; echo b > /proc/sysrq-trigger
2009-02-20 00:37:18
User: tsaavik
Functions: echo
7

This will cause your machine to INSTANTLY reboot. No un-mounting of drives or anything.

Very handy when something has gone horribly wrong with your server in that co-location facility miles away with no remote hands!

Suspect this works with all 2.2, 2.4 and 2.6 Linux kernels compiled with magic-syskey-request support.

for i in `recode -l | cut -d" " -f 1`; do echo $i": ?" | recode utf-8..$i -s -p >> temp; done; vim temp
2009-02-20 00:14:33
User: saerdnaer
Functions: cut echo vim
0

If you have some textfile with an unknown encoding you can use this list to find out

check_disk -w 15% -c 10% $(for x in $(cat /proc/mounts |awk '{print $2}')\; do echo -n " -p $x "\; done)
while read line; do echo "$(date),$(hostname),$line"; done < somefile.txt
while [ 0 ]; do echo -e "HTTP/1.1 302 Found\nLocation: http://www.whatevs.com/index.html" | nc -vvvv -l -p 80; done
2009-02-19 18:04:01
User: wwest4
Functions: echo
4

any HTTP requests to the machine on the specified port will be redirected to http://www.whatevs.com... quick, dirty, works fine for sites w/

echo "8000000/(20*6*86400)" | bc -l
echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"
2009-02-19 14:15:31
User: stinger
Functions: echo
2

above line in .bash_profile will give you window title in putty or terminal client when you login to your remote server

echo 1+1|bc
while true; do read i; echo $[$i]; done
2009-02-18 23:13:09
User: mulad
Functions: echo read
0

The $[...] block in bash and zsh will let you do math.

echo $[6*7]

This is the same as using $((...)), which also works in ksh. Of course, this is a simple, dumb wrapper and doesn't allow floating-point.

for (( i = 10; i > 0; i-- )); do echo "$i"; sleep 1; done
2009-02-18 20:31:46
User: mestr
Functions: echo sleep
4

Countdown from 10 or whatever you want:)

unset files i; set -f; O=$IFS; while IFS= read -r -d $'\0' files[i++]; do :; done < <(find . -name '*.avi' -print0) && IFS=$O; set +f && echo "Running: mplayer \"${files[ $(( $RANDOM % ${#files[@]} )) ]}\""
2009-02-18 16:53:57
User: DEinspanjer
Functions: echo find read set unset
0

unsets variables used by the one-liner

sets up the IFS bash variable to not be affected by whitespace and disables extra glob expansion

uses read to slurp the results of the find command into an array

selects an element of the array at random to be passed as an argument to mplayer

echo "foo bar" | sudo tee -a /path/to/some/file
2009-02-18 13:54:25
User: raphink
Functions: echo sudo tee
3

This is the solution to the common mistake made by sudo newbies, since

sudo echo "foo bar" >> /path/to/some/file

does NOT add to the file as root.

Alternatively,

sudo echo "foo bar" > /path/to/some/file

should be replaced by

echo "foo bar" | sudo tee /path/to/some/file

And you can add a >/dev/null in the end if you're not interested in the tee stdout :

echo "foo bar" | sudo tee -a /path/to/some/file >/dev/null
echo "^[]0;My_Title_Goes _Here^G"
2009-02-17 20:46:06
User: TheNomad
Functions: echo
5

If you are using an xterm emulation capable terminal emulator, such as PuTTY or xterm on Linux desktop, this command will replace the title of that terminal window. I know it is not nice to have seventeen terminals on your desktop with title PuTTY, you can not tell which one is connected to which server and doing what.

Even though the string between the quotes is typed as literals, it needs a little more finesse to make it work. Here is how it is done key-by-key:

echo "( ctrl-v then ctrl-[ )0;Enter_Title_String_Here( ctrl-v then ctrl-g )"( enter )

ctrl-v : means hold down ctrl key and hit v at the same time like you are pasting in windoze ; also please don't type the parentheses, i.e., ( and )

echo $RANDOM
echo foo bar foobar barfoo && echo !$ !^ !:3 !* && echo /usr/bin/foobar&& echo !$:h !$:t
2009-02-17 09:10:17
User: lhb
Functions: echo
11

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

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

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

foobar

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

Now with path:

echo /usr/bin/foobar

/usr/bin/foobar

For the head:

echo !$:h

echo /usr/bin

/usr/bin

And the tail:

echo !$:t

echo foobar

foobar

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

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

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

And

ls foo* | sort -n

foo1

foo10

foo4

foo7

But:

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

So

ls foo* | sort -n

foo01

foo04

foo07

foo10

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
0

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