Saving nan output of a bid to a record is adjuvant if you're trying to diagnose a problem, and it's ace easy to do.
When you tally a terminal command, it will typically people output successful nan terminal, truthful you tin publication it immediately. However, you’ll sometimes want to prevention nan output to analyse it later aliases harvester it pinch different tool.
You tin do this utilizing output redirection connected nan bid line. Find retired really to nonstop nan output of a bid to a matter record connected Windows, Mac, and Linux.
Redirect Command-Line Output to a File
There are 2 operators you tin usage to redirect nan output of a bid to a file: >> and >. It's important you understand nan quality betwixt nan 2 to debar unintended information loss.
The > awesome creates a caller record aliases overwrites it if it already exists. The >> usability besides creates a caller record if it doesn't exist, but it won’t overwrite an existing file. If nan record already exists, it will append nan matter to nan extremity of it.
To redirect nan output of a bid to a file, type nan command, past specify nan > aliases >> operator, and yet supply nan way to a record you want to redirect nan output to. For example, here’s really you tin prevention nan output from nan ls command, which lists nan contents of a directory:
ls > /path/to/fileReplace /path/to/file pinch nan afloat way to nan record you want to use. The bid will tally silently, storing nan output successful nan record you specify.
To position nan contents of nan record successful your terminal, you tin usage nan cat command. Again, switch nan /path/to/file pinch nan afloat way to nan record you want to view.
feline /path/to/fileYou should spot nan output from nan bid successful your caller file:
The > usability replaces nan contents of an existing file. If you want to prevention nan output from respective commands to a azygous file, usage nan >> usability instead. This appends to nan file, truthful you won't suffer immoderate erstwhile output you've saved.
For example, effort appending strategy accusation to nan extremity of nan record you conscionable created. Simply tally uname -a on Linux/Mac—or nan ver bid if you're connected Windows—and adhd nan >> usability on pinch nan way to nan file:
uname -a >> /path/to/fileRepeat this process arsenic galore times arsenic you request to support appending bid output to nan extremity of nan file.
Print Output to Screen and Redirect It to a File
The > and >> operators don't show nan output of a bid connected nan screen, they conscionable nonstop it to a file. If you want to nonstop nan output to a record and spot it connected nan screen, usage nan tee command.
To usage nan tee command, nonstop nan output from different bid to it utilizing nan tube operator, a vertical barroom (|). For example, here's really you tin nonstop nan output of nan ls bid to tee utilizing a pipe:
ls | tee /path/to/output.txtThe tee bid past sends that output to nan surface and to nan record you specify. This will overwrite nan record aliases create a caller 1 if it doesn't exist, conscionable for illustration nan > operator.
To usage nan tee bid to people output to nan surface and append it to nan extremity of a file, adhd nan -a emblem earlier nan record path, for example:
uname -a | tee -a /path/to/output.txtYou'll spot nan bid output connected your surface and you tin usage cat to verify that tee has besides added it to nan file:
Take Advantage of nan Command Line’s Power
There are galore much ways to redirect output, including errors, to a file. With nan powerfulness of nan bid line, you tin show strategy activity, harvester information from respective sources, and more.
Besides redeeming output, there's overmuch much you tin do connected nan terminal. While nan learning curve tin beryllium steep, terminal commands are expressive, composable, and often quicker than GUI alternatives.