Redirect stdout / stderr output to log file with timestamp
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I'm trying to redirect the stdout and stderr to a log file. The filename should be created dynamically with current timestamp.
I can create the filename with the following command:
$ date +%Y-%m-%d_%H-%M-%S.txt
2018-04-10_16-55-55.txt
So I want to do something like this:
mycommand &> (date +%Y-%m-%d_%H-%M-%S.txt)
But this doesn't work (-bash: syntax error near unexpected token
('`)
io-redirection timestamps
add a comment |Â
up vote
0
down vote
favorite
I'm trying to redirect the stdout and stderr to a log file. The filename should be created dynamically with current timestamp.
I can create the filename with the following command:
$ date +%Y-%m-%d_%H-%M-%S.txt
2018-04-10_16-55-55.txt
So I want to do something like this:
mycommand &> (date +%Y-%m-%d_%H-%M-%S.txt)
But this doesn't work (-bash: syntax error near unexpected token
('`)
io-redirection timestamps
Similar (no dupe): unix.stackexchange.com/questions/436800/â¦
â Kusalananda
Apr 10 at 17:03
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying to redirect the stdout and stderr to a log file. The filename should be created dynamically with current timestamp.
I can create the filename with the following command:
$ date +%Y-%m-%d_%H-%M-%S.txt
2018-04-10_16-55-55.txt
So I want to do something like this:
mycommand &> (date +%Y-%m-%d_%H-%M-%S.txt)
But this doesn't work (-bash: syntax error near unexpected token
('`)
io-redirection timestamps
I'm trying to redirect the stdout and stderr to a log file. The filename should be created dynamically with current timestamp.
I can create the filename with the following command:
$ date +%Y-%m-%d_%H-%M-%S.txt
2018-04-10_16-55-55.txt
So I want to do something like this:
mycommand &> (date +%Y-%m-%d_%H-%M-%S.txt)
But this doesn't work (-bash: syntax error near unexpected token
('`)
io-redirection timestamps
asked Apr 10 at 14:01
Caner
1033
1033
Similar (no dupe): unix.stackexchange.com/questions/436800/â¦
â Kusalananda
Apr 10 at 17:03
add a comment |Â
Similar (no dupe): unix.stackexchange.com/questions/436800/â¦
â Kusalananda
Apr 10 at 17:03
Similar (no dupe): unix.stackexchange.com/questions/436800/â¦
â Kusalananda
Apr 10 at 17:03
Similar (no dupe): unix.stackexchange.com/questions/436800/â¦
â Kusalananda
Apr 10 at 17:03
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
Yes, you will need to use a command substitution:
mycommand &> "$(date +%Y-%m-%d_%H-%M-%S.txt)"
Which is bash
-speak for
mycommand >"$(date +%Y-%m-%d_%H-%M-%S.txt)" 2>&1
Which is the same as
mycommand >"$(date +%F_%H-%M-%S.txt)" 2>&1
(%F
is the same as %Y-%m-%d
)
A command substitution, $(...)
, will be replaced by the output of the command inside.
What you used was a sub-shell, (...)
. A sub-shell can't accept redirections like that.
add a comment |Â
up vote
1
down vote
Try this:
today=`date +%Y-%m-%d_%H-%M-%S`; mycommand > $today.txt 2>&1
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Yes, you will need to use a command substitution:
mycommand &> "$(date +%Y-%m-%d_%H-%M-%S.txt)"
Which is bash
-speak for
mycommand >"$(date +%Y-%m-%d_%H-%M-%S.txt)" 2>&1
Which is the same as
mycommand >"$(date +%F_%H-%M-%S.txt)" 2>&1
(%F
is the same as %Y-%m-%d
)
A command substitution, $(...)
, will be replaced by the output of the command inside.
What you used was a sub-shell, (...)
. A sub-shell can't accept redirections like that.
add a comment |Â
up vote
2
down vote
accepted
Yes, you will need to use a command substitution:
mycommand &> "$(date +%Y-%m-%d_%H-%M-%S.txt)"
Which is bash
-speak for
mycommand >"$(date +%Y-%m-%d_%H-%M-%S.txt)" 2>&1
Which is the same as
mycommand >"$(date +%F_%H-%M-%S.txt)" 2>&1
(%F
is the same as %Y-%m-%d
)
A command substitution, $(...)
, will be replaced by the output of the command inside.
What you used was a sub-shell, (...)
. A sub-shell can't accept redirections like that.
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Yes, you will need to use a command substitution:
mycommand &> "$(date +%Y-%m-%d_%H-%M-%S.txt)"
Which is bash
-speak for
mycommand >"$(date +%Y-%m-%d_%H-%M-%S.txt)" 2>&1
Which is the same as
mycommand >"$(date +%F_%H-%M-%S.txt)" 2>&1
(%F
is the same as %Y-%m-%d
)
A command substitution, $(...)
, will be replaced by the output of the command inside.
What you used was a sub-shell, (...)
. A sub-shell can't accept redirections like that.
Yes, you will need to use a command substitution:
mycommand &> "$(date +%Y-%m-%d_%H-%M-%S.txt)"
Which is bash
-speak for
mycommand >"$(date +%Y-%m-%d_%H-%M-%S.txt)" 2>&1
Which is the same as
mycommand >"$(date +%F_%H-%M-%S.txt)" 2>&1
(%F
is the same as %Y-%m-%d
)
A command substitution, $(...)
, will be replaced by the output of the command inside.
What you used was a sub-shell, (...)
. A sub-shell can't accept redirections like that.
edited Apr 10 at 14:36
answered Apr 10 at 14:20
Kusalananda
102k13200317
102k13200317
add a comment |Â
add a comment |Â
up vote
1
down vote
Try this:
today=`date +%Y-%m-%d_%H-%M-%S`; mycommand > $today.txt 2>&1
add a comment |Â
up vote
1
down vote
Try this:
today=`date +%Y-%m-%d_%H-%M-%S`; mycommand > $today.txt 2>&1
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Try this:
today=`date +%Y-%m-%d_%H-%M-%S`; mycommand > $today.txt 2>&1
Try this:
today=`date +%Y-%m-%d_%H-%M-%S`; mycommand > $today.txt 2>&1
answered Apr 10 at 14:20
L.Ray
1967
1967
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f436784%2fredirect-stdout-stderr-output-to-log-file-with-timestamp%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Similar (no dupe): unix.stackexchange.com/questions/436800/â¦
â Kusalananda
Apr 10 at 17:03