1. Overview

As users of Linux, the terminal is where a lot of work happens, and sometimes, we need to search through it to find the specific information we desire. We’ll explore five ways to make the best use of the terminal, which are grep, the less command, terminal multiplexers, text-processing tools, and saving it to a file to later search for the needed information.

In this tutorial, we’ll look at how we can do that and apply it to the terminal output or history.

2. grep Command

‘Global Regular Expression Print’, also known as grep, is the most used command for searching in the terminal.

The grep command can search for not only strings but also regular expressions. Hence, it utilizes pattern matching and line-by-line processing as the core of its operation.

Monitoring output and searching simultaneously is a great and productive use of executing grep and watch side-by-side:

$ watch 'command_example | grep "string"'

watch monitors the output that is produced by tail every two seconds. Following that, grep searches through the output and displays the result of our search:

$ watch 'tail -f project.log | grep "error"'

This ensures the search works in real-time rather than waiting for tail to finish executing and then searching through it using grep. The terminal displays the new lines containing error being added in real-time.

3. less Command

The less command is used to search through longer outputs as it provides ease of use:

$ git log | less

The less command searches in the output of a command we specify. Following that, the search process requires indicating the pattern:

/false

Generally, we insert the search criteria we need by deploying /patternless begins the search for false throughout our output in a case-sensitive way. To move through the search, we press  for next and for previous. A case-insensitive form of the less command is also available:

$ git log | less -i

Subsequently, we add the search pattern as seen above. Following that, we can add more patterns and continue our search.

Lastly, to conclude our search,h we press q to exit.

4. Terminal Multiplexers

We utilize terminal multiplexers such as tmux and screen to search through the terminal output straightforwardly. The key feature that we’ll focus on is the enhanced scrollback buffer.

4.1. Installation

After updating our package manager, we can install tmux and screen using the package manager:

$ sudo apt install tmux
$ sudo apt install screen

We’ll discuss the use of each of them separately.

4.2. tmux

Using tmux allows us to scroll back and search through the terminal history in a user-friendly manner. To start our search in tmux, we press ctrl + b, then pressing [ allows us to enter copy mode that we utilize to search:

Ctrl + b, [

Navigating the terminal is easily done with the keyboard arrows or the mouse scroll. For a backward search through the terminal history, we press the ? key, and following that, we specify our search criteria. To search forward for another criteria, we press / and type the search query:

/terminated

As we have seen in the less command we can go forward to the next search using n and backward by pressing. Finally, to exit the copy mode and return to the terminal as before,e we press q.

4.3. screen

While using screen follows a similar pathway to tmux, there are differences in the keys used to prompt the search. Firstly, we have to enter copy mode before starting our search:

Ctrl + A, [

Following that, we define the direction of our search:

Ctrl + S

After pressing Ctrl + S, we define our search by typing it. For example, we can look for the word locked in our terminal history, and it scrolls to the position of the search if it’s found:

2023-11-16 09:20:33 Database is locked in UserAuth
Ctrl + R

We press Ctrl + S to search forward in the terminal or Ctrl + R to search in a reverse manner. Lastly, to exit the copy mode in this instance, we press the ESC button.

5. Text-Processing Tools

There are two main text-processing tools we use for terminal searching, which are awk and sed.

5.1. awk 

Awk is both a programming language and a command-line utility. It’s designed for pattern scanning and processing:

$ command_sample | awk '/string or filename/ { action }'

We use ls to display all the files and awk to specifically select our text file and then give it an action of printing the name of the file:

$ ls -l | awk '/\.txt$/ { print $9 }'
buisness.txt 
worknotes.txt

Finally, awk displays the file names in the directory found by l,s as seen above.

5.2. sed

We utilize sed to print the lines containing the string we are looking for:

$ dmesg | sed -n '/failed/p'

All lines containing failed will be displayed as a result of our search.

6. Conclusion

In this article, we discussed a few ways of searching through the terminal output and terminal history.

First, we discussed the grep command briefly and its ability to monitor the output and search simultaneously with the watch and tail commands.

We also discussed other options like using awk, sed, or the less command in case they are more suitable for our search.

Last but not least, we concluded that terminal multiplexers such as tmux and screen are preferred when searching in the terminal history without the need to execute the commands again.

Comments are closed on this article!