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
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
0

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.

twistd -no web
2014-02-01 22:35:50
User: nkorth
-1

Install twistd first with

sudo apt-get install python-twistd-web
ruby -ryaml -rjson -e 'puts JSON.pretty_generate(YAML.load(ARGF))' < file.yml > file.json
virtualenv --no-site-packages --distribute -p /usr/bin/python3.3 ~/.virtualenvs/pywork3
python -c "a='$(ps -u luke | grep Viber.exe)';b= a.split(' ')[1];import os;os.system('cpulimit -l 1 -p '+b)"
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).

pygmentize -l pytb myapp.log | less -SR
grep --color=always -nr 'setLevel' --include=*py | less -SRqg
curl -L -d "uid=<username>&pwd=<password>" http://www.example.com -c cookies.txt
2012-11-10 19:08:45
User: drwlrsn
Tags: curl http python
0

Generate a Netscape cookies file to use with Python's mechanize.

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.

alias colortest="python -c \"print('\n'.join([(' '.join([('\033[38;5;' + str((i + j)) + 'm' + str((i + j)).ljust(5) + '\033[0m') if i + j < 256 else '' for j in range(10)])) for i in range(0, 256, 10)]))\""
2012-10-26 07:43:06
User: Paaskehare
Functions: alias
1

Terminal Color tester using python, works with py2 and 3

colortest-python
alias pp='python -mjson.tool | pygmentize -l javascript'
2012-10-16 13:55:38
User: wires
Functions: alias
0

Uses pygmentize and python to create indented and colorized JSON output

__location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))
2012-10-11 09:32:20
User: evandrix
Tags: python
0

Sample USAGE: f = open(os.path.join(__location__, 'bundled-resource.jpg'));

import unicodedata; map(unicodedata.name, '\u2022'.decode('ascii'))
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 :)

strings -f sample.txt
for i in $(seq 1 20); do while read line; do echo "$i: $line"; done<$i.py; 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.

python3 -m http.server
python -c 'import string, random; print "".join(random.choice(string.letters+string.digits) for x in range(6))'
echo '{"json":"obj"}' | python -mjson.tool