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.
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:
Get the time since epoch. Useful when working with commands and logs which use this format.
Twitter stream feeds now require authentication.
This command is the FIRST in a set of five commands you'll need to get Twitter authorization for your final Twitter command.
*** IMPORTANT *** Before you start, you have to get some authorization info for your "app" from Twitter. Carefully follow the instructions below:
Go to dev.twitter.com/apps and choose "Create a new application". Fill in the form. You can pick any name for your app.
After submitting, click on "Create my access token". Keep the resulting page open, as you'll need information from it below.
If you closed the page, or want to get back to it in the future, just go to dev.twitter.com/apps
Now customize FIVE THINGS on the command line as follows:
1. Replace the string "Consumer key" by copying & pasting your custom consumer key from the Twitter apps page.
2. Replace the string "Consumer secret" by copying & pasting your consumer secret from the Twitter apps page.
3. Replace the string "Access token" by copying & pasting your access token from the Twitter apps page.
4. Replace string "Access token secret" by copying & pasting your own token secret from the Twitter apps page.
5. Replace the string 19258798 with the Twitter UserID NUMBER (this is **NOT** the normal Twitter NAME of the user you want the tweet feed from. If you don't know the UserID number, head over to www.idfromuser.com and type in the user's regular Twitter name. The site will return their Twitter UserID number to you. 19258798 is the Twitter UserID for commandlinefu, so if you don't change that, you'll receive commandlinefu tweets, uhm... on the commandline :)
Congratulations! You're done creating all the keys!
Environment variables k1, k2, k3, and k4 now hold the four Twitter keys you will need for your next step.
The variables should really have been named better, e.g. "Consumer_key", but in later commands the 256-character limit forced me to use short, unclear names here. Just remember k stands for "key".
Again, remember, you can always review your requested Twitter keys at dev.twitter.com/apps.
Our command line also creates four additional environment variables that are needed in the oauth process: "once", "ts", "hmac" and "id". "once" is a random number used only once that is part of the oauth procedure. HMAC is the actual key that will be used later for signing the base string. "ts" is a timestamp in the Posix time format. The last variable (id) is the user id number of the Twitter user you want to get feeds from. Note that id is ***NOT*** the twitter name, if you didn't know that, see www.idfromuser.com
If you want to learn more about oauth authentication, visit oauth.net and/or go to dev.twitter.com/apps, click on any of your apps and then click on "Oauth tool"
Now go look at my next command, i.e. step2, to see what happens next to these eight variables.
Gets any date since today. Other examples of recognized expressions are "2 years 4 days ago", "7 months" (in the future), "next Sunday", "yesterday", "tomorrow", etc.
Slightly shorter to type
Displays the same output as "cal", but with the current day highlighted (probably dependent on gnu grep, as I'm not sure other grep's support the "--color=auto" option). Tested and working on Ubuntu 11 and OSX Lion.
This uses curl to find out the access times of a web service
date -ud @1320198157
uses the -u switch for UTC
Another way could be
echo $(($(date -ud "00:29:36" +%s)%86400))
Another function to stick into your .bashrc
This spits out the time two minutes in the future, but already formatted for pasting into your crontab file for testing without any thought required on your part. Frequently things don't work the way you expect inside a crontab job, and you probably want to find out now that your $PATH is completely different inside of cron or other global variables aren't defined. So this will generate a date you can use for testing now, and then later you can change it to run at 5:37 am on a Sunday evening.
Neat idea! This variation works on FreeBSD.
Shorter, easier to remember version of cmd#7636
NTP is better, but there are situations where it can't be used. In those cases, you can do this to sync the local time to a server.
Requires you to have password free login to remote host ;)
Requires xclip and notify-send (If you want to put into clipboard and be notified when action is completed).
DATE=$(date +%Y-%m-%d_%H-%M-%S)-$(($(date +%N)/10000000));
HOST="ssh host of your choice";
DEST="destination folder without trailing slash";
URL="URL for file if uploaded to web enabled dir ie.
import -window root png:- | ssh $HOST "cat > $DEST/screenshot_$DATE.png";
echo $URL | xclip; notify-send -u low "Screenshot Taken" "Entire screen.\nCopied to clipboard"
No command substitution but subshell redirection
This version uses read instead of eval.
It's quite easy to capture the output of a command and assign it in a shell's variable:
But, what if we want to perform the same task with just one program invocation? Here comes the power of eval! date(1) outputs a string like "day=29; month=07; year=11" (notice the semicolons I added on purpose at date's custom output) which is a legal shell line. This like is then parsed and executed by the shell once again with the help of eval. Just setting 3 variables!
Inspired by LinuxJournal's column "Dave Taylor's Work the Shell".
I liked vaporub's suggestion, here a little simplification of the sed command.