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,226 results
p() { l=$LINES; case $1 in do) shift; IFS=$'\n' _pg=( $("$@") ) && _pgn=0 && p r;; r) echo "${_pg[*]:_pgn:$((l-4))}";; d) (( _pgn+=l-4 )); (( _pgn=_pgn>=${#_pg[@]}?${#_pg[@]}-l+4:_pgn )); p r;; u) (( _pgn=_pgn<=l-4?0:_pgn-$l-4 )); p r;; esac; }
2009-12-18 23:35:53
User: intuited
Functions: echo
Tags: bash pager
0

Manpages, command summaries, and pretty much everything else usually have the information you're most likely to want at the beginning. Seeing just the last 40 or so lines of options from a command that has 100 is not super useful, and having to scroll up each time you want to glance at something is spastic.

Run this and then do something like

p do vi --help

and you'll get the first screen(-mostly-)full of vi's usage info and options list

Then use

p d

to page down, and

p u

to page up.

To see the current page again:

p r

Also useful for situations like

p do aptitude search ~dsmorgasbord p next #p sudo aptitude -r install libwickedawesome-perl-snoochieboochies p next p sudo aptitude -r install libwickedawesome-perl-snoochieboochies snazztasticorama-dev-v0.&#8734;

where you're using readline up-arrow, HOME, END, etc., to quickly recall commented commands.

For the unaware, that option to aptitude search will bring up all of the packages whose descriptions contain the string "smorgasbord". Depending on your distro, there could potentially be hundreds of them.

getent shadow | while IFS=: read a b c; do grep -q '!' <<< "$b" && echo "$a LOCKED" || echo "$a not locked"; done
2009-12-18 15:24:19
User: sputnick
Functions: echo getent grep read
0
man 5 shadow

I think it's more reliable, because

passwd -S

dont show "locked" but "L" as second field on my Archlinux for a particular user.

( unixhome alternative ).

awk -F":" '{ print $1 }' /etc/passwd | while read UU ; do STATUS=$(passwd -S ${UU} | grep locked 2>/dev/null) ; if [[ ! -z ${STATUS} ]] ; then echo "Account ${UU} is locked." ; fi ; done
echo $LS_COLORS | sed 's/:/\n/g' | awk -F= '!/^$/{printf("%s \x1b[%smdemo\x1b[0m\n",$0,$2)}'
2009-12-15 01:17:46
User: bones7456
Functions: awk echo sed
8

This can show all ls colors, with a demo.

while [ 1 ]; do banner 'ze missiles, zey are coming! ' | while IFS="\n" read l; do echo "$l"; sleep 0.01; done; done
2009-12-14 07:40:07
User: craigds
Functions: banner echo read sleep
10

Displays a scrolling banner which loops until you hit Ctrl-C to terminate it.

Make sure you finish your banner message with a space so it will loop nicely.

{ echo -e "$body"; uuencode "$outfile" "$outfile"; } | mail -s "$subject" "$destaddr" ;
2009-12-10 18:08:59
User: glaudiston
Functions: echo mail uuencode
Tags: cat mail
0

on this way we can define the body too

time { i=0; while [ $(( i < 65535 )) -eq 1 ] ; do nc -zw2 localhost $((++i)) && echo port $i opened ; done; }
2009-12-09 17:33:47
User: glaudiston
Functions: echo time
1

in loop, until the last port (65535), list all opened ports on host.

in the sample I used localhost, but you can replace with any host to test.

while read str; do echo "$((++i)) - $str"; done < infile
2009-12-09 15:11:25
User: putnamhill
Functions: echo read
Tags: bash
-1

Hi glaudiston, you can save a few chars by leaving out cat and pipe and still enjoy the added flexibility.

cat infile | while read str; do echo "$((++i)) - $str" ; done;
2009-12-09 14:05:09
User: glaudiston
Functions: cat echo read
-1

Yep, is hard, but is a way more flexible using pipe.

alias :q='tput setaf 1; echo >&2 "this is NOT vi(m) :/"; tput sgr0'
2009-12-08 12:59:44
User: sputnick
Functions: alias echo tput
Tags: vim alias vi tput
-1

For vi(m) users :

Add it in your ~/.bashrc

Add an "exit" @ the end if you are masochist ;)

nc -zw2 www.example.com 80 && echo open
2009-12-07 21:35:25
User: sputnick
Functions: echo
Tags: netcat nc
6

@putnamhill, no need if statement in that case.

&& is a AND and || is a OR

if (nc -zw2 www.example.com 80); then echo open; fi
echo <ctrl+v><ctrl+o><enter>
2009-12-07 20:02:33
User: mkobler
Functions: echo
1

Also works in places where reset does not.

MyIps(){ echo -e "local:\n$(ifconfig $1 | grep -oP 'inet (add?r:)?\K(\d{1,3}\.){3}\d{1,3}')\n\npublic:\n$(curl -s sputnick-area.net/ip)"; }
2009-12-06 22:52:31
User: sputnick
Functions: echo
1

Like the tiltle said, you can use an argument too ( the interface )

MyIps eth0

will show only the IP of this interface and the public IP

( tested with Linux )

You can add that function in ~/.bashrc, then

. ~/.bashrc

Now you are ready to call this function in all your terms...

for f in *.jar; do if jar -tf $f | grep -q javax.servlet; then echo $f; fi; done
2009-12-04 20:11:36
User: wekoch
Functions: echo grep
1

You could subsitute javax.servlet for any namespace you need.

command_not_found_handle() { echo 6661696c626f61742e2e2e0a | xxd -p -r; }
2009-12-03 21:33:35
Functions: echo
-1

This will give you a friendly warning if a command doesn't exists.

echo "(Something like http://foo.com/blah_blah)" | grep -oP "\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))"
sudo echo 0 > /sys/block/sdb/queue/rotational
2009-11-27 12:16:17
User: nickleus
Functions: echo sudo
5

if you still get a permissions error using sudo, then nano the file:

sudo nano -w /sys/block/sdb/queue/rotational

and change 1 to 0

this thread:

http://www.ocztechnologyforum.com/forum/showpost.php?p=369836&postcount=15

says that this will "help the block layer to optimize a few decisions"

printf $(echo -n $1 | sed 's/\\/\\\\/g;s/\(%\)\([0-9a-fA-F][0-9a-fA-F]\)/\\x\2/g')
2009-11-25 04:27:39
User: infinull
Functions: echo printf sed
2

My version uses printf and command substitution ($()) instead of echo -e and xargs, this is a few less chars, but not real substantive difference.

Also supports lowercase hex letters and a backslash (\) will make it through unescaped

function decToBin { echo "ibase=10; obase=2; $1" | bc; }
2009-11-24 22:57:58
User: woxidu
Functions: echo
3

Convert some decimal numbers to binary numbers. You could also build a general base-converter:

function convBase { echo "ibase=$1; obase=$2; $3" | bc; }

then you could write

function decToBun { convBase 10 2 $1; }
echo .*
for item in *;do echo -n "$item - ";find "$item" -type f -print0 | xargs -0 file -iNf - | grep video | cut -d: -f1 | xargs -d'\n' /usr/share/doc/mplayer/examples/midentify | grep ID_LENGTH | awk -F= '{sum+=$2} END {print(sum/60)}'; done | grep -v ' - 0$'
2009-11-19 06:28:15
User: jnash
Functions: awk cut echo file grep xargs
0

I know this has been beaten to death but finding video files using mime types and printing the "hours of video" for each directory is (IMHO) easier to parse than just a single total. Output is in minutes.

Among the other niceties is that it omits printing of non-video files/folders

PS: Barely managed to fit it within the 255 character limit :D

while true; do echo -ne "$(date)\r"; sleep 1; done
2009-11-17 22:45:37
User: polaco
Functions: echo sleep
12

The above code is just an example of printing on the same line, hit Ctrl + C to stop

When using echo -ne "something\r", echo will:

- print "something"

- dont print a new line (-n)

- interpret \r as carriage return, going back to the start of the line (-e)

Remember to print some white spaces after the output if your command will print lines of different sizes, mainly if one line will be smaller than the previous

Edit from reading comments: You can achieve the same effect using printf (more standardized than echo): while true; do printf "%-80s\r" "$(date)"; sleep 1; done

(echo CD_DA; for f in {01..99}; do echo "$f Hz">&2; sox -nt cdda -r44100 -c2 $f.cdda synth 30 sine $f; echo TRACK AUDIO; echo FILE \"$f.cdda\" 0; done) > cdrdao.toc && cdrdao write cdrdao.toc && rm ??.cdda cdrdao.toc
2009-11-17 06:23:42
User: hackerb9
Functions: cdrdao echo rm write
21

This command creates and burns a gapless audio CD with 99 tracks. Each track is a 30 second sine wave, the first is 1 Hz, the second 2 Hz, and so on, up to 99 Hz. This is useful for testing audio systems (how low can your bass go?) and for creating the constant vibrations needed to make non-Newtonian fluids (like cornstarch and water) crawl around.

Note, this temporarily creates 500MB of .cdda files in the current directory. If you don't use the "rm" at the end of the command, you can burn more disks using

cdrdao write cdrdao.toc

Prerequisites: a blank CD-R in /dev/cdrw, sox (http://sox.sourceforge.net/), and cdrdao (http://cdrdao.sourceforge.net/). I'm also assuming a recent version of bash for the brace expansion (which just looks nicer than using seq(1), but isn't necessary).

range () { end=$(echo "$1 + $2 - 1" | bc); echo "$1-$end"; }
2009-11-12 22:53:08
User: bartonski
Functions: echo
0

for example:

echo "..1234567." | cut -c $(range 3 7)

yields

1234567