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,228 results
echo "\"\e[5~\": history-search-backward" >> ~/.inputrc
2009-03-10 15:32:52
User: silbermm
Functions: echo
0

What this does is, if I type ?ssh ? then hit the page-up key, it will complete the line to the last time in my history file that I typed ssh. Hitting page up again will go to the 2nd to last time I typed it. Incredibly handy if you ever type the same commands more than once.

credit goes to http://www.sharms.org/blog/2009/03/10/make-your-bash-shell-cool-again/

echo "\"\e[6~\": history-search-forward" >> ~/.inputrc
echo -n $mypass | md5sum | awk {'print $1'}
2009-03-10 13:12:21
User: tororebelde
Functions: awk echo md5sum
1

This was useful to generate random passwords to some webpage users, using the sample code, inside a bash script

i="8uyxVmdaJ-w";mplayer -fs $(curl -s "http://www.youtube.com/get_video_info?&video_id=$i" | echo -e $(sed 's/%/\\x/g;s/.*\(v[0-9]\.lscache.*\)/http:\/\/\1/g') | grep -oP '^[^|,]*')
2009-03-09 03:57:44
User: lrvick
Functions: echo grep sed
56

This is the result of a several week venture without X. I found myself totally happy without X (and by extension without flash) and was able to do just about anything but watch YouTube videos... so this a the solution I came up with for that. I am sure this can be done better but this does indeed work... and tends to work far better than YouTube's ghetto proprietary flash player ;-)

Replace $i with any YouTube ID you want and this will scrape the site for the _real_ URL to the full quality .FLV file on Youtube's server and will then will hand that over to mplayer (or vlc or whatever you want) to be streamed.

In some browsers you can replace $i with just a % or put this in a shell script so all YouTube IDs can be handed directly off to your media player of choice for true streaming without the need for Flash or a downloader like clive. (I do however fully recommend clive if you wish to archive videos instead of streaming them)

If any interest is shown I would be more than happy to provide similar commands for other sites. Most streaming flash players use similar logic to YouTube.

Edit: 05/03/2011 -

Updated line to work with current YouTube. It could be a lot prettier but I will probably follow up with another update when I figure out how to get rid of that pesky Grep. Sed should take that syntax... but it doesn't.

Original (no longer working) command:

mplayer -fs $(echo "http://youtube.com/get_video.php?$(curl -s $youtube_url | sed -n "/watch_fullscreen/s;.*\(video_id.\+\)&title.*;\1;p")")

echo "Whatever you need" | sudo tee [-a] /etc/system-file.cfg
2009-03-09 01:33:31
User: asmoore82
Functions: echo sudo tee
4

only for sudo-style systems.

Use this construct instead of I/O re-directors ``>'' or ``>>'' because

sudo only elevates the commands and *not* the re-directors.

***warning: remember that the `tee` command will clobber

file contents unless it is given the ``-a'' argument

Also, for extra security, the "left" command is still run unprivileged.

echo "The date is: $(date +%D)"
2009-03-07 15:51:59
User: atoponce
Functions: echo
76

This is a simple example of using proper command nesting using $() over ``. There are a number of advantages of $() over backticks. First, they can be easily nested without escapes:

program1 $(program2 $(program3 $(program4)))

versus

program1 `program2 \`program3 \`program4\`\``

Second, they're easier to read, then trying to decipher the difference between the backtick and the singlequote: `'. The only drawback $() suffers from is lack of total portability. If your script must be portable to the archaic Bourne shell, or old versions of the C-shell or Korn shell, then backticks are appropriate, otherwise, we should all get into the habit of $(). Your future script maintainers will thank you for producing cleaner code.

echo 1 2 3 > FILE; while read -a line; do echo ${line[2]}; done < FILE
2009-03-06 15:32:40
User: occam
Functions: echo read
Tags: bash
-2

This will print out the third column of every line in FILE. Useful for many files in /proc or *csv data.

SUM=0; for FILESIZE in `find /tmp -type f -iname \*pdf -exec du -b {} \; 2>/dev/null | cut -f1` ; do (( SUM += $FILESIZE )) ; done ; echo "sum=$SUM"
2009-03-05 17:16:52
User: alcik
Functions: cut du echo
Tags: find du
0

This example summarize size of all pdf files in /tmp directory and its subdirectories (in bytes).

Replace "/tmp" with directory path of your choice and "\*pdf" or even "-iname \*pdf" with your own pattern to match specific type of files. You can replace also parameter for du to count kilo or megabytes, but because of du rounding the sum will not be correct (especially with lot of small files and megabytes counting).

In some cases you could probably use sth like this:

du -cb `find /tmp -type f -iname \*pdf`|tail -n 1

But be aware that this second command CANNOT count files with spaces in their names and it will cheat you, if there are some files matching the pattern that you don't have rights to read. The first oneliner is resistant to such problems (it will not count sizes of files which you cant read but will give you correct sum of rest of them).

touch /tmp/$$;for N in `seq -w 0 7777|grep -v [89]`; do chmod $N /tmp/$$; P=`ls -l /tmp/$$ | awk '{print $1}'`; echo $N $P; done;rm /tmp/$$
echo Print text vertically|sed 's/\(.\)/\1\n/g'
dpkg --compare-versions 1.0-2ubuntu5 lt 1.1-1~raphink3 && echo y || echo n
2009-02-28 18:01:59
User: raphink
Functions: echo
Tags: Debian Ubuntu
2

Compares two versions with dpkg. It is not always obvious what version dpkg/apt will consider to be more recent. Operators include the following :

* These treat an empty version as earlier than any version: lt le eq ne ge gt.

* These treat an empty version as later than any version: lt-nl le-nl ge-nl gt-nl.

* These are provided only for compatibility with control file syntax: < > >.

This command doesn't output anything. It only returns with status 0 or 1, hence the echo "y" || echo "n" to get an output.

for vm in $(vmware-cmd -l);do echo -n "${vm} ";vmware-cmd ${vm} getstate|awk '{print $2 " " $3}';done
2009-02-26 16:45:04
User: dbart
Functions: echo
3

I use this command on my machines running VMware Server to print out the state of all registered Virtual machines.

echo COMMAND | xargs -ixxx ps -C xxx -o pid= | xargs -ixxx ls -l /proc/xxx/cwd
echo "!!" > foo.sh
2009-02-25 00:37:25
User: dnavarre
Functions: echo
81

Sometimes commands are long, but useful, so it's helpful to be able to make them permanent without having to retype them. An alternative could use the history command, and a cut/sed line that works on your platform.

history -1 | cut -c 7- > foo.sh
declare -i aa ; aa=3*8 ; echo $aa
OLD_IFS="$IFS"; IFS=: ARRAY=($PATH); echo ${ARRAY[2]}; IFS="$OLD_IFS"
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