Convert date to specified format linux - linux

I am creating the logs and pushing to logstash. The date format of the logstash server is of the below destination form. In the Linux server, I can use the date command to convert the local date/time to log file. How to use the date command to get the below format in the Linux server. What is the format of the below command? I can set the format as TZ='CEST' date +"%field". I tried to set few format specifies but could not get the string 'th' in the date of the month. Also I don't understand the last field '221' in the date. Any help ?
destination server format: August 4th 2017, 22:30:45.221

try this code:
#!/bin/sh
DaySuffix() {
case `date +%d` in
1|21|31) echo "st";;
2|22) echo "nd";;
3|23) echo "rd";;
*) echo "th";;
esac
}
date "+%B %-d`DaySuffix` %Y, %T.%3N"
result for now: August 4th 2017, 12:04:49.750
help source: Formatting the date in unix to include suffix on day (st, nd, rd and th)

Related

Change Date Format in OSX Command

I am storing a Curl command output in a variable which comes as a date. (any previous date)
date=`curl ...`
The output is:
03-22-2021
Now I want to change this date format to below
March 22, 2021
I am running below commands:
currDate="03-22-2021"
formattedDate=`date -d "${currDate}" +%B %d, %Y`
echo $formattedDate
But I am getting this error
date: illegal time format
usage: date [-jnRu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
[-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
is it possible to use such a date format? How to do it?
I have resolved my above question as per below for OSX
currDate="03-22-2021"
formattedDate=`date -jf "%m-%d-%Y" "${currDate}" +"%B %d, %Y"`
echo $formattedDate
Output is:-
March 22, 2021
is it possible to use such a date format?
No.
How to do it?
Convert the format to something GNU date can eat. Then pass it.
I typically look at man date:
DATE STRING
The --date=STRING is a mostly free format human readable date string such as "Sun, 29 Feb 2004 16:21:42 -0800" or "2004-02-29 16:21:42" or even
"next Thursday". [...]
I recommend to use YYYY-MM-DD hh-mm-ss format and add also timezone information.
tmp=03-22-2021
date -d "${tmp:6:4}-${tmp:0:2}-${tmp:3:2} 12:00:00 UTC" "+%B %d, %Y"
Do not use backticks ` . Use $(..) instead.
Check your scripts with http://shellcheck.net
The format argument to date has to be one argument, not +%B %d, %Y three.
For explanation of ${tmp:5:4} expansion see variable expansion section in bash manual.
For anything more complicated with dates in shell, I recommend dateutils package with strptime command.
in OSX Command
On OSX install GNU date or install dateutils. The BSD date available on OSX is not capable of reading user input.

Shell script to convert a date string to specific timestamp format

I have a date time string like this 17-Mar-2020.22:22 -0300.I want to get the output as 2020-03-17 22:12 .
The timezone offset is causing an issue as without that I am able to convert
For example DATE=17-Mar-2020.22:22
date -jf "%d-%b-%Y.%H:%M" $DATE "+date \"%Y-%m-%d %H:%M\""
But this timezone offset is causing an issue.Can someone please help?
Because the input is in an unusual format, it needs to be first formatted to a format date can eat. I use the format indicated in man page "2004-02-29 16:21:42" or "Sun, 29 Feb 2004 16:21:42 -0800". GNU date allows for very flexible input format - don't trust it.
Below I used sed to match the input and reformat it.
input='17-Mar-2020.22:22 -0300'
input2="$(sed 's/\([0-9]*\)-\([^-]*\)-\([^\.]*\)\.\([0-9]*\):\([0-9]*\)/\1 \2 \3 \4:\5:00/' <<<"$input")"
# input2="17 Mar 2020 22:22:00 -0300"
date --date="$input2" +"%Y-%m-%d %H:%M"
# 2020-03-18 02:22
date -jf
GNU date differs a lot from BSD date. They are completely different. BSD date supports the -j and -f options, GNU date does not.
There are dateutils and they include strptime utility that allows to do what you wanted to do with date:
strptime -e -f "%Y-%m-%d %H:%M\n" -i "%d-%b-%Y.%H:%M %Z" "17-Mar-2020.22:22 -0300"

How to make a script to rename files

I have an actioncam that saves my video in a folder into the SD Card.
Using linux, here is the path:
/media/mattiapdo/EOS_DIGITAL/_REC/100MEDIA
Files are saved in the REC_0001.AVI format
I would write a script that renames each file using the writing date.
Furthermore I notice that for some strange reason, the date and the hour are different from the effective: for example, 12/07/2017 10:30 is written as 09/02/2011 07:55
As the camera is very old and minimal, I can't reset the correct date and the correct hour so I would prefer to manipulate them in aftermath.
The goal would be to rename REC_0001.AVI in 2017_07_12__10_30.AVI
Does anyone have any ideas?
You can use the date command to print the elapsed seconds since, Unix lingo, the Epoch, aka 1970-01-01 UTC. Assuming that the camera date is in Anglo format, and that by default date likes the Anglo format, you have to swap the month and day in your date
$ date --date='09/02/2011 07:55' +%s
1314942900
$ date --date='07/12/2017 10:30' +%s
1499848200
$
so that you can compute a Delta between the real date and the camera idea of time
$ Delta=$(($(date --date='07/12/2017 10:30' +%s)-$(date --date='02/09/2011 07:55' +%s)))
$ echo $Delta
184905300
$
You haven't (yet?) told us how you fetch the date from the camera, but let's
say that
$ camera=$(fetch_date $current_file_name)
and assuming that $camera is in a format that date likes,
$ fromEpoch=$(($(date --date="$camera" +%s)+$Delta))
the last step is to get back the date in a format that you like , I suggest
the ISO 8601 format, so that your files are correctly sorted by ls
$ corrected_date=$(date --date="#$fromEpoch" +%Y-%m-%dT%H:%M)
$ cp $current_file_name other_directory/$corrected_date.AVI
The boring details about the date command, that is indeed VERY flexible and useful, are available using
$ man date
I hope that you can write your script with the info that I gave you, thank you for the question.
Addendum
Caveat emptor: totally untested
$ cat script
Delta=$(($(date --date='07/12/2017 10:30' +%s)-$(date --date='02/09/2011 07:55' +%s)))
mkdir -p ATTIC
mv *AVI ATTIC
for file in ./ATTIC/*.AVI ; do
########## fetch_date command is a placeholder for the real command
cam_date=$(fetch_date "$file")
cam_fromEpoch=$(date --date="$cam_date" +%s)
correct_fromEpoch=$(($cam_fromEpoch+$Delta))
ISO_8601=$(date --date="#$correct_fromEpoch" +%Y-%m-%dT%H:%M)
cp $file $ISO_8601.AVI
done
# cleanup, e.g. list current directory and ATTIC and ask if ATTIC is to be removed
$

Send previous day file to server in unix

I'm devloping a shell script to scp a.txt to different servers(box1 and box2) and script is running in boxmain server. Below are the requirements,
my script will connect to db2 database and generate a.txt file in boxmain
a.txt will be scp'ed to box1 once the file is generated
The file generated in boxmain(a.txt) will be scp'ed to box2 on the next day, i.e. It will be an SCP of the previous day's boxmain file
Note : box1,box2,boxmain are servers
i tried the below, able to finish first 2 tasks but stuck in 3rd. Please suggest how to achieve the third point. Thanks in advance.
db2 -tvf query.sql #creates a.txt
scp a.txt user#box1:/root/a.txt
now=$(date +"%m/%d/%Y")
cp a.txt a_$now.txt
my os version is AIX test 1 6
There is a slight problem with your question definition: using '/' in the name of your source filename will make it interpreted as not just a filename but a path containing directories as well because '/' is the directory separator. It might be a good idea to use now=$(date +"%m-%d-%Y") instead of now=$(date +"%m/%d/%Y").
But to answer your actual problem which I think boils down to this: how to get date(1) to output a date from yesterday on AIX?
The answer was found from The UNIX and Linux Forums: just set the environment variable describing your timezone to have +24 in it and you'll get yesterdays date from output of date.
For example:
user#foo ~]# date
Mon Nov 4 09:40:34 EET 2013
user#foor ~]# TZ=EST+24 date
Sun Nov 3 07:40:36 EST 2013
Applying this to your problem, just set an appropriate value for TZ when you run now=$(date +"%m/%d/%Y") ie. use now=$(TZ=ZONE+24 date +"%m-%d-%Y") (note the corrections on the path separator and replace ZONE with your own timezone).

BASH date command: can't re-combine date and time

Linux v2.4/Bash v3.2/GNU utils/date command version 5.0
I'm struggling with the date command. In a BASH application, the user can set date and time separately, resulting in separate variables for date and time. Further on, these variables are re-combined but this appears not be palatable for the date command: I get a different date back. Time is the same, however. Testing code:
#!/bin/bash
dnow1="$(date)"
echo "1 $dnow1" # --> Sat Sep 14 16:31:48 EDT 2013
#split date and time
dldate="$(date -d "$dnow1" +"%d-%m-%Y")"
echo "2 $dldate" # --> 14-09-2013
dltime="$(date -d "$dnow1" +"%H:%M:%S")"
echo "3 $dltime" # --> 16:31:48
#try to re-combine date and time
string="${dldate} ${dltime}"
echo "4 $string" # --> 14-09-2013 16:31:48
dnow2="$(date -d "$string")"
echo "5 $dnow2" # --> Thu Mar 5 16:31:48 EST 2020
I must be missing something here. Can anyone enlighten me? Thanks!
Note:
I'm working an original XBOX that has few/low resources so there's no room for other solutions like Python. I'm a 'bashist' anyway so it must be BASH!
Edit: corrected time format. Thanks Mat.
As to "$(....)" I have made it a habit to double quote wherever possible.
When getting your date use this format instead:
#split date and time
dldate="$(date -d "$dnow1" +"%Y-%m-%d")"
From GNU date manual
The output of the date command is not always acceptable as a date string, not only because of the language problem, but also because there is no standard meaning for time zone items like ‘IST’. When using date to generate a date string intended to be parsed later, specify a date format that is independent of language and that does not use time zone items other than ‘UTC’ and ‘Z’.
First of all using -d won't work in 14-09-2013 fashion, you can easily set date and time with one command and put it into variable. eg, just try this below on shell and then you can put into shell script.
date --date="Feb 2 2014 13:12:10"
Sun Feb 2 13:12:10 PST 2014

Resources