Dreevoo.com | Online Learning and Knowledge Sharing
Home | Programs | Microsoft Windows | How to store output into to a text file in Linux
Click to view your profile

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.

  Author: mat | Version: Ubuntu 12.04 | 28th September 2013 |  


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.



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


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.



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.



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.


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.

  Please login to post a comment
online learning made for people