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 by root from sorted by
Terminal - Commands by root - 53 results
lynx --dump http://www.commandlinefu.com/commands/random/plaintext | grep .
2010-12-04 20:53:10
User: root
Functions: grep
2

Can be integrated into your .bashrc if you like. You'll probably want to grep out my name.

cssh 192.168.125.{1..200}
2010-07-11 11:25:59
User: root
2

This will open an awful lot of little windows, but is quite useful if you want to quickly patch something on a cluster of servers.

/etc/init.d/cron restart && tail -100 /var/log/syslog
2010-05-05 09:56:30
User: root
Functions: tail
-7

Debian-specific but very useful as cron files are prone to very subtle gotchas

gmetric -n $METRIC_NAME -v foo -t string -d 10
2009-05-29 14:21:24
User: root
Tags: ganglia
-1

The -d flag sets the lifetime of a metric and defaults to 0 hence why old metrics continue to be graphed in the dashboard. Submitting a dummy value and short lifetime ensures that the metric is removed from the dashboard.

grep -PL "\t" -r . | grep -v ".svn" | xargs sed -i 's/\t/ /g'
2009-05-28 08:52:14
User: root
Functions: grep sed xargs
3

Note that this assumes the application is an SVN checkout and so we have to throw away all the .svn files before making the substitution.

svn log | grep "bodge\|fudge\|hack\|dirty"
2009-05-15 09:55:44
User: root
Functions: grep
-4

A good way to understand what you've let yourself in for. Potential project metric could be the count:

svn log | grep -c "bodge\|fudge\|hack\|dirty"
./my-really-long-job.sh && notify-send "Job finished"
2009-05-07 15:50:27
User: root
Tags: notify-send
24

You will need libnotify-bin for this to work:

sudo aptitude install libnotify-bin
mysql --database=dbname -B -N -e "SHOW TABLES" | awk '{print "ALTER TABLE", $1, "CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;"}' | mysql --database=dbname &
2009-03-21 18:45:15
User: root
Functions: awk
Tags: mysql
18

This loops through all tables and changes their collations to UTF8. You should backup beforehand though in case some data is lost in the process.

watch -n 1 uptime\;myqladmin --user=<user> --password=<password> --verbose processlist
2009-03-21 18:29:28
User: root
Functions: watch
Tags: mysql
-6

Useful for monitoring both MySQL and the server load at the same time.

tail -1000 /some/file | vim -
2009-02-25 11:43:27
User: root
Functions: tail vim
17

The hyphen tells vim to open from STDOUT - saves having to create temporary files.

ssh user@host 'mysqldump dbname | gzip' > /path/to/backups/db-backup-`date +%Y-%m-%d`.sql.gz
2009-02-18 21:12:53
User: root
Functions: ssh
Tags: mysql
17

I have this on a daily cronjob to backup the commandlinefu.com database from NearlyFreeSpeech.net (awesome hosts by the way) to my local drive. Note that (on my Ubuntu system at least) you need to escape the % signs on the crontab.

identify -format "%wx%h" /path/to/image.jpg
watch -n 1 mysqladmin --user=<user> --password=<password> processlist
2009-02-16 11:21:16
User: root
Functions: watch
Tags: mysql
45

Watch is a very useful command for periodically running another command - in this using mysqladmin to display the processlist. This is useful for monitoring which queries are causing your server to clog up.

More info here: http://codeinthehole.com/archives/2-Monitoring-MySQL-processes.html

xrandr --output LVDS --auto --output VGA --auto --right-of LVDS
2009-02-11 13:20:17
User: root
7

You'll need to make sure your xorg.conf permits a virtual screen size this big. If it doesn't then xrandr should return a suitable error message that tells you the required size.

diff <(head -500 product-feed.xml) <(head -500 product-feed.xml.old)
2009-02-11 09:24:38
User: root
Functions: diff head
3

Useful for massive files where doing a full diff would take too long. This just runs diff on the first 500 lines of each. The use of subshells to feed STDIN is quite a useful construct.

cat file.php | perl -p -e 's/(\$|->)(str|arr|obj|int|flt|boo|bool|mix|res)([A-Z])/$1\L$3/g'
2009-02-10 14:37:12
User: root
Functions: cat perl
0

This removes the type prefix used in Hungarian notation (v. bad) for PHP variables. Eg. variables of the form $intDays, $fltPrice, $arrItems, $objLogger convert to $days, $price, $Items, $logger.

find . -name "*.java" -exec gnome-terminal \-x vim {} \;
2009-02-10 09:38:04
User: root
Functions: find vim
-6

Not that useful really, more novel. Can open up an awful lot of terminal windows.

guid(){ lynx -nonumbers -dump http://www.famkruithof.net/uuid/uuidgen | grep "\w\{8\}-" | tr -d ' '; }
echo "rm -rf /unwanted-but-large/folder" | batch
2009-02-04 19:07:52
User: root
Functions: echo
38

Good for one off jobs that you want to run at a quiet time. The default threshold is a load average of 0.8 but this can be set using atrun.

realpath examplefile.txt
2009-02-04 15:41:44
User: root
4

Useful in scripts when the file is passed in as an argument. Eg.

filepath=$(realpath $1)
ssh user@host cat /path/to/remotefile | diff /path/to/localfile -
2009-02-04 11:33:19
User: root
Functions: cat diff ssh
148

Useful for checking if there are differences between local and remote files.

zcat /usr/share/man/man1/grep.1.gz | grep "color"
2009-02-04 09:38:45
User: root
Functions: grep zcat
-7

This decompresses the file and sends the output to STDOUT so it can be grepped. A good one to put in loops for searching directories of gzipped files, such as man pages.

bind -p | grep -F "\C"
2009-02-03 16:22:14
User: root
Functions: grep
5

Useful for getting to know the available keyboard shortcuts.

(> errors.log) && tail -f !^
2009-02-03 16:08:19
User: root
Functions: tail
2

This is useful for keeping an eye on an error log while developing. The !^ pulls the first arg from the previous command (which needs to be run in a sub-shell for this shortcut to work).

cat /etc/issue