Saving Terminal/Shell Output to a File
# Using the > operator
echo 'Hello World' > test.txt
ls -la > test.txt
# Using the tee command
echo 'Hello World' | tee test.txt
ls -la | tee test.txt
There may be times where you wish to save your terminal or shell output to a file.
Luckily there are 2 simple ways you can do this:
- Output to a file using
>
command - Output to a file using the
tee
command
Using the >
operator at the end of your command.
echo 'Hello World' > test.txt
Above we will output the command to the test.txt
file. In the example above our command was echo 'Hello World'
. We could also list out the current directory:
ls -la > test.txt
And that would save the output to the test.txt file.
Using the tee
command
Tee is a command that takes standing input and writes to standard output. You can |
your command to the tee
command like so:
echo 'Hello World' | tee test.txt
The tee
command will still process the output into your command prompt and it will also write it to the test.txt
file. We can do the same with our ls
command:
ls -la | tee test.txt
And the output will show up in our prompt and in our test.txt
No explanation generated yet.