I'm writing a bash script in FreeBSD that appends commands to a log file. Before I execute the commands that will append the log data, I want to print a line in the file which shows the current date above the data, like this:
---Tue Aug 20 17:26:37 EDT 2019---
I know that I can use the date command to output the timestamp, but I'm not sure how to include the "---" before and after the timestamp to add to the file. What's the simplest way to do this?
You can pass a format string to date:
date '+---%a %b %e %H:%M:%S %Z %Y---'
Related
I understand how to reformat a date using the date command, and I am fine with that. However, I have a wrinkle in that I am struggling with - the date I want to reformat is the output of another command, so I am storing it in the variable. I am struggling with the syntax of how to specify that I want to take the output of one command, and run it through date -d, and store it in another variable. Here is what I tried:
expdate=`get_expire_date.sh`
echo $expdate
Mon 23 Mar 2022 05:05:05 PM UTC
expdval=`date -d'($expdate)'`
echo $expdval
I get today's date, not the converted expire date from the script output. If I leave the parenthesis out, of course, it treats $expdate as the literal text to translate and gives an error, whereas if I leave the single quote marks off, it uses the spaces in the date string as a delimiter and only grabs the first token.
What am I doing wrong?
First, parameter expansion doesn't occur inside single quotes. You would need to change the single quotes
expdval=`date -d'($expdate)'`
to double quotes
expdval=`date -d"($expdate)"`
Second, the parentheses create an invalid input, which results (for reasons I don't really understand) in an output of midnight of the current day. (You'll get the same result with the trivial invalid date date -d "".)
Drop the parentheses, and you'll get the same date back (because the input format matches the default output format).
$ date -d "$expdate"
Wed Mar 23 13:05:05 EDT 2022
To actually manipulate it, you'll need an explicit output format:
$ date -d "$expdate" +%Y-%m-%d
2022-03-23
or some form of date "arithmetic":
$ date -d "$expdate + 2 days"
Fri Mar 25 13:05:05 EDT 2022
I found I had to use double-quotes instead, like this (and sorry for the old way of doing things, updating to new shell syntax):
expdval=$(date -d"$(get_expire_date.sh)")
Let's say the file name is : filetest.txt
and is present in the directory: inventory/data
How can I get the date at which this file was last modified/created?
Example : let's say if the file is created on 3rd June 2007 then it should return only value 3.
That means, how to extract the date only from that timestamp of file?
You can use the date command to extract the modification time of a file by passing it as reference.
date -r filename prints the last modified timestamp of the file.
Sat Jul 3 16:03:53 IST 2021
date +%d -r filename print just the day you asked for, but with a leading zero.
03
date +%-d -r filename prints the day, without a leading zero.
3
I'm trying to develop a bash script which filters csv files (generated every hour) for a day before and merge them into a single CSV file. This script seems to do the job for me, except that I'm trying to filter files based on their filenames.
There would be 24 files for each day in the directory, and I need to filter out these files based on their name format:
foofoo_2017052101502.csv
foofoo_2017052104502.csv
foofoo_2017052104503.csv
foofoo_2017052204501.csv
foofoo_2017052204504.csv
Here, I need to filter out for May 21, 2017. So my output CSV files must have the first three .csv files.
What should I add in the script for this filter?
The following script will calculate the previous day yyyymmdd and use that value in the grep to automatically filter out all the file names generated the previous day.
For MacOS
dt=`date -j -v-1d +%Y%m%d`
echo $dt
OutputFiles=`ls | grep foofoo_${dt}`
For Linux
dt=`date -d "yesterday" +%Y%m%d`
echo $dt
OutputFiles=`ls | grep foofoo_${dt}`
These commands when added to the script mentioned will filter the file names for the previous day based upon the current time stamp.
You can let bash do the filtering for you using globbing, for example to list only files with date May 21, 2017 you could use:
for filename in foofoo_20170521*.csv; do...
If you want to be able to call your script with an argument specifying the date to have more flexibility, you can use:
for filename in "foofoo_${1}*.csv"; do...
And then call your script with the date that you want to filter as an argument:
./your_script 20170521
And as #David C. Rankin mention in the comments, a very practical way to do it would be to concatenate all the files from the date you want into one csv that you would then use in your script:
cat foofoo_20170521*.csv > combined_20170521.csv
My File contains Date as Thu 03/26/2015
I need to get output as 03/26/2015 I don't need day part in my date
Use command
date /t
Output will gate date only
I have date list.
Jan21
Jan23
I want Jan21 show as 01-21-2014 and Feb5 show as 02-05-2014. How do i do this in shell script?
You can try this,
date -d 'Jan21' '+%d-%m-%Y'