### Commands tagged date (102) the last day the last week the last month all time sorted by date votes

• Fake system time before running any command. Show Sample Output

1
datefudge "2012-12-01 12:00" date
· 2012-06-25 19:41:05
• Choosing your year and month. You only need the gnu date command and bash. desiredDay of the week is (1..7); 1 is Monday. If you want desiredDay of week (0..6); 0 is Sunday `desiredDay=6; year=2012; month=5; n=0; while [ \$(date -d "\$year-\$((month+1))-1 - \$n day" "+%w") -ne \$desiredDay ]; do n=\$((n+1)); done; date -d "\$year-\$((month+1))-1 - \$n day" "+%x"` Show Sample Output

0
desiredDay=6; year=2012; month=5; n=0; while [ \$(date -d "\$year-\$((month+1))-1 - \$n day" "+%u") -ne \$desiredDay ]; do n=\$((n+1)); done; date -d "\$year-\$((month+1))-1 - \$n day" "+%x"
· 2012-05-17 12:02:30
• If your locale has Monday as the first day of the week, like mine in the UK, change the two \$7 into \$6 Show Sample Output

2
cal 04 2012 | awk '{ \$7 && X=\$7 } END { print X }'
· 2012-05-06 23:43:21
• This is a little trickier than finding the last Sunday, because you know the last Sunday is in the first position of the last line. The trick is to use the NF less than or equal to 7 so it picks up all the lines then grep out any empty lines. Show Sample Output

-2
cal 04 2012 | awk 'NF <= 7 { print \$7 }' | grep -v "^\$" | tail -1
· 2012-05-03 16:57:45
• The command renames all files in a certain directory. Renaming them to their date of creation using EXIF. If you're working with JPG that contains EXIF data (ie. from digital camera), then you can use following to get the creation date instead of stat. * Since not every file has exif data, we want to check that dst is valid before doing the rest of commands. * The output from exif has a space, which is a PITA for filenames. Use sed to replace with '-'. * Note that I use 'echo' before the mv to test out my scripts. When you're confident that it's doing the right thing, then you can remove the 'echo'... you don't want to end up like the guy that got all the files blown away. Credits: http://stackoverflow.com/questions/4710753/rename-files-according-to-date-created Show Sample Output

1
for i in *.jpg; do dst=\$(exif -t 0x9003 -m \$i ) && dst_esc=\$(echo \$dst | sed 's/ /-/g' ) && echo mv \$i \$dst_esc.jpg ; done
· 2012-05-02 07:23:38
• Get the time since epoch. Useful when working with commands and logs which use this format.

2
date +%s
· 2012-03-12 00:14:22
• 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.

1
date --date="90 days ago"
· 2012-03-07 07:49:27
• Convert readable date/time with `date` command Show Sample Output

1
awk '{cmd="date --date=\""\$1"\" +\"%Y/%m/%d %H:%M:%S\" "; cmd | getline convdate; print cmd";"convdate }' file.txt
· 2012-02-28 14:08:52

• 0
echo \$(date -d @\$((0x4f28b47e)))
· 2012-02-07 11:14:08

• 1
date -d '2011-12-15 05:47:09' +"epoch: %s or format: %Y/%m/%d"
· 2011-12-16 18:43:36

• 0
date -d @\$(echo \$((\$(date +%s)-\$(cat /proc/uptime|cut -d. -f1))))
· 2011-11-04 12:58:27
• date -ud @1320198157

6
TZ=UTC date -d @1320198157
· 2011-11-02 01:48:49
• uses the -u switch for UTC Another way could be `echo \$((\$(date -ud "00:29:36" +%s)%86400))`

-5
date -ud "1970/01/01 00:29:36" +%s
· 2011-11-01 17:02:46

• -5
TZ=GMT date -d "1970/01/01 00:29:36" +%s
· 2011-11-01 13:06:00
• The same with colors

0
tail -f /var/log/squid/access.loc | ccze -CA
· 2011-07-15 14:58:53

• 3
tail -f /var/log/squid/access.log | perl -p -e 's/^([0-9]*)/"[".localtime(\$1)."]"/e'
· 2011-07-06 08:55:27
• 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 Show Sample Output

2
curl -Is google.com | grep Date
· 2011-06-24 11:19:47
• 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}

1
if [ \$(date +%u) -lt 6 ];then AT="tomorrow"; else AT="next monday";fi;echo "beep" | at \${AT}
· 2011-06-01 14:15:11
• Countdown clock - Counts down from \$MIN minutes to zero. I let the date command do the maths. This version doesn't use seq. Show Sample Output

11
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
• Shows a simple clock in the console -t param removes the watch header Ctrl-c to exit Show Sample Output

5
watch -t -n1 'date "+%r %F %A"'
· 2011-02-16 03:05:48
• * 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} Show Sample Output

1
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
• even shorter (infix) version. Show Sample Output

0
buf() { cp -v \$1 \${1/\${1%%.*}/\$f-\$(date +"%Y%m%d_%H%M%S")};}
· 2010-12-15 12:16:03
• "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. Show Sample Output

0
buf() { f=\${1%%.*};e=\${1/\$f/};cp -v \$1 \$f-\$(date +"%Y%m%d_%H%M%S")\$e;}
· 2010-12-15 09:50:04
• This backup function preserve the file suffix allowing zsh suffix aliases and desktop default actions to work with the backup file too. Show Sample Output

-3
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
• 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)}; }

2
buf () { cp \$1{,\$(date +%Y%m%d_%H%M%S)}; }
· 2010-12-14 14:02:03
•  < 1 2 3 4 >  Last ›

### What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. 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.