Output redirection
> >> 2>
Write a command's output to a file instead of the screen
How it works
A command normally prints its result on screen. > sends that output to a file instead: ls > files.txt. If the file does not exist it is created; if it does, its old contents are wiped.
>> appends the output to the end of the file and leaves what is there alone, which is how logs are kept. Errors go to a separate stream (stderr) and do not end up in the file with >; 2> is for them. To collect both output and errors in one file, write command > file 2>&1.
Careful. > wipes the file without warning. Mixing up > and >> is a common way to lose a log or a config.
Syntax
<command> > <file>
<command> >> <file>
<command> 2> <file>
<command> > <file> 2>&1Examples
Write the file list to files.txt (overwriting it)
ls > files.txtsame as:
ls >files.txtls— list files>— output to a file, old contents are erasedfiles.txt— file
Append the line done to the end of log.txt
echo "done" >> log.txtsame as:
echo done >> log.txt,echo 'done' >> log.txt,echo "done" >>log.txtecho "done"— print text>>— append to the end of the filelog.txt— file
Run npm test and save only the errors to errors.log
npm test 2> errors.logsame as:
npm test 2>errors.lognpm test— command2>— redirect the error stream (stderr)errors.log— file