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 using date from sorted by
Terminal - Commands using date - 147 results
date --date="90 days ago"
2012-03-07 07:49:27
User: dan
Functions: date
Tags: date
1

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.

echo $(date -d @$((0x4f28b47e)))
date -d '2011-12-15 05:47:09' +"epoch: %s or format: %Y/%m/%d"
cal | grep -C7 --color=auto $(date +%d)
2011-11-29 11:35:41
User: cjp64
Functions: cal date grep
0

Slightly shorter to type

cal |grep -A7 -B7 --color=auto $(date +%d)
2011-11-26 22:13:12
User: 4fthawaiian
Functions: cal date grep
6

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.

grep -R Subject /var/spool/exim/input/ | sed s/^.*Subject:\ // | sort | uniq -c | sort -n > ~/email_sort.$(date +%m.%d.%y).txt
for i in {1..40};do echo -n $i. $(date +%H:%M:%S):\ ; (time curl 'http://ya.ru/' &> /dev/null) 2>&1|grep real;sleep 1;done
2011-11-11 10:40:38
User: AntonyC
Functions: date echo grep sleep time
Tags: curl
-1

This uses curl to find out the access times of a web service

date -d @$(echo $(($(date +%s)-$(cat /proc/uptime|cut -d. -f1))))
TZ=UTC date -d @1320198157
date -ud "1970/01/01 00:29:36" +%s
2011-11-01 17:02:46
User: frans
Functions: date
Tags: echo date time
-4

uses the -u switch for UTC

Another way could be

echo $(($(date -ud "00:29:36" +%s)%86400))
TZ=GMT date -d "1970/01/01 00:29:36" +%s
date -d "$(uptime | awk '{gsub(/,/,"",$3);gsub(/:/," hours ",$3); print "- " $3 " minutes"}')"
date -u +%W$(uname)|sha256sum|sed 's/\W//g'
crontest () { date '-d +2 minutes' +'%M %k %d %m *'; }
2011-09-16 00:47:24
Functions: date
6

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.

fn=$(find . -type f -printf "%T@\t%p\n"|sort -n|tail -1|cut -f2); echo $(date -r "$fn") "$fn"
TZ=$TZ-72 date +%d.%m.%Y
date `ssh user@server date "+%y%m%d%H%M.%S"`
2011-08-30 22:32:14
User: wu
Functions: date
Tags: ssh ,NTP ,Date
0

Neat idea! This variation works on FreeBSD.

date --set="$(ssh user@server date)"
2011-08-30 20:03:06
User: splante
Functions: date
Tags: ssh ,NTP ,Date
32

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.

DATE=$(date +%Y-%m-%d_%H-%M-%S)-$(($(date +%N)/10000000)); HOST=ssh_host; DEST=file_dest; URL=url/screenshot_$DATE.png; import -window root png:- | ssh $HOST "cat > $DEST/screenshot_$DATE.png"; echo $URL | xclip; notify-send -u low "Title" "Message"
2011-08-13 00:40:36
User: swordfischer
Functions: date echo ssh
4

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"

read day month year < <(date +'%d %m %y')
2011-07-30 06:06:29
User: frans
Functions: date read
Tags: bash read
10

No command substitution but subshell redirection

read day month year <<< $(date +'%d %m %y')
eval $(date +"day=%d; month=%m; year=%y")
2011-07-29 12:47:26
User: xakon
Functions: date eval
Tags: bash eval
4

It's quite easy to capture the output of a command and assign it in a shell's variable:

day=$(date +%d) month=$(date +%m)

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".

MAC=$((date +'%Y%m%d%H%M%S%N'; cat /proc/interrupts) | md5sum | sed -r 's/(..)/\1:/g' | cut -d: -f 1-6)
2011-07-14 13:11:50
User: jazznjam
Functions: cat cut date md5sum sed
-1

I liked vaporub's suggestion, here a little simplification of the sed command.

if [ $(date +%u) -lt 6 ];then AT="tomorrow"; else AT="next monday";fi;echo "beep" | at ${AT}
2011-06-01 14:15:11
User: eduo
Functions: at date echo
1

Line can be modified as needed. This considers weekdays to be Mon-Fri. If run any working day it'll provide a parameters for the next working day for "at".

"beep" provided as a sample command.

This can be modified easily to include wait time. If you need something to run "D" days after today:

# D=4;if [ $(date +%u --date="${D} days") -lt 5 ];then AT="+${D} days";else AT="next monday";fi; echo "beep" | at noon ${AT}

echo $(( $( date +%s ) - $( stat -c %Y * | sort -nr | head -n 1 ) ))