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 bc from sorted by
Terminal - Commands using bc - 38 results
openssl rand -hex 1 | tr '[:lower:]' '[:upper:]' | xargs echo "obase=2;ibase=16;" | bc | cut -c1-6 | sed 's/$/00/' | xargs echo "obase=16;ibase=2;" | bc | sed "s/$/:$(openssl rand -hex 5 | sed 's/\(..\)/\1:/g; s/.$//' | tr '[:lower:]' '[:upper:]')/"
2013-10-22 08:40:46
User: 4fd
Functions: bc cut echo sed tr xargs
0

I did not come up with this one myself, but found this somewhere else several months ago.

bc <<<"..."
echo 'scale=10; 4*a(1)' | bc -l
2012-10-08 15:03:07
User: chars19
Functions: bc echo
Tags: bc -l
1

a() function in bc is for arctangent, available if using -l option included.

echo `disklabel mfid1s4 | sed -n '$p' | awk '{print $2}'` / 1024 / 1024 | bc -l
hexdec() { bc <<< "obase=10; ibase=16; $1"; }
hex() { bc <<< "obase=16; $1"; }
2011-12-09 13:57:05
User: bjomape
Functions: bc
Tags: bash hex bc numbers
4

Use the standard calculator bc to convert decimals to hex

function right { bc <<< "obase=8;ibase=2;$1"; }; touch foo; chmod $(right 111111011) foo; ls -l foo
2011-11-16 22:43:31
User: nerd
Functions: bc chmod ls touch
0

I simply find binary notation more straightforward to use than octal in this case.

Obviously it is overkill if you just 600 or 700 all of your files...

read c; while [ -n "$c" ]; do clear; echo -e "$c = "$(echo "$c" |bc -l)"\n"; read c; done
memnum=$(awk '{ print $2 }' /proc/meminfo |head -n1); echo "$memnum / 1024 / 1024" | bc -l
2011-11-08 16:28:25
User: wekoch
Functions: awk bc echo head
-2

Probably more trouble than its worth, but worked for the obscure need.

calc() { echo "scale=4; ${*//,/.}" | bc -l; }
2011-10-24 19:58:20
User: fpunktk
Functions: bc echo
-1

This is an "argument calculator" funktion. The precision is set to 4 and you can use dot (.) or comma (,) as decimal mark (which is great for german users with a comma on the numpad).

bc <<< 'obase=60;299'
2011-10-21 13:15:17
User: mathias
Functions: bc
22

This is a very simple way to input a large number of seconds and get a more useful value in minutes and seconds. Avoids useless use of echo.

bc <<< `uptime | sed -e 's/^.*up //' -e 's/[^0-9:].*//' | sed 's/:/*60+/g'`
2011-08-29 10:24:43
User: totti
Functions: bc sed
1

Want to run scripts/programs in the system after starting X minute [ For letting the system to free ]? This will give uptime in minute.

parallel echo -n {}"\ "\;echo '$(du -s {} | awk "{print \$1}") / $(find {} | wc -l)' \| bc -l ::: *
for c in gcc bison dialog bc asdf; do if ! which $c >/dev/null; then echo Required program $c is missing ; exit 1; fi; done
paste -sd'+' file|bc -l
seq 1 2 99999999 | sed 's!^!4/!' | paste -sd-+ | bc -l
2011-02-09 23:36:07
User: flatcap
Functions: bc paste sed seq
Tags: sed seq bc paste math
0

Calculate pi from the infinite series 4/1 - 4/3 + 4/5 - 4/7 + ...

This expansion was formulated by Gottfried Leibniz: http://en.wikipedia.org/wiki/Leibniz_formula_for_pi

I helped rubenmoran create the sum of a sequence of numbers and he replied with a command for the sequence: 1 + 2 -3 + 4 ...

This set me thinking. Transcendental numbers!

seq provides the odd numbers 1, 3, 5

sed turns them into 4/1 4/3 4/5

paste inserts - and +

bc -l does the calculation

Note: 100 million iterations takes quite a while. 1 billion and I run out of memory.

echo "e("{1..8}");" | bc -l
echo `lcg-infosites --vo lhcb ce | cut -f 1| grep [[:digit:]]| tr '\n' '+' |sed -e 's/\ //g' -e 's/+$//'`|bc -l
2010-11-10 15:06:00
User: kbat
Functions: bc cut echo grep sed tr
-2

Of course, this command must be executed at a GRID User Interface

lhcb - name of your VO, substitute it with the one you are interested it.

echo $(echo 'scale=2; ' '100 * ' $(eix --only-names -I | wc -l) / $(eix --only-names | wc -l) | bc -l)%
calc() { bc <<< $*; }
seq 8 | awk '{print "e(" $0 ")" }' | bc -l
2010-08-14 02:52:39
User: polar
Functions: awk bc seq
Tags: awk seq bc
0

If you want a sequence that can be plotted, do:

seq 8 | awk '{print "e(" $0 ")" }' | bc -l | awk '{print NR " " $0}'

Other bc functions include s (sine), c (cosine), l (log) and j (bessel). See the man page for details.

sensible-browser http://www.pythonchallenge.com/pc/def/$(bc <<< 2^38).html
2010-02-02 00:53:18
User: kzh
Functions: bc
-1

This is the answer to the 0th problem from the python challenge < http://www.pythonchallenge.com/ >. Replace sensible-browser with firefox, w3m or whatever.

echo $(echo $(seq $MIN $MAX) | sed 's/ /+/g') | bc -l
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

mplayer -vo dummy -ao dummy -identify * 2>&1 | grep ID_LENGTH | sed 's/.*=\([0-9]*\)/\1/' | xargs echo | sed 's/ /+/g' | bc | awk 'S=$1; {printf "%dh:%dm:%ds\n",S/(60*60),S%(60*60)/60,S%60}'
2009-09-24 10:33:19
User: Strawp
Functions: awk bc echo grep sed xargs
5

You're behind on your TV catch-up, but how far behind? This command tries to open mplayer against all files in the current dir. If it's a video file it will contain ID_LENGTH, which is summed and output in hours, minutes and seconds.

Someone better at awk could probably reduce this down a lot.