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/
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.
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
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:
This function displays the latest comic from xkcd.com. One of the best things about xkcd is the title text when you hover over the comic, so this function also displays that after you close the comic.
To get a random xkcd comic, I also use the following:
xkcdrandom(){ wget -qO- dynamic.xkcd.com/comic/random|tee >(feh $(grep -Po '(?<=")http://imgs[^/]+/comics/[^"]+\.\w{3}'))|grep -Po '(?<=(\w{3})" title=").*(?=" alt)';}
There are 10 alternatives - vote for the best!
I wasn't sure how to display the image, so I thought I'd try xml for a different twist.
This function displays the latest comic from xkcd.com. One of the best things about xkcd is the title text when you hover over the comic, so this function also displays that after you close the comic.
To get a random xkcd comic use the following:
xkcdrandom() { wget -qO- http://dynamic.xkcd.com/comic/random | sed -n 's#^<img src="\(http://imgs.[^"]\+\)"\s\+title="\(.\+\?\)"\salt.\+$#eog "\1"\necho '"'\2'#p" | bash; }
These are just a bit shorter than the ones eigthmillion wrote, however his version didn't work as expected on my laptop for some reason (I got the title-tag first), so these build a command which is executed by bash.
If you can do better, submit your command here.
You must be signed in to comment.
How do you execute it? I got this error:
local: 1: ?>: bad variable namewhen I executed it like this:
local f=$(curl -s http://xkcd.com/);display $(echo "$f"|grep -Po '(?<=")http://imgs.xkcd.com/comics/[^"]+(png|jpg)');echo "$f"|awk '/<img src="http:\/\/imgs\.xkcd\.com\/comics\/.*?" title=.*/{gsub(/^.*title=.|".*?$/,"");print}';You execute it by pasting in the command as is on the command line (which defines a function called xkcd). Then you use it by simply writing "xkcd" on the command line.
aha! thanks! :) how do you set it so it's always available, perhaps with a keyboard shortcut
You can make it always available by pasting it in your .bashrc file.
Remember this does not work on ZSH for some reason.
Zsh doesn't allow declaring a variable local and assigning it at the same time, whereas bash does. This function can be made to work with zsh by separating the declaration and assignment like this:
xkcd(){ local f;f=$(curl -s http://xkcd.com/);display $(echo "$f"|grep -Po '(?<=")http://imgs.xkcd.com/comics/[^"]+(png|jpg)');echo "$f"|awk '/<img src="http:\/\/imgs\.xkcd\.com\/comics\/.*?" title=.*/{gsub(/^.*title=.|".*?$/,"");print}';}And the same for the random version:
xkcdrandom(){ local f;f=$(wget -q http://dynamic.xkcd.com/comic/random/ -O -);display $(echo "$f"|grep -Po '(?<=")http://imgs.xkcd.com/comics/[^"]+(png|jpg)');echo "$f"|awk '/<img src="http:\/\/imgs\.xkcd\.com\/comics\/.*?" title=.*/{gsub(/^.*title=.|".*?$/,"");print}';}Cute :]
lynx --dump --source http://www.xkcd.com | grep `lynx --dump http://www.xkcd.com | egrep '(png|jpg)' | awk ' { print $0 } '
` | grep title | cut -d = -f2,3 | cut -d '"' -f2,4 | sed -e 's/"/|/g' | awk -F"|" ' { system("display " $1);system("echo "$2); } '
Debian doesn't compile grep with the '-P' option; I've altered your command to use 'pcregrep' instead, which will work (I did have to use apt-get to install pcregrep).
xkcd(){ wget -qO- http://xkcd.com/|tee >(feh $(pcregrep -oo '(?<=")http://imgs[^/]+/comics/[^"]+\.\w{3}'))|pcregrep -o '(?<=(\w{3})" title=").*(?=" alt)';}Thanks bartoniski. I usually try to include a version that doesn't use Perl compatible regexs, but didn't for this command. Grep's about the first thing I replace when I install Debian.
A version that works in zsh and takes args for numbered comic, random comic, or leave blank for newest comic:
xkcd() { local comic="$1"; local xkcdurl; if [[ $comic == "random" ]]; then xkcdurl="http://dynamic.xkcd.com/comic/random/"; else xkcdurl="http://xkcd.com/${comic// /+}"; fi; wget -qO- ${xkcdurl}|tee >(feh $(grep -Po '(?<=")http://imgs[^/]+/comics/[^"]+\.\w{3}'))|grep -Po '(?<=(\w{3})" title=").*(?=" alt)';}