Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash --redirect output to log file (with day date in the name)

I just want to to redirect my script output to a file, to do so I try to redirect the output of a simple command and it works for a particular syntax and not for the other. Could you please have a look and give me any advice, Thanks in advance. Rgds, O.

This one is working fine:

du -h > "/var/log/mytst.$(date +%Y-%m-%d_%H:%M).log"

and this doesn't:

du -h > /var/log/mytst."$(date +"%D--%H:%M:%S")".log 2>&1

Any idea?

like image 477
Orsius Avatar asked Aug 11 '14 11:08

Orsius


People also ask

How do I redirect all output from a bash script to a file?

For utilizing the redirection of bash, execute any script, then define the > or >> operator followed by the file path to which the output should be redirected. “>>” operator is used for utilizing the command's output to a file, including the output to the file's current contents.


1 Answers

Look at this output:

$ echo "/var/log/mytst.$(date +%Y-%m-%d_%H:%M).log"
/var/log/mytst.2014-08-11_13:54.log
$ echo /var/log/mytst."$(date +"%D--%H:%M:%S")".log
/var/log/mytst.08/11/14--13:54:00.log

The second one spects a tree hierarchy, as the /'s in the output of date +%D are treated as directory separators. Your question does not state how the second version 'does not work', so I would bet the error is No such file or directory.

like image 173
Marcos Dione Avatar answered Oct 12 '22 22:10

Marcos Dione