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 by argv from sorted by
Terminal - Commands by argv - 21 results
case `uname` in FreeBSD)a=$#; case $a in 2) case $1 in 0) jot $(($2+1)) 0 $2 ;; *) jot $2 $1 $2 ;;esac;esac;esac; # usage: seq 1 4; seq 0 4
dng(){ local a;a=$(sed '/'"$1"'/!d' /etc/hosts |sed '=;'"${2-1,$}"'!d'|sed '/ /!d');echo $a|tr '\040' '\n'|nl -bp'[0-9]$'|less -E;export dn=$(echo $a|sed 's,.* ,,');export ip=$(echo $a|sed 's, .*,,');echo \$dn=$dn;echo \$ip=$ip;}
2012-04-01 23:57:09
User: argv
Functions: echo export less nl sed tr
0

usage: dng BRE [selection]

default selection is the last match

DNS is ok, but although domainnames may be easier to remember than IP numbers, it still requires typing them out. This can be error-prone. Even more so than typing IPv4 numbers, depending on the domainname, its length and complexity.

_p(){ ps ax |grep $1 |sed '/grep.'"$1"'/d' |while read a;do printf ${a%% *}' ';printf "${a#* }" >&2;printf '\n';done;}
2012-04-01 19:46:19
User: argv
Functions: grep printf ps read sed
0

proc lister

usage: p

proc killer

usage: p patt [signal]

uses only ps, grep, sed, printf and kill

no need for pgrep/pkill (not part of early UNIX)

_p(){

ps ax \

|grep $1 \

|sed '

/grep.'"$1"'/d' \

|while read a;do

printf ${a%% *}' ';

printf "${a#* }" >&2;

printf '\n';

done;

}

p(){

case $# in

0)

ps ax |grep .|less -iE;

;;

1)

_p $1;

;;

[23])

_p $1 2>/dev/null \

|sed '/'"$2"'/!d;

s,.*,kill -'"${3-15}"' &,'|sh -v

;;

esac;

}

alas, can't get this under 255 chars.

flatcap?

_p(){ ps ax |grep $1 |sed '/grep.'"$1"'/d' |while read a;do printf ${a%% *}' ';printf "${a#* }" >&2;printf '\n';done;}
2012-04-01 19:45:17
User: argv
Functions: grep printf ps read sed
0

proc lister

usage: p

proc killer

usage: p patt [signal]

uses only ps, grep, sed, printf and kill

no need for pgrep/pkill (not part of early UNIX)

_p(){

ps ax \

|grep $1 \

|sed '

/grep.'"$1"'/d' \

|while read a;do

printf ${a%% *}' ';

printf "${a#* }" >&2;

printf '\n';

done;

}

p(){

case $# in

0)

ps ax |grep .|less -iE;

;;

1)

_p $1;

;;

[23])

_p $1 2>/dev/null \

|sed '/'"$2"'/!d;

s,.*,kill -'"${3-15}"' &,'|sh -v

;;

esac;

}

alas, can't get this under 255 chars.

flatcap?

lso(){ jot -w '%04d' 7778 0000 7777 |sed '/[89]/d;s,.*,printf '"'"'& '"'"';chmod & '"$1"';ls -l '"$1"'|sed s/-/./,' \ |sh \ |{ echo "lso(){";echo "ls \$@ \\";echo " |sed '";sed 's, ,@,2;s,@.*,,;s,\(.* \)\(.*\),s/\2/\1/,;s, ,,';echo \';echo };};}
2012-01-08 05:48:24
User: argv
Functions: chmod echo ls sed sh
0

this requires the use of a throwaway file.

it outputs a shell function.

assuming the throwaway file is f.tmp

usage: >f.tmp;lso f.tmp > f.tmp; . f.tmp;rm f.tmp;lso -l ...

notes:

credit epons.org for the idea. however his version did not account for the sticky bit and other special cases.

many of the 4096 permutations of file permissions make no practical sense. but chmod will still create them.

one can achieve the same sort of octal output with stat(1), if that utility is available.

here's another version to account for systems with seq(1) instead of jot(1):

lso(){

case $# in

1)

{ case $(uname) in

FreeBSD)

jot -w '%04d' 7778 0000 7777 ;;

*)

seq -w 0000 7777 ;;

esac; } \

|sed '

/[89]/d

s,.*,printf '"'"'& '"'"';chmod & '"$1"';ls -l '"$1"'|sed s/-/./,' \

|sh \

|{

echo "lso(){";

echo "ls \$@ \\";

echo " |sed '";

sed '

s, ,@,2;

s,@.*,,;

s,\(.* \)\(.*\),s/\2/\1/,;

s, ,,';

echo \';

echo };

};

;;

*)

echo "usage: lso tmp-file";

;;

esac;

}

this won't print out types[1]. but its purpose is not to examine types. its focus is on mode and its purpose is to make mode easier to read (assuming one finds octal easier to read).

1. one could of course argue "everything is a file", but not always a "regular" one. e.g., a "directory" is really just a file comprising a list.

case $# in 0) echo usage: $0 pattern ;; *)case $1 in */*)sed ' s,'"$1"',\ ,g';; *) sed ' s/'"$1"'/\ /g' ;;esac;esac;
2011-12-30 23:54:12
User: argv
Functions: echo sed
-4

alternative to tr char '\012'

works with sed's that don't accept "\n"

allows for multi-char sentinals, while tr(1) only operates on single chars

col1(){ case $# in 0)echo col1 col-length;;*) sed 's/\(.\{'"$1"'\}\)\(.*\)/\1/' esac;}
2011-12-30 23:35:29
User: argv
Functions: sed
-3

for small output only

example usage:

jobs -l |col1 72

tmpfs(){ cd /;for i in $@;do tar czvf /tmp/$i $i;mount -t tmpfs tmpfs /$i;tar xvzf /tmp/$i;cd ~ ;}# usage: tmpfs etc var
_ff(){ cd /mnt;echo /mnt/*/* |sed 's/ \/mnt\//\&/g' |sed '/'"$1"'/!d'; cd -;}
2011-12-30 23:25:31
User: argv
Functions: cd echo sed
-5

_ff(){

cd /mnt;

echo /mnt/*/* |sed '

s/ \/mnt\//\&/g;

'|sed '/'"$1"'/!d';

cd -;

}

ff(){

case $# in

0)

echo "usage: ff glob [sed-cmds] [--|var-name]"

;;

1)

_ff $1 |sed =

;;

[2-9])

case $2 in

--) _ff $1 |less -SN

;;

*) _ff $1 |sed -n ''"$2"''|tr '\n' '\040' |sed 's/.*/export '"$3"'=\"&/;s/=\" /=\"/;s/ $/\"/' > $HOME/.ff;

case $# in

3)

. $HOME/.ff

;;

esac;

sed '

s/export .*=\"/\$'"$3"' = \"/;' $HOME/.ff;\

;;

esac

;;

esac;

}

v(){

local a=$HOME;

sed '

s/export /less -n \$/;

s/=.*//;

' $a/.ff > $a/.v ;

. $a/.v ;

}

Another approach using ls(1)

lsl(){

_lsl ()

{

ls -l $3 /mnt/*/$1* 2>/dev/null;

};

case $# in

0)

echo "usage: lsl pat [ls-options|result-no]";

echo "usage: lsle pat [sed-cmds]"

;;

1)

_lsl $1 |sed =

;;

2)

case $2 in

-*) _lsl $1 $@;;

*)

_lsl $1 |sed 's/.* //;

'"$2"'!d;

'"$2"'q' > $HOME/.lsl ;

export v=$(sed 1q $HOME/.lsl);

echo \$v = $v

;;

esac

;;

esac;

}

exp(){

echo "%s/\$/ /";

echo "%j";

echo "s/^/export v=\"";

echo "s/\$/\"";

echo "s/ \"\$/\"";

echo ".";

echo "wq";

}

lsle(){

lsl $1 -1 |sed $2 > .lsl&&

exp |ed -s .lsl >&-&&

. .lsl&&

echo \$v = $v;

}

sedi(){ case $# in [01]|[3-9])echo usage: sedi sed-cmds file ;;2)sed -a ''"$1"';H;$!d;g;' $2 |sed -a '/^$/d;w '"$2"'' ;;esac;}
mem(){ { case $1 in st*|[vgid]*) printf "%s " "$@";; *) dd if=$3 2>&1|sed '$!d;/^0/d;s/ .*//;s/^/'"$1"' '"$2"' 1 0 /; r '"$3"'' 2>/dev/null;;esac;printf "\r\nquit\r\n";}|nc -n 127.0.0.1 11211; }
2011-06-17 06:39:07
User: argv
Functions: dd printf sed
5

usage: mem memcache-command [arguments]

where memcache-command might be:

set

add

get[s]

append

prepend

replace

delete

incr

decr

cas

stats

verbosity

version

notes:

exptime argument is set to 0 (no expire)

flags argument is set to 1 (arbitrary)

se(){ while read a;do [ "$a" != "${a#*$@*}" ]&&echo $a;done ;} # usage: se pattern # use in place of sed /pat/!d where RE are overkill
2011-04-06 03:37:40
User: argv
Functions: read sed
1

POSIX requires this "string truncating" functionality.

might as well use it, at least for very small tasks where invoking sed and using RE is overkill.

cls(){ printf "\33[2J";} or, if no printf, cat >cls;<ctrl-v><ctrl+[>[2J<enter><ctrl+d> cls(){ cat cls;}
2011-04-06 01:51:45
User: argv
Functions: cat printf
13

this leaves the cursor at the bottom of the terminal screen, where your eyes are.

ctrl-l moves it to the top, forcing you to look up.

tcp(){ tcpdump -nUs0 -w- -iinterface $1|tcpdump -n${2-A}r- ;} usage: tcp '[primitives]' [X|XX]
2011-03-07 03:40:11
User: argv
Functions: tcpdump
1

Sometimes the question comes up: How to get unbuffered tcpdump output into the next program in the pipe? i.e. if your OS forces you to wait for the buffer to fill before the next program sees any of the output

If you use -Uw- then you can't use -A (or -X or -XX) at the same time.

When the question comes up, I've never seen anyone suggest this simple solution: chaining 2 tcpdump instances.

svn co http://simile.mit.edu/repository/crowbar/trunk&& cd ./trunk/xulapp/ xulrunner --install-app && Xvfb :1 && DISPLAY=:1 xulrunner application.ini 2>/dev/null 1>/dev/null && wget -O- "127.0.0.1:10000/&url=http://www.facebook.com"
2010-10-16 05:12:11
User: argv
Functions: cd wget
-1

some other options:

&delay=1000

&mode=links

much more with piggybank as scraper

works well with your favourite curses or non-curses http clients

liveh(){ tcpdump -lnAs512 ${1-} tcp |sed ' s/.*GET /GET /;s/.*Host: /Host: /;s/.*POST /POST /;/[GPH][EOo][TSs]/!d;w '"${2-liveh.txt}"' ' >/dev/null ;} # usage: liveh [-i interface] [output-file] && firefox &
2010-10-11 01:01:11
User: argv
Functions: sed tcpdump
5

Default output-file is "liveh.txt".

This uses only BRE, in case you're using an older version of sed(1) that doesn't have support for ERE added.

With a modern sed(1), to reduce false positive matches, you might do something like:

liveh(){ tcpdump -lnnAs512 -i ${1-} tcp |sed 's/.*GET /GET /;s/.*Host: /Host: /;s/.*POST /POST /;/GET |Host: |POST /!d;/[\"'"'"]/d;/\.\./d;w '"${2-liveh.txt}"'' >/dev/null ;}

Anyway, it's easy to clean up the output file with sed(1) later.

rd(){ while read a ;do printf "$a\n";sleep ${1-1};done ;} # usage: rd < file ; or ... | rd
2010-10-03 04:16:03
User: argv
Functions: file printf read sleep
2

usage examples

ls largedir |rd

lynx -dump largewebsite.com |rd

rd < largelogfile

ram() { for i in /tmp /altroot;do mount -t tmpfs tmpfs $i;done&& for i in /var /root /etc $HOME; do find -d $i |cpio -pdmv /tmp&& mount -t tmpfs tmpfs $i&& mv -v /tmp$i/* $i&& rm -vrf /tmp$i ; done ;} usage: (in rc sequence) ram
2010-08-31 08:25:55
User: argv
Functions: cpio find mount mv rm
4

if you use disk-based swap then it can defeat the purpose of this function.

jb() { if [ -z $1 ];then printf 'usage:\njb <"date and/or time"> <"commandline"> &\nsee parsedate(3) strftime(3)\n';else t1=$(date +%s); t2=$(date -d "$1" +%s) ;sleep $(expr $t2 - $t1);$2 ;fi ;}
2010-08-26 23:50:42
User: argv
Functions: date expr printf
2

EXAMPLES

jb "next sun 12pm" "/bin/sh ~you/1.sh" &

jb "2010-08-29 12:00:00" "~you/1.sh" &

jb "29aug2010 gmt" ". ~you/1.sh" &

jb 12:00p.m. "nohup ./1.sh" &

jb 1min "echo stop!" &

SEE ALSO

parsedate(3) strftime(3)

st() { LDFLAGS=-static CFLAGS=-static CXXFLAGS=-static NOSHARED=yes ./configure $@ ;} usage: st [configure operands]
cls(){ printf "\033c";} or, if no printf, cat > c ;<ctrl+v> <ctrl+[>c <enter><ctrl-d> c(){ cat c;} #usage: c