Managing Output on Ubuntu Terminal
To be an Ubuntu power user, you must learn the command line. This is the most powerful part of Ubuntu that you can do almost everything from the terminal without the GUI.
In this tutorial we are going to learn how to manage the output of your Ubuntu terminal.
This is a step-by-step tutorial on learn how to manage the output of Ubuntu terminal. To follow this tutorial, you need to have the following requirements:
• A computer with Ubuntu operating system installed.
• Basic knowledge of Ubuntu terminal. With basic I mean, how to open a terminal on Ubuntu and how to execute commands.
First open the terminal of your Ubuntu operating system. You can open your terminal by pressing Control, Alt and T at the same time. You can also go to the Unity dashboard and search by the keyword “Terminal” and click on the terminal. Or you can right click on the Desktop and click on “Open Terminal”.
The ‘less’ command:
In the last tutorial, when you tried to read a very big text file with ‘cat’ command, you saw how problematic it was. What if you could scroll up and down as you do with graphical text editor? Well you can do that with the help of ‘less’ command. Let’s see how!
Last time we read ‘/etc/passwd’ file with ‘cat’. Now let’s read it will ‘less’. Type in the following command and press Enter,
This is a long file. To see more, press the ‘down arrow’ key.
You can go up with ‘up arrow’ key.
You can exit ‘less’ by pressing ‘q’ key.
The ‘grep’ command:
In your Linux journey, you will need to find things on long configuration files. But reading the whole configuration file for just one word is just crazy! So what we Linux admins do is, we use the ‘grep’ command.
The easiest way to use ‘grep’ is “grep searchString filename”.
Let’s search for the username, ‘root’ on ‘/etc/passwd’ with grep as an example. Type in the following command and press Enter.
“grep root /etc/passwd”
As you can see in the screenshot, the line that matches the keyword “root” is displayed.
The ‘|’ and the shell:
This is called a shell pipe. It works like any other real life pipe! It just controls the flow of data. That is, it takes the input of one command as the output of another command.
In our previous example, we used ‘grep’ command to find a specific line from a file ‘/etc/passwd’. It showed only a single line. But that won’t be the case all the time. The result might be long. So what do we do when the output is long? We use the ‘less’ command. So we want the output of ‘grep’ command as the input of ‘less’ command.
To do that, type in the following command and press Enter,
“grep root /etc/passwd | less”
Press ‘q’ to quit ‘less’.