I'm trying to create a log file of a bash script that is run with the data. I currently have this:
bash script.sh > /var/log/%Y-%m-%d_%H:%M.log
The problem is the log file actual writes %Y-%m-%d_%H:%M and not the date. Is there way to get the date to actually write out the date by only running it in the console?
a date, or at least the year, when the contents of the file were created, in the YYYY-MM-DD format (four digit year, two digit month, two digit day, with a dash in between.)
Sample shell script to display the current date and time #!/bin/bash now="$(date)" printf "Current date and time %s\n" "$now" now="$(date +'%d/%m/%Y')" printf "Current date in dd/mm/yyyy format %s\n" "$now" echo "Starting backup at $now, please wait..." # command to backup scripts goes here # ...
To get the date in bash shell and add a date, you can use the GNU date command as date -d 'Feb 12 +1 day' +%F which will output 2020-02-13 .
Pass that string to date
:
bash script.sh > "/var/log/$(date +%Y-%m-%d_%H:%M).log"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With