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,221 results
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].

echo *
2009-02-16 21:20:13
User: grep
Functions: echo
2

I know its not much but is very useful in time consuming scripts (cron, rc.d, etc).

echo "<?php phpinfo(); ?>" | php > phpinfo.txt
echo "SELECT * FROM table; " | mysql -u root -p${MYSQLROOTPW} databasename | sed 's/\t/","/g;s/^/"/;s/$/"/;s/\n//g' > outfile.csv
2009-02-16 18:55:18
Functions: echo sed
8

This command converts a MySQL query directly into a .csv (Comma Seperated Value)-file.

echo "sed -e"|perl -pe 's/sed -e/perl -pe/'
2009-02-16 18:39:06
User: drossman
Functions: echo
-1

Replace sed regular expressions with perl patterns on the command line.

The sed equivalent is: echo "sed -e"|sed -e 's/sed -e/perl -pe/'

echo "Set Twitter Status" ; read STATUS; curl -u user:pass -d status="$STATUS" http://twitter.com/statuses/update.xml
2009-02-16 14:34:05
User: ronz0
Functions: echo read
-2

Modify the script for your username and password, and save it as a script. Run the script, and enjoy ./tweet

for (( i = 0; i < 100; i++ )); do echo "$i"; done
2009-02-16 03:19:26
User: themattrix
Functions: echo
1

Print 0 through 99, each on a separate line.

echo 'example' | sed -e 's/^\(.\)/\U\1/'
echo $(( (1234567890 - `date -u +\%s`) / 60 / 60 ))
echo $PATH | tr : \\n
2009-02-13 15:54:58
User: piyo
Functions: echo tr
2

This works in bash and zsh.

You may also want to alias it, if you need to look at it often...

alias lpath="echo \$PATH | tr : \\\\n"

"\$PATH" to make sure to look at your current $PATH

svn st | grep /main/java | awk '{print $2}' | xargs echo | xargs svn ci -m "my comment here"
for ip in $(seq 1 25); do ping -c 1 192.168.0.$ip>/dev/null; [ $? -eq 0 ] && echo "192.168.0.$ip UP" || : ; done
2009-02-11 14:57:34
Functions: echo ping seq
0

this is very useful when there is a different network host to determine which are turned on or not

echo $0
2009-02-11 08:58:01
User: yogsototh
Functions: echo
7

Return the current shell. It is better than print $SHELL which can sometimes return a false value.

while read line; do echo -e "$line@mail.com"; done < list.txt
i=0; f=$(find . -type f -iregex ".*jpg");c=$(echo $f|sed "s/ /\n/g"| wc -l);for x in $f;do i=$(($i + 1));echo "$x $i of $c"; mogrify -strip $x;done
echo 'ibase=10; obase=2; 127' | bc
echo 16i `echo "F" | tr '[a-f]' '[A-F]'` p | dc ; echo 16o "15" p | dc
for i in $(seq 1 50) ; do echo Iteration $i ; done
2009-02-08 23:03:31
User: abcde
Functions: echo seq
8

Optionally, one can use {1..50} instead of seq. E.g. for i in {1..50} ; do echo Iteration $i ; done