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 python from sorted by
Terminal - Commands using python - 59 results
find . -name '*.mp3' | sort | while read -r mp3; do echo -e "<h3>$mp3</h3>\n<audio controls src=\"$mp3\"></audio>"; done > index.html; python -m http.server
2014-03-24 15:01:49
User: hendry
Functions: echo find python read sort
Tags: audio browser
1

I tried a few curses based mp3 players for playing back choir practice songs for my wife.

Unfortunately none of the ones I tried were capable of scrubbing a track.

Firefox saves the day.

echo "select a, b, c from table where a = 3;"| python -c "import sys;import sqlparse;print sqlparse.format(sys.stdin.read(), reindent=True, keyword_case='upper')"
2014-02-21 23:17:15
User: hide1713
Functions: echo python
1

You need to apt-get install python-sqlparse. This command simply formats a sql query and prints it out. It is very useful when you want to move a sql query from commandline to a shell script. Everything is done locally, so you don't need to worry about copying sql query to external websites.

curl -s http://www.census.gov/popclock/data/population/world | python -c 'import json,sys;obj=json.load(sys.stdin);print obj["world"]["population"]'
python -c "a='$(ps -u luke | grep Viber.exe)';b= a.split(' ')[1];import os;os.system('cpulimit -l 1 -p '+b)"
cat foo.json | python -mjson.tool
2013-05-06 20:11:05
User: jeffgage
Functions: cat python
Tags: json
1

For situations where you keep JSON in a VCS and you want your diffs to be sane, such as within a Chef configuration repo.

python -c 'import sys, yaml, json; yaml.dump(json.load(sys.stdin), sys.stdout, default_flow_style=False)' < file.json > file.yaml
2013-04-24 00:31:39
User: tebeka
Functions: python
Tags: python json yaml
4

Convert JSON to YAML.

Note that you'll need to have PyYaml installed.

python -c 'import sys, yaml, json; json.dump(yaml.load(sys.stdin), sys.stdout, indent=4)' < file.yaml > file.json
2013-04-24 00:28:55
User: tebeka
Functions: python
Tags: python json yaml
0

Converts YAML file to JSON.

Note that you'll need to install PyYAML. Also some YAML data types (like dates) are not supported by JSON).

python -c "help('modules')"
FILE='mp3.list';LNNO=`wc -l $FILE|cut -d' ' -f 1`;LIST=( `cat $FILE` );for((;;)) do SEED=$((RANDOM % $LNNO));RNNO=$(python -c "print int('`openssl rand -rand ${LIST[$SEED]} 8 -hex 2>/dev/null`', 16) % $LNNO");mplayer ${LIST[$RNNO]};sleep 2s; done
cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 18 | head -1 | python -c "import sys,crypt; stdin=sys.stdin.readline().rstrip('\n'); print stdin;print crypt.crypt(stdin)"
2012-11-09 00:40:22
User: cnyg
Functions: cat fold head python tr
1

Generate a 18 character password from character set a-zA-Z0-9 from /dev/urandom, pipe the output to Python which prints the password on standard out and in crypt sha512 form.

python -m SimpleHTTPServer 8000 &; sudo localtunnel -k ~/.ssh/id_rsa.pub 8000
2012-10-30 17:52:14
User: dzup
Functions: python sudo
0

you need ruby gems and localtunnel, further instruction on their website

for ubuntu precise, just intall ...

sudo apt-get install ruby ruby1.8-dev rubygems1.8 libopenssl-ruby

sudo gem install localtunnel

ssh-keygen -t rsa

python -m SimpleHTTPServer 8000

sudo localtunnel -k ~/.ssh/id_rsa.pub 8000

python -c 'import googl; print googl.Googl("<your_google_api_key>").shorten("'$someurl'")[u"id"]'
2012-05-31 17:14:17
User: shr386
Functions: python
1

(1) required: python-googl ( install by: pip install python-googl )

(2) get from google API console https://code.google.com/apis/console/

tail -F /var/log/nginx/access.log | python -c 'exec("import sys,time\nl=0\ne=int(time.time())\nfor line in sys.stdin:\n\tt = int(time.time())\n\tl += 1\n\tif t > e:\n\t\te = t\n\t\tprint l\n\t\tl = 0")'
2012-05-15 21:56:46
User: pykler
Functions: python tail
0

Realtime lines per second in a log file using python ... identical to perl version, except python is much better :)

python -c 'print open("path/to/image.png", "rb").read().encode("base64").replace("\n","")'
2012-04-20 13:50:53
User: leonjh
Functions: python
2

useful if you are using lots of data URI's in your css files

while read ; do python <script> ; done
2012-02-23 22:29:09
User: Zulu
Functions: python read
-2

Very useful for test a script. After launch this command, you only have to press ENTER for launch your script again. I work with screen and tape ENTER instead of '!!'+ENTER

If you break your script with CTRL-C, it will wait for press ENTER and will re-launch

You can write like it : while read -p "Press ENTER" ; do python ; done

python -c 'print "hello".encode("hex")'
2011-12-13 23:05:17
User: atoponce
Functions: python
2

You can use "decode()" in a similar manner:

python -c 'print "68656c6c6f".decode("hex")'
python -c 'print hex(1337)'
2011-12-13 22:03:10
User: atoponce
Functions: python
0

Python is always such much more readable than most shell scripting.

python -c "from uuid import UUID; print UUID('63b726a0-4c59-45e4-af65-bced5d268456').hex;"
2011-11-20 10:35:44
User: mackaz
Functions: python
-2

Remove dashes, also validates if it's a valid UUID (in contrast to simple string-replacement)

python -c 'import string, random; print "".join(random.choice(string.letters+string.digits) for x in range(6))'
echo '{"json":"obj"}' | python -mjson.tool
python -c "import sys;print (sys.byteorder) + ' endian'"
python -ic "from __future__ import division; from math import *; from random import *"
2011-10-24 19:47:27
User: fpunktk
Functions: python
1

This opens a python command line. You can use math and random and float-division is enabled (without appending .0 to integers). I just don't know how to specify a standard precision.

python -c 'p="SeCuR3PwD";import hashlib as h;s=h.md5(p).hexdigest()[:2];pw=h.md5(s+p).hexdigest();print pw+":"+s;'
2011-10-16 18:49:08
User: Xiol
Functions: as python
1

ZenCart uses a MD5 with a salt to secure its passwords. If you need to forcibly change someone's password to a known value within the database, this one-liner can generate the password. Change the value of 'p' to the password you want.

python -c'for i in range(1,101):print"FizzBuzz"[i*i%3*4:8--i**4%5]or i'
2011-10-12 21:15:35
User: atoponce
Functions: python
5

A common programming question for interviewers to ask potential job candidates is to code "FizzBuzz". That is, if a number is divisible by 3, then it should display "Fizz". If a number is divisible by 5, it should display "Buzz". If it is divisible by both, then it should display "FizzBuzz". Otherwise, display the current number between 1 and 100.

python -m SimpleHTTPServer
2011-08-29 03:19:26
Functions: python
Tags: python
3

This works on all versions of python 2.X.

Tested on Linux and bundled python versions on Mac OSX and Solaris / UNIX

Note: Serves globally on port 8000.

Ctrl+c to stop.

Don't start the server and leave it on a internet connected machine. :)