Redirecting Streams of Text

When you used the echo command to create a file, you took the output of one command and directed it somewhere else. Let’s look at this in more detail.

Execute this command to view all of the running processes on your computer:

 $ ​​ps​​ ​​-ef

The ps command shows you the processes running on your computer, and the -ef options show you the processes for every user in all sessions. Once again, the output of the command streams by.

This problem can be solved in a couple of ways. The first approach would be to capture the output to a file by using the > operator, just like you did to create a text file with echo:

 $ ​​ps​​ ​​-ef​​ ​​>​​ ​​processes.txt

You could then open that file in your favorite text editor and read its contents, or you could use less to view it one page at a time.

But a better way involves less steps. You can redirect the output of the ps command to the less command by using the pipe (|) character. Give it a try:

 $ ​​ps​​ ​​-ef​​ ​​|​​ ​​less

Now you can read the output more easily.

As you work on the command line, you’ll find yourself taking the output of one program and sending it off to another program, which will then output something new. Sometimes you might use three or four programs together to achieve a result. Imagine the data that flows as a stream of text that can be processed by programs. Each program that processes the text does one thing. You’ll learn more detail about this concept later in the book. For now, turn your attention back to files and directories.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.167.87