> sends the standard output of a command to a file. Nothing appears on the screen. The output is in the file:
text
learner@tidepool:~$ grep ERROR shop.log > errors.txt
learner@tidepool:~$ cat errors.txt
09:30 ERROR printer not foundgrep ERROR shop.log
stdout
errors.txtin place of the terminal
This works with every command that writes standard output: ls, cat, echo, wc, find.
Use echo and > to put text in a file without an editor:
text
learner@tidepool:~$ echo "status=open" > status.txtThe rules of >
| The file | Result |
|---|---|
| Does not exist | > makes it |
| Exists | > empties it first. The old text is gone |
| Is in a directory that does not exist | Error: No such file or directory |
The shell prepares > first
The shell opens and empties the file before the program starts. Two results of this:
- If the command fails, the file still exists, and it is empty.
- Never send output into the file that the same command reads.
grep ERROR shop.log > shop.logemptiesshop.logbeforegrepreads it.