|
|
|
|
How to store output into to a text file in Linux
By default the command line outputs are displayed onto a screen, but you can easily redirect into a text file or even make it to be used as an input for the following command.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1.
|
|
|
>
Using >, will write the command line results into a text file.
For example: echo "aaa" > text.txt
The command line writes tekst "aaa" into a text file with a name text.txt. In case the file with the same name already exists, the file will be overwrtiten.
|
|
|
2.
|
|
|
>>
If you use >> in a command line, the results will also be written into a text file, but this time the existing file will not get overwritten, the text will get appended to the already existing file content at the end of the file.
For example: echo "bbb" >> text.txt
|
|
|
3.
|
|
|
You can of course use any other Linux command that generates output.
For example: ls > text.txt
The ls command displays all the files in a folder you are in in our case, the list will be redirected into a tetx.txt file instead of the screen.
|
|
|
4.
|
|
|
;
In case you want to run more than one command in a single command line, use ;
For example: ls -l ; ls -l ; ls -l ;
The ls -l will be executed 3 times in a row.
|
|
|
5.
|
|
|
|
Using |, you will command the output to be used as an input int the following command.
For example: cat test.txt | grep aaa
The cat displays the content of text.txt. And this content is then used in the grep command which will only display the lines containing aaa.
|
|
|
6.
|
|
|
We can also create combinations made of commands used in this tutorial.
For example: cat test.txt | grep aaa | grep - v bbb > dat.txt
This command line finds all the lines in the text.txt, containing text aaa but do not contain bbb, the result gets stored into a dat.txt file.
|
|
|
|
|
|
|
|
|
|