if I open windows cmd and type this command:
copy D:\1.txt C:
then I see this result:
Access is denied.
0 file(s) copied.
but with this command: copy D:\1.txt C:\ > d:\output.txt output text file shows only this : 0 file(s) copied.
why "Access is denied." is not in output.txt? and how could I have complete output in text file?
CodePudding user response:
Redirecting a command's output to a file via > somefile.txt
only redirects the data written to "standard output" stream to the file, it does not include data written to "standard error" stream, which is where the "Access is denied." message is written in your case. To capture all output, you need to append 2>&1
to the end of the command, e.g.:
copy D:\1.txt C:\ > d:\output.txt 2>&1
This can be understood as an instruction to send data written to standard error (stream 2) to standard output (stream 1).
You can also redirect standard error to a different file to standard output with e.g.:
copy D:\1.txt C:\ > d:\output.txt 2> d:\errors.txt