How to convert any date to YYYYMMDDHHMMSS using unix shell script?
It is not specific date it may any date. how to convert it?
example date format is: 20110101050603
Thanks
Puspa
DATE = "15 Jun 2015 10:10:10"; date -d"$DATE" +%Y%m%d%H%M%S
Output :-
20150615101010
More on Dates
Note this only works on GNU date.
I have read that:
Solaris version of date, which is unable to support -d can be resolve with replacing sunfreeware.com version of date.
Update:-
Use ls command to find all files and use grep command for all files.
ls | grep "[0-9]{14}"
grep command also accepts regular expression.
But it won't validate your file accurate. For example, if a file name is 20151835101010, then it will validate this file. But actually month and date both are wrong.
So my suggestion is to write a shell script to identify valid file name and find oldest of them by processing one by one.
Related
In order to do simple checks (e.g. date) without using files I tried the following (without sucess):
Store date in enviroment variable:
fs0:\> date >v date_var
Shell: Incorrect redirection syntax - '>v'
as an alternative I tried to pipe the result of date:
fs0:\> date | check_date.nsh
date: Too many arguments
Do I need certain lib or command level to run the above character sequences?
I´m using a uefi shell with:
EFI Shell version 2.40 [5.10]
Current running mode 1.1.2
...
fs0:\> ver
EFI Specification Revision : 2.40
Uefi_Shell_Spec_2_2 chapter 3.4.4 let me think that both should work fine.
If I remember correctly, UEFI shell does not support process piping, nor variable piping, so you cannot chain outputs, but it do support file redirections.
try to use data > date_file.
This question already has answers here:
Get date of last saturday - BusyBox 1.1.0
(2 answers)
Closed 5 years ago.
I'm currently working on an ESXI and want to get the date X day ago
I have this command line working on other linux :
now=`date +"%Y/%m/%d"`
earlier=`date -d "$now -15 days" +%d/%m/%Y`
but when i try to use it on my esxi server the line :
earlier=`date -d "$now -15 days" +%d/%m/%Y`
is not working, i get the error
date: invalid date '2018/01/30 -4 days'
So I tried to write the date in differents way like american format but still have the error.
the esxi version is 6.0.0
I have searched on internet but i didn't find anything.
Can someone know what is the problem ?
Thank you.
edit: for those having the same problem i got the solution in the comments below
It turns out this is busybox date. Busybox date is very limited in its abilities, but fortunately it supports (undocumentedly) the #seconds syntax:
date -d "#$(( $(busybox date +%s) - 86400 * 7 ))" +%d/%m/%Y
This of course requires that you have a modern version of bash. If this command doesn't work, try typing "bash" and if that works try typing the command again.
If you don't have modern bash...then do you have awk or bc installed? Python? Perl? Basically the tactical goal is to get date to spit out seconds, subtract 7 days worth of seconds, and then feed the output into date again to convert format.
Old
Try this command line. It avoids the $now reference which might confuse some versions of date.
date -d "15 days ago" +%d/%m/%Y
If this doesn't work, can you tell us which version of date you are using? date --version and/or date --help should provide the necessary information.
I am using AIX server.
I have a variable containing date returned from database i.e.
$ echo $date_var
12-JAN-17
Now i want to convert this string into yyyymmdd format.
I issued the following command
date -d $date_var +%Y%m%d
But I am getting error:
date : illegal operation --d
I think AIX server does not support this functionality. What can I do instead?
Could not add comment hence posting it as answer.
Can you check -u switch. reference link
I have the problem,that the following code doesn't work.
start "" "datetime.exe" +%s -d "!timestamp!">tmp_datetime.txt
in cmd it works well, the variable timestamp is in the right Format.
for cmd I type the following
datetime.exe +%s -d "YYYY-MM-ddTHH:mm:ss"
and back comes the date as unix timestamp.
After running my Batch file with the start command, the tmp_datetime.txt is empty
Please try to use "call" instead of "start". Start will open a new window. I think there goes the problem with.
So try this one:
call datetime.exe +%s -d "!timestamp!">tmp_datetime.txt
occasionally I run a backup of my phpbb forum files from the Shell command line:
zip -r forum_backup ~/public_html/forum/*
I'd like to add date elements to the file name, so that the zip file created is automatically formed as
forum_backup_05182013.zip
any other similar current date format would also be acceptable
now=$(date +"%m%d%Y")
zip -r forum_backup_$now ~/public_html/forum/
Without defining a variable first you can do it in one line with
zip -r "forum_backup_$(date +"%Y-%m-%d").zip" filelist
As taken from here
the following shell command, change the format as you want
FORMAT="%Y%m%d"
_DATE=$(date +"$FORMAT" )
zip -r "forum_bakcup_${_DATE}" ~/public_html/forum/*