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 -e ${PATH//:/\\n} | less
2010-01-09 23:25:02
User: nicoulaj
Functions: echo
Tags: PATH
0

Display the $PATH with one line per entry, in a pager.

i=0; for f in $(find ./ -size -10M -exec stat -c %s {} \; ); do i=$(($i + $f)); done; echo $i
for k in `git branch|sed s/^..//`;do echo -e `git log -1 --pretty=format:"%Cgreen%ci %Cblue%cr%Creset" "$k"`\\t"$k";done|sort
echo - | sed -e :a -e 's/^.\{1,50\}$/&-/;ta'
2010-01-06 17:18:15
User: glaudiston
Functions: echo sed
0

the sed way to print a linhe with 50 hyphens

echo 50 > /proc/sys/vm/swappiness
for i in *.png; do convert "$i" "${i%.png}.jpg" && rm "$i" && echo "$i is converted."; done
2010-01-02 16:26:13
User: auriza
Functions: echo rm
1

Convert all PNG images in directory to JPEG using ImageMagick, and delete the old PNG images.

echo 'Desktop SPAM!!!' | while read SPAM_OUT; do notify-send "$SPAM_OUT"; done
2009-12-31 15:38:35
User: michelsberg
Functions: echo read
Tags: notify-send
2

Route output to notify-send to show nice messages on the desktop, e.g. title and interpreter of the current radio stream

xlaunch(){ T=/tmp/$$;sh -c "$@" >$T.1 2>$T.2;S=$?;[ $S -ne 0 ]&&{ echo -e "'$@' failed with error $S\nSTDERR:\n$(cat $T.2)\nSTDOUT:\n$(cat $T.1)\n"|xmessage -file -;};rm -f $T.1 $T.2;}
2009-12-31 06:59:56
User: bhepple
Functions: echo
Tags: X
1

When you run an X program from a terminal you can see any errors. But when it's run from another X program (eg from a menu item, from your fluxbox 'keys' file etc) it might just die and you see nothing (except perhaps in .xsession-errors). Instead, launch it via this command and you'll see the termination status, stderr and stdout.

eg: "xlaunch firefox" or "xlaunch 'echo stdout; echo stderr >&2; false'":

'echo stdout; echo stderr >&2; false' failed with error 1

STDERR:

stderr

STDOUT:

stdout

BACKUP_FILE_SIZE=`eval ls -l ${BACKUP_FILE} | awk {'print $5'}`; if [ $BACKUP_FILE_SIZE -le 20 ]; then echo "its empty"; else echo "its not empty"; fi
2009-12-29 08:34:37
User: Redrocket
Functions: awk echo ls
-2

If you gzip an empty file it becomes 20 bytes. Some backup checks i do check to see if the file is greater than zero size (-s flag) but this is no good here. Im sure someone has a better check than me for this? No check to see if file exists before checking it's size.

b(){ echo "scale=${2:-2}; $1" | bc -l; }
2009-12-23 23:44:31
User: dadi
Functions: bc echo
0

terms inclosing '()' must be enclosed by "" (soft quotes)

bash variables must be referenced: b $x/$y

ugly bracket checking (balanced, fractions...)

default precision 2

DIR=. ; FSTYPE=$(df -TP ${DIR} | grep -v Type | awk '{ print $2 }') ; echo "${FSTYPE}"
2009-12-22 14:01:50
User: unixhome
Functions: awk df echo grep
1

Exclude 400 client hosts with NFS auto-mounted home directories.

Easily modified for inclusion in your scripts.

echo -ne "\e[32m" ; while true ; do echo -ne "\e[$(($RANDOM % 2 + 1))m" ; tr -c "[:print:]" " " < /dev/urandom | dd count=1 bs=50 2> /dev/null ; done
2009-12-19 19:05:04
User: psykotron
Functions: dd echo tr true
Tags: color
4

A bit different from some of the other submissions. Has bold and uses all c printable characters. Change the bs=value to speed up and increase the sizes of the bold and non-bold strings.

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.