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,256 results
(IFS=:;for p in $PATH; do test -d $p || echo $p; done)
2009-09-19 17:51:06
User: haivu
Functions: echo test
Tags: bash PATH
9

I often need to know of my directory in the PATH, which one DOES NOT exist. This command answers that question

* This command uses only bash's built-in commands

* The parentheses spawn a new sub shell to prevent the modification of the IFS (input field separator) variable in the current shell

echo -ne "\033]0;`id -un`:`id -gn`@`hostname||uname -n|sed 1q` `who -m|sed -e "s%^.* \(pts/[0-9]*\).*(\(.*\))%[\1] (\2)%g"` [`uptime|sed -e "s/.*: \([^,]*\).*/\1/" -e "s/ //g"` / `ps aux|wc -l`]\007"
2009-09-19 06:57:53
User: AskApache
Functions: echo
4

I usually have 5 or more ssh connections to various servers, and putting this command in my .bash_profile file makes my putty window or x terminal window title change to this easily recognizable and descriptive text. Includes the username, group, server hostname, where I am connecting from (for SSH tunneling), which device pts, current server load, and how many processes are running.

You can also use this for your PROMPT_COMMAND variable, which updates the window title to the current values each time you exec a command.

I prefix running this in my .bash_profile with

[[ ! -z "$SSH_TTY" ]] &&

which makes sure it only does this when connecting via SSH with a TTY.

Here's some rougher examples from http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html

# If set, the value is executed as a command prior to issuing each primary prompt. #H=$((hostname || uname -n) 2>/dev/null | sed 1q);W=$(whoami) #export PROMPT_COMMAND='echo -ne "\033]0;${W}@${H}:${PWD/#$HOME/~} ${SSH_TTY/\/dev\//} [`uptime|sed -e "s/.*: \([^,]*\).*/\1/" -e "s/ //g"`]\007"' #PROMPT_COMMAND='echo -ne "\033]0;`id -un`:`id -gn`@`hostname||uname -n 2>/dev/null|sed 1q` `command who -m|sed -e "s%^.* \(pts/[0-9]*\).*(\(.*\))%[\1] (\2)%g"` [`uptime|sed -e "s/.*: \([^,]*\).*/\1/" -e "s/ //g"` / `command ps aux|wc -l`]\007"' #[[ -z "$SSH_TTY" ]] || export PROMPT_COMMAND #[[ -z "$SSH_TTY" ]] && [[ -f /dev/stdout ]] && SSH_TTY=/dev/stdout

And here's a simple function example for setting the title:

function set_window_title(){ echo -e "\033]0; ${1:-$USER@$HOST - $SHLVL} \007"; }
for p in ${PATH//:/ }; do [[ -d $p && -x $p ]] && echo $p; done
2009-09-19 06:43:57
User: AskApache
Functions: echo
1

Finds executable and existing directories in your path that can be useful if migrating a profile script to another system. This is faster and smaller than any other method due to using only bash builtin commands.

See also:

+ http://www.commandlinefu.com/commands/view/743/list-all-execs-in-path-usefull-for-grepping-the-resulting-list

+ http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html

for s in `seq -f %02.0f 5 15`; do echo $s; done
2009-09-18 13:21:38
User: Yeraze
Functions: echo
Tags: bash seq
0

Uses 'seq' with formatting parameter to generate the necessary padded sequence. Change '%02.0f' to how many digits you need (for 3, use %03.0f, etc) and replace 5 & 15 with your desired min and max.

echo "Decode this"| tr [a-zA-Z] $(echo {a..z} {A..Z}|grep -o .|sort -R|tr -d "\n ")
for i in {0..1}{0..9}; do echo $i; done
2009-09-18 02:51:12
User: dennisw
Functions: echo
Tags: bash strings
-1

Bash 4 will let you do {00..19} to get leading zeros, but Bash 3 doesn't have that feature. This technique gets you partway there (the sequences need be such that the last digit ranges from zero to nine - you can't use this for something like Bash 4's {03..27}, for example). When this limitation is not a problem, you can avoid some complicated string manipulation for concatenating leading zeros.

You can add more digits like this: {0..1}{0..9}{0..9} (ranges from 0 to 99 with up to two leading zeros). To pad with additional zeros:

for i in 000{0..1}{0..9}; do echo $i; done

or

for i in {0..1}{0..9}; do echo "000$i"; done

This is useful for creating values to sort or for creating filenames with a fixed format. Note that this will also work:

touch {0..1}{0..9}
echo 127.0.0.1 | egrep -e '^(([01]?[0-9]{1,2}|2[0-4][0-9]|25[0-4])\.){3}([01]?[0-9]{1,2}|2[0-4][0-9]|25[0-4])$'
2009-09-17 17:40:48
User: arcege
Functions: echo egrep
-1

Handles everything except octets with 255. Ran through ip generator with variable octet lengths.

echo 254.003.032.3 | grep -P '^((25[0-4]|2[0-4]\d|[01]?[\d]?[1-9])\.){3}(25[0-4]|2[0-4]\d|[01]?[\d]?[1-9])$'
2009-09-17 12:59:44
User: foob4r
Functions: echo grep
0

This obey that you don't match any broadcast or network addresses and stay between 1.1.1.1 - 254.254.254.254

echo "123.32.12.134" | grep -P '([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])'
find repMainPath -maxdepth 1 -mindepth 1 -type d | while read dir; do echo processing $dir; sudo svnadmin dump --deltas $dir >dumpPath/`basename $dir`; done
2009-09-15 20:14:51
User: Marco
Functions: dump echo find read sudo
Tags: bash svn
2

This command dumps all SVN repositories inside of folder "repMainPath" (not recursively) to the folder "dumpPath", where one dump file will be created for each SVN repository.

echo a,b,c | sed -e s/,/\',\'/g -e s/^/\(\'/ -e s/$/\'\)/
echo {'1,2,3',4}" o'clock" ROCK
2009-09-15 10:12:49
User: danam
Functions: echo
5

AFAIR this is the wording ;)

find -type f |while read a;do [ "`head -c3 -- "${a}"`" == $'\xef\xbb\xbf' ] && echo "Match: ${a}";done
[ -z "$VAR" ] && echo "VAR has not been set" && exit 1
echo {1..3}" o'clock" ROCK
echo "$(</etc/issue)"
2009-09-14 09:44:35
User: mobidyc
Functions: echo
-2

another replacement for cat.

echo foo | netcat 192.168.1.2 25
2009-09-13 01:33:02
User: pykler
Functions: echo
2

Using netcat, usuallly installed on debian/ubuntu.

Also to test against a sample server the following two commands may help

echo got milk? | netcat -l -p 25

python -c "import SocketServer; SocketServer.BaseRequestHandler.handle = lambda self: self.request.send('got milk?\n'); SocketServer.TCPServer(('0.0.0.0', 25), SocketServer.BaseRequestHandler).serve_forever()"

echo "foo" > /dev/tcp/192.168.1.2/25
2009-09-12 16:48:05
User: mobidyc
Functions: echo
23

this command will send a message to the socket 25 on host 192.168.1.2 in tcp.

works on udp and icmp

understand only IP address, not hostname.

on the other side (192.168.1.2), you can listen to this socket and test if you receive the message.

easy to diagnose a firewall problem or not.

for L in `echo :$PATH | tr : '\n'`; do F=${L:-"."}/fileName; if [ -f ${F} -o -h ${F} ]; then echo ${F}; break; fi; done
2009-09-11 16:14:36
User: arcege
Functions: echo tr
-1

Searches in order of the directories of $PATH. Stops after finding the entry; looks for only that fileName. Works in Bourne, Korn, Bash and Z shells.

function sepath { echo $PATH |tr ":" "\n" |sort -u |while read L ; do cd "$L" 2>/dev/null && find . \( ! -name . -prune \) \( -type f -o -type l \) 2>/dev/null |sed "s@^\./@@" |egrep -i "${*}" |sed "s@^@$L/@" ; done ; }
2009-09-11 15:03:22
User: mobidyc
Functions: cd echo egrep find read sed sort tr
Tags: bash ksh PATH
-1

search argument in PATH

accept grep expressions

without args, list all binaries found in PATH

echo "vertical text" | grep -o '.'
2009-09-11 03:45:04
User: dennisw
Functions: echo grep
11

Define a function

vert () { echo $1 | grep -o '.'; }

Use it to print some column headers

paste <(vert several) <(vert parallel) <(vert vertical) <(vert "lines of") <(vert "text can") <(vert "be used") <(vert "for labels") <(vert "for columns") <(vert "of numbers")
time echo 'n=1000000;m=(n+1)/2;a=0;b=1;i=0;while(m){e[i++]=m%2;m/=2};while(i--){c=a*a;a=c+2*a*b;b=c+b*b;if(e[i]){t=a;a+=b;b=t}};if(n%2)a*a+b*b;if(!n%2)a*(a+2*b)' | bc
2009-09-10 09:00:44
User: Escher
Functions: echo time
-135

EDIT: Trolling crap removed ;)

takes approx 6 secs on a Core 2 Duo @ 2GHz, and 15 secs on atom based netbooks!

uses monoid (a,b).(x,y)=(ax+bx+ay,ax+by) with identity (0,1), and recursion relations:

F(2n-1)=Fn*Fn+F(n-1)*F(n-1)

F(2n)=(Fn+2*F(n-1))*Fn

then apply fast exponentiation to (1,0)^n = (Fn,F(n-1))

.

Note that: (1,0)^-1=(1,-1) so (a,b).(1,0) = (a+b,a) and (a,b)/(1,0)=(a,b).(1,0)^-1=(b,a-b)

So we can also use a NAF representation to do the exponentiation,http://en.wikipedia.org/wiki/Non-adjacent_form , it's also very fast (about the same, depends on n):

time echo 'n=1000000;m=(n+1)/2;a=0;b=1;i=0;while(m>0){z=0;if(m%2)z=2-(m%4);m=(m-z)/2;e[i++]=z};while(i--){c=a*a;a=c+2*a*b;b=c+b*b;if(e[i]>0){t=a;a+=b;b=t};if(e[i]<0){t=a;a=b;b=t-b}};if(n%2)a*a+b*b;if(!n%2)a*(a+2*b)' | bc
time echo 'n=70332;m=(n+1)/2;a=0;b=1;i=0;while(m){e[i++]=m%2;m/=2};while(i--){c=a*a;a=c+2*a*b;b=c+b*b;if(e[i]){t=a;a+=b;b=t}};if(n%2)a*a+b*b;if(!n%2)a*(a+2*b)' | bc
2009-09-10 08:58:47
User: Escher
Functions: echo time
-136

Calculates nth Fibonacci number for all n>=0, (much faster than matrix power algorithm from http://everything2.com/title/Compute+Fibonacci+numbers+FAST%2521 )

n=70332 is the biggest value at http://bigprimes.net/archive/fibonacci/ (corresponds to n=70331 there), this calculates it in less than a second, even on a netbook.

UPDATE: Now even faster! Uses recurrence relation for F(2n), see http://en.wikipedia.org/wiki/Fibonacci_number#Matrix_form

n is now adjusted to match Fn at wikipedia, so bigprimes.net table is offset by 1.

UPDATE2: Probably fastest possible now ;), uses a simple monoid operation:

uses monoid (a,b).(x,y)=(ax+bx+ay,ax+by) with identity (0,1), and recursion relations:

F(2n-1)=Fn*Fn+F(n-1)*F(n-1)

F(2n)=Fn*(2*F(n-1)+Fn)

then apply fast exponentiation to (1,0)^n = (Fn,F(n-1))

.

Note that: (1,0)^-1=(1,-1) so (a,b).(1,0) = (a+b,a) and (a,b)/(1,0)=(a,b).(1,0)^-1=(b,a-b)

So we can also use a NAF representation to do the exponentiation,http://en.wikipedia.org/wiki/Non-adjacent_form , it's also very fast (about the same, depends on n):

time echo 'n=70332;m=(n+1)/2;a=0;b=1;i=0;while(m>0){z=0;if(m%2)z=2-(m%4);m=(m-z)/2;e[i++]=z};while(i--){c=a*a;a=c+2*a*b;b=c+b*b;if(e[i]>0){t=a;a+=b;b=t};if(e[i]<0){t=a;a=b;b=t-b}};if(n%2)a*a+b*b;if(!n%2)a*(a+2*b)' | bc
echo -e "${PATH//://\n}" >/tmp/allpath; grep -Fh -f /tmp/allpath /var/lib/dpkg/info/*.list|grep -vxh -f /tmp/allpath >/tmp/installedinpath ; find ${PATH//:/ } |grep -Fxv -f /tmp/installedinpath
2009-09-09 05:33:14
User: kamathln
Functions: echo find grep
Tags: Debian dpkg PATH
0

OS: Debian based (or those that use dpkg)

Equivalent to doing a dpkg -S on each file in $PATH, but way faster.

May report files generated though postinstall scripts and such. For example . It will report /usr/bin/vim .. which is not not a file installed directly by dpkg, but a link generated by alternatives hooks

echo src::${PATH} | awk 'BEGIN{pwd=ENVIRON["PWD"];RS=":";FS="\n"}!$1{$1=pwd}$1!~/^\//{$1=pwd"/"$1}{print $1}'
2009-09-09 04:03:46
User: arcege
Functions: awk echo
Tags: awk echo PATH
-2

Removes trailing newline; colon becomes record separator and newline becomes field separator, only the first field is ever printed. Replaces empty entries with $PWD. Also prepend relative directories (like ".") with the current directory ($PWD). Can change PWD with env(1) to get tricky in (non-Bourne) scripts.