Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write output of bash script to file with date

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?

like image 242
Devin Dixon Avatar asked Apr 25 '13 02:04

Devin Dixon


People also ask

How do you write the date on a file?

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.)

How do I print the current date in bash?

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 # ...

How do I create a date in bash?

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 .


1 Answers

Pass that string to date:

bash script.sh > "/var/log/$(date +%Y-%m-%d_%H:%M).log"
like image 138
Blender Avatar answered Oct 13 '22 23:10

Blender