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 - 61 results
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
4

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

python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"
python -m SimpleHTTPServer
2011-02-12 11:40:56
User: soissons
Functions: python
-2

"this command line isn't mine but i find it very useful" ^^

This one-liner starts a dedicated server hosting (web server) on port 8000 with the contents of current directory on all the interfaces (address 0.0.0.0), not just localhost. If you have "index.html" or "index.htm" files, it will serve those, otherwise it will list the contents of the currently working directory.

It works because python comes with a standard module called SimpleHTTPServer. The -m argument makes python to search for a module named SimpleHTTPServer.py in all the possible system locations (listed in sys.path and $PYTHONPATH shell variable). Once found, it executes it as a script. If you look at the source code of this module, you'll find that this module tests if it's run as a script if __name__ == '__main__', and if it is, it runs the test() method that makes it run a web server in the current directory.

python -c "print 'this is a test'.title()"
python -m http.server
2010-12-17 12:52:45
User: Alanceil
Functions: python
25

In Python version 3, the module was merged into http.server. Gentlemen, change your aliases.

python -c "from itertools import imap; from random import randint; print ':'.join(['%02x'%x for x in imap(lambda x:randint(0,255), range(6))])"
python -c 'import datetime; print(datetime.date.today().isocalendar()[1])'
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.

python -c $(echo -e 'import py_compile\npy_compile.compile("/path/to/script.py")');
python -c "print 1+1"
$ python -u script.py
2010-05-20 17:53:47
User: recursiverse
Functions: python
2

You have a python script that slowly prints output, you want to pipe the output to grep or tee, and you are impatient and want to watch the results right away. Rather than modify your script (making it slightly less efficient), use the -u option to have the output unbuffered.

utime(){ python -c "import time; print(time.strftime('%a %b %d %H:%M:%S %Y', time.localtime($1)))"; }
! python %
python -c 'print "-"*50'
2010-03-25 11:09:30
User: funky
Functions: python
9

essentially the ruby one, but perhaps has a larger installed base

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()"
python -c 'import sys,csv; c = csv.reader(sys.stdin); [sys.stdout.write("^M".join(map(repr,r))+"\n") for r in c];' <tmp/test.csv | column -s '^M' -t
2010-02-01 14:57:25
User: pykler
Functions: c++ column python
Tags: CSV
1

Will handle pretty much all types of CSV Files.

The ^M character is typed on the command line using Ctrl-V Ctrl-M and can be replaced with any character that does not appear inside the CSV.

Tips for simpler CSV files:

* If newlines are not placed within a csv cell then you can replace `map(repr, r)` with r

python -c 'print "-" * 50'
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.

gem list --local | python -c "import sys;import re;l=sys.stdin.readlines();x=['config.gem \"'+line[:-1][:line.index(' ')] + '\" , ' +line[:-1][line.index(' '):].replace('(',':version => \"').replace(')','')+'\"' for line in l];print '\n'.join(x)"
gem list --local | python -c "import sys;import re;l=sys.stdin.readlines();x=['config.gem :'+line[:-1][:line.index(' ')] + ' , ' +line[:-1][line.index(' '):].replace('(',':version => ').replace(')','') for line in l];print '\n'.join(x)"
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