Display date from twelve hours ago

HI Guys

I want to create date folder in unix base on currant date minus 12 hours.

Ex:

Currant date :07222013 and time is 1 Am


So the folder will create date :07212013

What is your OS and version?

In Linux:

gacanepa@Gabriel-PC ~ $ uname -a
Linux Gabriel-PC 3.5.0-17-generic #28-Ubuntu SMP Tue Oct 9 19:31:23 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux

Using GNU Bash:

gacanepa@Gabriel-PC ~ $ bash --version
bash --version
GNU bash, version 4.2.37(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.

This does the trick:

mkdir $(date +%m%d%Y -d "12 hours ago")

Otherwise, you can get the job done using perl (available out-of-the-box in most Unix flavors):

gacanepa@Gabriel-PC ~ $ mkdir $(perl -e 'print scalar localtime( time - 43200 ) . "\n";' | date +%m%d%Y)
#/bin/bash
#purpose of this code is to display past time, as per this system clock in IST timings
arg=$1
echo $(date +%Y%m%d%H%M%S -d "$arg hours ago")
echo

Run this code as below

-bash-4.1$ ./past_date 12
20130722225318

here , 12 is passed as runtime parameter.The script can give output for any dynamic number entered by user.

Still Not Work :

p14515f@glne2cx1> (date +%m%d%Y -d "100 hours ago" )
07232013

You are not referencing the hours in the date command options. What do you actually expect to get out of it and what are you trying to do? You are asking it to display MonthDayFullyear, so I'm lost as to where 12 hours comes in.

Robin

#/bin/bash
#purpose of this code is to display past time, as per this system clock in IST timings
arg=$1
echo $(date +%Y%m%d%H%M%S -d "$arg hours ago")
echo
-bash-4.1$

you had missed what i have highlighted

and what you tried gave me following results

-bash-4.1$ date +%m%d%Y -d "100 hours ago"
07212013