Controlling Output

If you just type single commands and do nothing else with them, you’ll see the output printed to your screen. The shell also lets you send this output elsewhere, though, using two important techniques: first by stream redirection and second by creating pipelines.

Stream Redirection

We can redirect streams using > and >> after the command whose output we want to redirect. The first will write the output to a file, clearing the file’s contents if it exists. The second will also write to a file, but will append to the existing content if the file already exists.

To revisit the ls example:

 
user@computer:~ $ ​ls​​ ​​-l​​ ​​Pictures​​ ​​>​​ ​​files.txt
 
 
user@computer:~ $ ​cat​​ ​​files.txt
 
-rw-r--r-- 1 rob staff 75130 2 May ...

Get Text Processing with Ruby now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.