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 date from sorted by
Terminal - Commands tagged date - 94 results
echo $(date -d @$((0x4f28b47e)))
date -d '2011-12-15 05:47:09' +"epoch: %s or format: %Y/%m/%d"
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
-5

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
tail -f /var/log/squid/access.loc | ccze -CA
tail -f /var/log/squid/access.log | perl -p -e 's/^([0-9]*)/"[".localtime($1)."]"/e'
curl -Is google.com | grep Date
2011-06-24 11:19:47
User: d3Xt3r
Functions: grep
Tags: http date time
2

This command will show the current GMT time using HTTP. This might be useful if you just want to know what's the current human-readable and accurate-enough time, without changing the system time, using a simple command that would work regardless of the availability of NTP.

Note: To get a quicker and more accurate response, replace google.com with your local NTP server.

Also can be used as an alternative to the "htpdate" program:

http://www.commandlinefu.com/commands/view/668/set-your-computers-clock-using-http-and-htp-http-time-protocol-when-ntpsntp-is-not-available

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}

MIN=10;for ((i=MIN*60;i>=0;i--));do echo -ne "\r$(date -d"0+$i sec" +%H:%M:%S)";sleep 1;done
2011-02-20 11:56:28
User: flatcap
Functions: echo sleep
11

Countdown clock - Counts down from $MIN minutes to zero.

I let the date command do the maths.

This version doesn't use seq.

watch -t -n1 'date "+%r %F %A"'
2011-02-16 03:05:48
User: oracular
Functions: watch
Tags: date clock watch
5

Shows a simple clock in the console

-t param removes the watch header

Ctrl-c to exit

svn log -r '{YYYY-MM-DD}:{YYYY-MM-DD}' | sed -n '1p; 2,/^-/d; /USERNAME/,/^-/p' | grep -E -v '^(r[0-9]|---|$)' | sed 's/^/* /g'
2010-12-22 17:52:19
User: antic
Functions: grep sed
1

* Replace USERNAME with the desired svn username

* Replace the first YYYY-MM-DD with the date you want to get the log (this starts at the midnight event that starts this date)

* Replace the second YYYY-MM-DD with the date after you want to get the log (this will end the log scan on midnight of the previous day)

Example, if I want the log for December 10, 2010, I would put {2010-12-10}:{2010-12-11}

buf() { cp -v $1 ${1/${1%%.*}/$f-$(date +"%Y%m%d_%H%M%S")};}
buf() { f=${1%%.*};e=${1/$f/};cp -v $1 $f-$(date +"%Y%m%d_%H%M%S")$e;}
2010-12-15 09:50:04
User: unefunge
Functions: cp date
0

"infix" version in bash (4.x+)

Remove -v to make it silent.

BTW: The OP forgot to use "cat" and "nmap" ;-) I had a good laugh though.

buf () {oldname=$1; if [ "$oldname" != "" ]; then datepart=$(date +%Y-%m-%d); firstpart=`echo $oldname | cut -d "." -f 1`; newname=`echo $oldname | sed s/$firstpart/$firstpart.$datepart/`; cp -i ${oldname} ${newname}; fi }
2010-12-14 19:58:34
User: Seebi
Functions: cp cut date sed
-3

This backup function preserve the file suffix allowing zsh suffix aliases and desktop default actions to work with the backup file too.

buf () { cp $1{,$(date +%Y%m%d_%H%M%S)}; }
2010-12-14 14:02:03
User: unefunge
Functions: cp date
2

1. you don't need to prepend the year with 20 - just use Y instead of y

2. you may want to make your function a bit more secure:

buf () { cp ${1?filename not specified}{,$(date +%Y%m%d_%H%M%S)}; }

read -a A<<<".*.**..*....*** 8 9 5 10 6 0 2 11 7 4";for C in `date +"%H%M"|fold -w1`;do echo "${A:${A[C+1]}:4}";done
2010-12-02 22:04:49
User: __
Functions: echo fold read
4

Like 7171, but fixed typo, uses fewer variables, and even more cryptic!

read -a A <<<"8 9 5 10 6 0 3 11 7 4";B='.*.**..*....***';for C in $(date +"%H%M"|fold -w1);do echo "${B:${A[C]}:4}";done
for a in $(date +"%H%M"|cut -b1,2,3,4 --output-delimiter=" ");do case "$a" in 0)echo "....";;1)echo "...*";;2)echo "..*.";;3)echo "..**";;4)echo ".*..";;5)echo ".*.*";;6)echo ".**.";;7)echo ".***";;8)echo "*...";;9)echo "*..*";;esac;done
tail -f file |xargs -IX printf "$(date -u)\t%s\n" X
echo "10 i 2 o $(date +"%H%M"|cut -b 1,2,3,4 --output-delimiter=' ') f"|dc|tac|xargs printf "%04d\n"|tr "01" ".*"
2010-11-24 23:49:21
User: unefunge
Functions: echo printf tr xargs
4

displays current time in "binary clock" format

(loosely) inspired by: http://www.thinkgeek.com/homeoffice/lights/59e0/

"Decoding":

8421

.... - 1st hour digit: 0

*..* - 2nd hour digit: 9 (8+1)

.*.. - 1st minutes digit: 4

*..* - 2nd minutes digit: 9 (8+1)

Prompt-command version:

PROMPT_COMMAND='echo "10 i 2 o $(date +"%H%M"|cut -b 1,2,3,4 --output-delimiter=" ") f"|dc|tac|xargs printf "%04d\n"|tr "01" ".*"'

tail -f file | while read line; do printf "$(date -u '+%F %T%z')\t$line\n"; done
2010-11-24 05:50:12
User: derekschrock
Functions: file printf read tail
Tags: tail date
4

Should be a bit more portable since echo -e/n and date's -Ins are not.

tail -f file | while read line; do echo -n $(date -u -Ins); echo -e "\t$line"; done
2010-11-19 10:01:57
User: hfs
Functions: date echo file read tail
Tags: tail date
6

This is useful when watching a log file that does not contain timestamps itself.

If the file already has content when starting the command, the first lines will have the "wrong" timestamp when the command was started and not when the lines were originally written.

find . -type f | while read line; do NEW_TS=`date -d@$((\`stat -c '%Y' $line\` + <seconds> )) '+%Y%m%d%H%M.%S'`; touch -t $NEW_TS ${line}; done
2010-11-18 14:03:32
User: angleto
Functions: find read touch
1

Increase the modification date for the files selected with the find command.