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 ruby from sorted by
Terminal - Commands tagged ruby - 19 results
ruby -ruri -e 'u=URI(ARGV[0]).host.split("."); puts u[(u[-2] =~ /^com?$/ ? -3 : -2)..-1].join(".")' http://test.example.com
2014-05-25 18:43:44
User: wejn
Tags: ruby
1

Extracts 2nd-level domain part (or 3rd level, for co.* or com.*) from the URI's hostname. See sample output.

ruby -rrubygems/commands/mirror_command -S gem mirror
2014-04-22 18:03:01
User: rubiojr
Tags: ruby
2

https://github.com/rubygems/rubygems-mirror/issues/20

ls -1 | xargs ruby -e'puts ARGV.shuffle'
grep -l --include '*.rb' --include '*.rake' '^[^#]*[^a-zA-Z0-9[:punct:][:space:]]' -R . | xargs -L1 awk '!/encoding/ && NR < 2 { print FILENAME }'
rvm --default use ruby-1.9.3
ruby -rwebrick -e'WEBrick::HTTPServer.new(:Port => 3000, :DocumentRoot => Dir.pwd).start'
2013-08-20 10:19:27
User: boylah
1

Here's how to serve a directory in one line of Ruby.

Handy for sharing files at a conference, for example.

ruby -ryaml -rjson -e 'puts YAML.dump(JSON.parse(STDIN.read))' < file.json > file.yaml
echo 'Dir.foreach("/usr/local/Cellar/cowsay/3.03/share/cows") {|cow| puts cow; system "fortune | cowsay -f /usr/local/Cellar/cowsay/3.03/share/cows/#{cow}" }' | ruby
2013-04-15 12:27:38
User: orkoden
Functions: echo
-1

Shows a list of all installed cows saying a fortune. Also lists the cows names. Pic your favorite cow!

Needs cowsay, fortune and ruby installed. The path only applies to OS X with cowsay installed using homebrew. On Linux it might be /usr/share/cowsay/cows/ or similar. Uses ruby just because.

echo "$: << '.'" >> $IRBRC
cap -s branch=my_branch deploy
2012-05-23 06:54:21
User: carlzulauf
0

You can also deploy a specific tag:

cap -s tag=my_tag deploy
ash prod<tab>
2012-05-12 19:51:02
User: c3w
-1

http://github.com/c3w/ash

. a Ruby SSH helper script

. reads a JSON config file to read host, FQDN, user, port, tunnel options

. changes OSX Terminal profiles based on host 'type'

USAGE:

put 'ash' ruby script in your PATH

modify and copy ashrc-dist to ~/.ashrc

configure OSX Terminal profiles, such as "webserver", "development", etc

run "ash myhostname" and away you go!

v.2 will re-attach to a 'screen' named in your ~/.ashrc

tail -f production.log | perl -ne 'if (/^Completed.in.(\d+)/){$d = int($1/1000);print "\n";$f{$d}++;for $t (sort(keys(%f))){print $t."s: ".$f{$t}."\n"}}'
2012-02-23 14:37:33
User: theist
Functions: perl tail
0

Commandline perl filter for, using a production.log from a rails app, display on realtime the count of requests grouped by "seconds to complete" (gross round, but fair enough for an oneliner) :)

ruby -ne 'puts $_.split.collect(&:capitalize).join(" ")' <<< "pleAse cOuld YOu capiTalizE Me"
2011-09-15 01:04:43
User: Quiryaki
1

"-n" loops around ; "-e" executes the given quoted string ; "$_" is the current line ; "split" creates an array on white space; each item of the array is "collected" to be then "capitalized" ; the array is "joined" back into a string.

echo 'fOo BaR' | ruby -e "p STDIN.gets.split.map(&:capitalize).join(' ')"
require 'time';backup_dir = '/path';Dir.glob(backup_dir+"/*.sql").each{ |f| filetime = Time.parse(`mdls -name kMDItemContentCreationDate -raw #{f}`);monthago = Time.now - (30 * 60 * 60 * 24);`rm #{f}` if filetime < monthago }
ruby -e 'puts (1..6).map{"%0.2X"%rand(256)}.join(":")'
2010-12-08 10:01:31
User: eightmillion
3

Ruby version.

Also, a perl version:

perl -e 'printf("%.2x.",rand(255))for(1..5);printf("%.2x\n",rand(255))'
changing_assets = `s3cmd sync --dry-run -P -M --exclude=*.php --delete-removed #{preprod_release_dir}/web/ #{s3_bucket} | grep -E 'delete:|upload:' | awk '{print $2}' | sed s_#{preprod_release_dir}/web__`
2010-05-07 16:03:42
User: trivoallan
Functions: awk grep sed sync
2

Can be useful to granulary flush files in a CDN after they've been changed in the S3 bucket.

ruby -rdate -e 'p DateTime.now.cweek'
ruby -e 'require "date"; puts DateTime.now.cweek'