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 tagged python from sorted by
Terminal - Commands tagged python - 51 results
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).

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.

alias calc='python -ic "from math import *; from random import *"'
2011-10-24 08:15:41
User: Bonster
Functions: alias
0

use python as calculator, press ctrl+d to exit

reminder: when doing factions add atleast one decimal number like so

22.0/7 or 22/7.0

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. :)

python3 -m http.server
$sudo aptitude install python-virtualenv; virtualenv --no-site-packages jpaenv; source jpaenv/bin/activate
find . -type f -name "*.pyc" -delete;
:ret
2010-12-13 05:03:10
User: light13
Tags: vim python
8

usage:

:[rang]ret[!][tabstop value]

python is indent sensitive, after command

:set list

you may see your codes are mixed with tab and space

ret can help you to convert space to tab or tab to space

calc() { bc <<< $*; }
calc() { python -c "from math import *; print $1"; }
2010-10-07 08:26:39
User: asmaier
Functions: python
1

This function defines a command line calculator that handles everything pythons math module can handle, e.g. trigonometric functions, sqrt, log, erf, ... (see http://docs.python.org/library/math.html). It even knows about the constants pi and e.

vim ... :nmap <F5> :w^M:!python %<CR>
2010-09-03 18:44:21
User: duxklr
Functions: vim
Tags: vim python
1

This will save and execute your python script every time your press the F5 function key.

It can also be added to your .vimrc:

autocmd BufRead *.py nmap :w^M:!python %

NOTE: the ^M is not just caret-M, it can be created by type: ctrl-v ctrl-m

filterous -dntb --tag Bash < bookmarks.xml
2010-07-07 07:42:11
User: l0b0
2

You can install filterous with

sudo apt-get install libxslt1-dev; sudo easy_install -U filterous
curl -s "http://feeds.delicious.com/v2/json?count=5" | python -m json.tool | less -R
2010-03-24 09:15:12
User: keimlink
Functions: less python
11

Validates and pretty-prints the content fetched from the URL.

python -c "import platform; print platform.node()"
pydoc -p 8888 & gnome-open http://localhost:8888
2010-02-28 07:01:43
User: haivu
Tags: python help
12

I use this command to start a local Python document server over HTTP port 8888.

python -c "import socket; s=socket.socket(socket.AF_INET, socket.SOCK_DGRAM); s.connect(('google.com', 80)); print s.getsockname()[0]"
2009-12-29 19:17:03
User: whiteinge
Functions: python
-11

Requires Python; doesn't require a specific server to return your IP.

python2 <<< $'import json\nf = open("sessionstore.js", "r")\njdata = json.loads(f.read())\nf.close()\nfor win in jdata.get("windows"):\n\tfor tab in win.get("tabs"):\n\t\ti = tab.get("index") - 1\n\t\tprint tab.get("entries")[i].get("url")'
2009-12-28 21:56:05
User: sputnick
Tags: firefox python
3

You have to do that before :

cd ~/.mozilla/firefox/<YOUR PROFILE>

Require bash. If you use something else, you may use

echo <CODE> | python

Forked from ArkSeth python script.

xmlproc_parse.python-xml &>/dev/null <FILE> || exit 1
2009-12-11 17:30:03
User: sputnick
Functions: exit
Tags: python xml
-1

For debian likes, that's in python-xml package.

python -c 'import socket; s = socket.socket(socket.AF_INET, socket.SOCK_STREAM); s.connect(("<hostname>", <port>)); print s.getsockname()[0] ; s.close() ;' 2> /dev/null
2009-10-13 16:21:15
User: angleto
Functions: python
2

on multihomed hosts, connected to several networks, could be usefull to know the source address (local ip address) used to reach the target host, this command does not require root priviledges.

The command use a TCP socket, if there is any error the command return an empty string, elsewhere return a valid ip address.

python fsrecovery.py -P 0 -f <path-to-instance>/Data.fs <path-to-instance-destination>/Data.fs.packed
curl -s "http://services.digg.com/stories?link=$NEWSURL&appkey=http://www.whatever.com&type=json" | python -m simplejson.tool | grep diggs
echo '{"json":"obj"}' | python -m simplejson.tool
2009-05-10 13:58:05
User: azeey
Functions: echo python
Tags: python json
11

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)

python -c "import socket; print '\n'.join(socket.gethostbyname_ex(socket.gethostname())[2])"
2009-04-30 16:10:43
User: haivu
Functions: python
Tags: Network python
-6

The socket.gethostname() call returns the host name of the computer. The socket.gethostbyname_ex() call returns a list of three items: The host name, the list of aliases for this host, and a list of IP addresses. Recall that Python?s array starts with index 0, the socket.gethostbyname_ex(?)[2] expression refers to the list of IP addresses. Finally, the print statement prints out the IP addresses, one per line.