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 true from sorted by
Terminal - Commands using true - 17 results
true | mailx -n -a MYTEXT.txt -r my@mail.com -s log -S smtp=mail.com -S smtp-auth-user=MYUSER -S smtp-auth-password=MYPASSWORD FRIEND@mail.com
2013-03-12 16:37:30
User: xmuda
Functions: mailx true
5

It is the best way i found to send a mail from the console in my centos server.

while true ; do killall mc ; done
2012-04-13 08:03:41
User: unixhero
Functions: killall true
0

I use this loop for a variety of things. If a process won't die, I try to ask it nicely to let it die gracefully, and then i use killall and kill -9 to end its life. This will run the program killall and then start it again when it completes, indefinately.

export GREP_COLOR='1;32';while [ true ]; do head -n 100 /dev/urandom; sleep .1; done | hexdump -C | grep --color=auto "ca fe"
while true ; do echo -n "`date`";curl localhost:3000/site/sha;echo -e;sleep 1; done
2011-10-14 21:00:41
User: donnoman
Functions: echo sleep true
1

In this case it runs the command 'curl localhost:3000/site/sha' waiting the amount of time in sleep, ie: 1 second between runs, appending each run to the console.

This works well for any command where the output is less than your line width

This is unlike watch, because watch always clears the display.

while true ; do sleep 1 ; clear ; (netstat -tn | grep -P ':36089\s+\d') ; done
2011-09-28 11:39:43
User: hute37
Functions: clear grep netstat sleep true
-3

shell loop to scan netstat output avoiding loolback aliases (local/remote swap for local connections)

while true ; do nc -l 80 < index.html ; done
2011-08-31 15:17:33
User: ztank1013
Functions: true
12

Very simple web server listening on port 80 will serve index.html file or whatever file you like pointing your browser at http://your-IP-address/index.html for example.

If your web server is down for maintenance and you'd like to inform your visitors about it, quickly and easily, you just have to put into the index.html file the right HTML code and you are done! Of course you need to be root to run the command using port 80.

pdfnup --nup 2x1 --frame true --landscape --outfile output.pdf input.pdf
2010-12-21 14:20:06
User: TetsuyO
Functions: true
3

This is an example of the usage of pdfnup (you can find it in the 'pdfjam' package). With this command you can save ink/toner and paper (and thus trees!) when you print a pdf.

This tools are very configurable, and you can make also 2x2, 3x2, 2x3 layouts, and more (the limit is your fantasy and the resolution of the printer :-)

You must have installed pdfjam, pdflatex, and the LaTeX pdfpages package in your box.

Confirm() { echo -n "$1 [y/n]? " ; read reply; case $reply in Y*|y*) true ;; *) false ;; esac }
2010-11-22 16:54:16
User: eikenberry
Functions: echo false read true
Tags: read
2

This version works across on all POSIX compliant shell variants.

while [ true ]; do head -n 100 /dev/urandom; sleep .1; done | hexdump -C | grep "ca fe"
true || false && echo true || echo false
2010-04-20 09:17:08
User: Sizeof
Functions: echo false true
Tags: Linux UNIX
1

You can do some boolean logic like

A or B then C else D using

or : ||

and : &&

So you can do some :

# false || false && echo true || echo false

false

# true || false && echo true || echo false

true

# false || true && echo true || echo false

true

# true || true && echo true || echo false

true

and so on ...

I use it like :

(ssh example.com 'test something') || $(ssh example.net 'test something') && echo ok || echo ko
function ends_in_y() { case $(date +%A) in *y ) true ;; * ) false ;; esac } ; ends_in_y && echo ok
2010-04-06 22:18:52
Functions: date echo false true
-1

The shell has perfectly adequate pattern matching for simple expressions.

count="1" ; while true ; do read next ; if [[ "$next" = "$last" ]] ; then count=$(($count+1)) ; echo "$count" ; else count="1" ; echo $count ; fi ; last="$next" ; done
2010-03-30 04:02:29
User: dabom
Functions: echo read true
Tags: bash read Game
8

Really bored during class so I made this...

Basically, you hold period (or whatever) and hit enter after a second and you need to make the next line of periods the same length as the previous line...

My record was 5 lines of the same length.

It's best if you do it one handed with your pointer on period and ring on enter.

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
3

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.

while true ; do du -sk testfile ; sleep 10 ; done
2009-08-14 11:39:52
User: Solaris
Functions: du sleep true
-2

This command produces the output of "du -sk testfile" in every 10 seconds. You can change the command to be whatever you want.

while true ; do IFS="" read i; echo "$i"; sleep .01; done < <(tr -c "[:digit:]" " " < /dev/urandom | dd cbs=$COLUMNS conv=unblock | GREP_COLOR="1;32" grep --color "[^ ]")
2009-07-22 03:59:07
Functions: dd echo grep read sleep tr true
Tags: color
2

Same as above but slooooow it down

true && { echo success;} || { echo failed; }
2009-04-02 01:49:25
Functions: echo true
23

This will perform one of two blocks of code, depending on the condition of the first. Essentially is a bash terniary operator.

To tell if a machine is up:

ping -c1 machine { echo succes;} || { echo failed; }

Because of the bash { } block operators, you can have multiple commands

ping -c1 machine && { echo success;log-timestamp.sh }|| { echo failed; email-admin.sh; }

Tips:

Remember, the { } operators are treated by bash as a reserved word: as such, they need a space on either side.

If you have a command that can fail at the end of the true block, consider ending said block with 'false' to prevent accidental execution

while true ; do scripts/bootstrap.py ; ./manage.py runserver ; done
2009-03-27 04:43:54
User: taurus
Functions: true
-3

I wrote a script called bootstrap.py to delete the database, then load a new database with initial values. With this single-line shell loop, when I need to make a schema change (which happens often in the early stages of some projects), I hit ctrl-C to stop the running Django server, then watch bootstrap.py do its thing, then watch the server restart.