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,211 results
date -d2009-05-18 > /dev/null 2>&1 ; echo $?
2009-05-18 20:30:05
User: GeckoDH
Functions: date echo
2

Important to know: a valid date will return 0, otherwise 1!

echo 2006-10-10 | grep -c '^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]$'
2009-05-11 22:18:43
User: rez0r
Functions: echo grep
-1

Quick and easy way of validating a date format of yyyy-mm-dd and returning a boolean, the regex can easily be upgraded to handle "in betweens" for mm dd or to validate other types of strings, ex. ip address.

Boolean output could easily be piped into a condition for a more complete one-liner.

function echox { echo `tput cup $(($(tput lines))) $(( ($(tput cols) - $(echo "${#1}"))/2 ))`"$1"`tput cup $(tput lines) $(( $(tput cols)-1 ))`; }
2009-05-10 23:24:49
Functions: echo tput
1
function echox { echo `tput cup $(($(tput lines))) $(( ($(tput cols) - $(echo "${#1}"))/2 ))`"$1"`tput cup $(tput lines) $(( $(tput cols)-1 ))`; }

echox prints given argument on bottom line center screen in terminal

function echoxy { echo `tput cup $(($(tput lines)/2)) $(( ($(tput cols) - $(echo "${#1}"))/2))`"$1"`tput cup $(tput lines) $(( $(tput cols)-1 ))`; }

exhoxy prints given argument center screen

function echos { echo `tput cup $(($(tput lines)-2)) $(($(tput cols)-$(echo ${#1})))&&tput sc`"$1"`tput cup $(($(tput lines)-2)) 0 && tput rc`; } while [ 1 ]; do echos "`date`"; done

echos prints date and time on second from last line (used as status message)

you can easily use these functions by placing them in your .bashrc file, make sure to source your .bashrc once you do

echo '{"json":"obj"}' | python -m simplejson.tool
2009-05-10 13:58:05
User: azeey
Functions: echo python
Tags: python json
10

You can use a site like http://www.jsonlint.com/ or use the command line to validate your long and complex json data. This is part of the simplejson package for python http://undefined.org/python/#simplejson.

Wrong json expression example:

echo '{ 1.2:3.4}' | python -m simplejson.tool

Expecting property name: line 1 column 2 (char 2)

for i in `seq -w 1600` ; do links -dump http://www.robsjokes.com/$i/index.html | sed '/Random Joke/,/Next Joke/!d' | sed '/^$/,/^$/!d' >> ~/temp/Rob.jokes ; echo '%' >> ~/temp/Rob.jokes ; done
echo $?
2009-05-09 17:32:23
Functions: echo
11

The variable ? contain the last exit status of a command, it can be printed for troubleshooting purpose.

echo 00{1..9} 0{10..99} 100
screen -dmS "name_me" echo "hi"
2009-05-06 02:04:15
Functions: echo screen
3

Runs an instance of screen with name of "name_me" and command of "echo "hi""

To reconnect to screen instance later use:

screen -r name_me

gnuplot -persist <(echo "plot '<(sort -n listOfNumbers.txt)' with lines")
2009-05-02 13:46:02
User: penthief
Functions: echo
21

Useful when you've produced a large file of numbers, and want to quickly see the distribution. The value of y halfway along the x axis is the median. Simple!

Just create the listOfNumbers.txt file with a number on each line to try it out.

perm=( 6 4 4 ) ; for elem in ${perm[@]}; do echo `expr 7 - $elem` ; done
2009-05-01 12:08:19
Functions: echo
1

Umask is obtained subtracting 7 from each cypher of octal format. I store octal perm format in an array,then for each element of array I subtract 7. The result is the umask.

_llines=100; while [ $_llines -gt 1 ]; do echo $_llines; _llines=$(($_llines-1)); done
2009-04-29 00:19:45
User: romulusnr
Functions: echo
-2

You've opened a terminal window and you've connected off to a remote host that didn't pick up your terminal size, and all your curses and paging apps are screwed up as a result. You need to quickly determine how many lines are in your current terminal view (to feed into "stty rows X").

echo "screen -DR" >> ~/.bash_profile
2009-04-28 20:45:37
User: sud0er
Functions: echo
Tags: screen
11

From screen's manpage: "Attach here and now. In detail this means: If a session is running, then reattach. If necessary detach and logout remotely first. If it was not running create it and notify the user. This is the author's favorite."

Toss this in your ~/.bash_profile so that you never have that "oh crap" moment where you wanted to run something in screen and didn't.

echo "Hello world" | base64
2009-04-27 15:00:08
User: P17
Functions: echo
Tags: encoding
-3

Also works with files:

cat file

Hello world

base64 file

SGVsbG8gd29ybGQK

To decrypt use the -d option:

echo SGVsbG8gd29ybGQK | base64 -d

Hello world

for i in /sys/class/net/*; do e=`basename $i`; echo "# $e"; sudo ethtool $e | grep -E "Link|Speed" ; done
2009-04-25 14:32:08
User: hank
Functions: echo grep sudo
Tags: Network Linux
0

Basically an improvement on an earlier ethtool command line.

echo "A great password" | md5sum
2009-04-24 14:32:56
User: ubersoldat
Functions: echo
-5

You can also use sha1sum and variants for longer passwords

for M in 0 1 2 3 ; do echo eth$M ;/sbin/ethtool eth$M | grep -E "Link|Speed" ; done
2009-04-22 19:32:41
User: Wubby
Functions: echo grep
6

Give the Speed and Link status of eth# 0-3. This is sort of what mii-tool does, but eth-tool is better, yet lacks device discovery.

echo *
2009-04-17 21:40:58
User: kFiddle
Functions: echo
Tags: echo ls
-5

Sometimes "ls" is just too slow, especially if you're having problems with terminal scroll speed, or if you're a speed freak. In these situations, do an echo * in the current directory to immediately see the directory listing. Do an echo * | tr ' ' '\n' if you want a column. Do an alias ls='echo *' if you want to achieve higher echelons of speed and wonder. Note that echo * is also useful on systems that are so low in memory that "ls" itself is failing - perhaps due to a memory leak that you're trying to debug.

echo -e "aa\nbb\ncc\ndd\n123" | sed -e :a -e "/$/N; s/\n/;/; ta"
echo "12345,12346" |sed -e's/ //'|tr "," "\n"| while read line; do echo -n $line" "; svn log -vr $line https://url/to/svn/repository/|grep "/"|head -1|cut -d"/" -f2; done
echo 'wget url' | at 12:00
#!/bin/sh #du.sh i=`hostname -i` df -h > /tmp/space.txt echo "server $i " >> /tmp/space.txt uuencode /tmp/space.txt space.txt | mail -s "HDD usage $i" email@email.com
for i in $( find . ); do echo zipping file: $i zip $i.zip $i done
2009-04-07 20:37:49
User: archlich
Functions: echo find
-4

Note that this will not work with files with spaces or characters that need to be escaped. Feel free to leave any comments to improve upon this command, and I'll add it in.

Thanks!

find . \( -type d -name .svn -prune \) -o -print | while read file ; do mergeinfo=`svn propget svn:mergeinfo $file` ; [ "$mergeinfo" != "" ] && echo -e "$file\n $mergeinfo\n" ; done
read -p 'Username: ' u;sudo -H -u $u xauth add $(xauth list|grep :$(echo ${DISPLAY: -4:2}));sudo su - $u
echo $( (( $( (2**31 -1) ) - $(date +%s) )) )
2009-04-02 05:14:23
User: Chartreuse
Functions: date echo
Tags: bash echo date
0

Echos the number of seconds from the current time till the specified time (Example in command is (2**31-1)) aka the Unix epoch. Just replace that number with the specified date (in seconds past Jan. 1st 1970) and it will return the seconds.

NOTE: Only works in bash