It’s common to redirect output from a command to files and pipes. While there are many examples with some commands, others are rarely used for such cases.
In this tutorial, we’ll explore how to write all lines from the less command to a file. First, we’ll start with a brief introduction to the usage and syntax of less. After that, we’ll create an example file containing a number of lines. Finally, we’ll explore several methods to write all lines to a file using the less command.
2. less Command Overview
The less command is a command-line pager that displays the file’s contents on the terminal one page at a time. Since less doesn’t have to wait for the entire file to load, it’s usually much quicker than text editors like vi.
less not only supports different file formats but also has features like horizontal scrolling. less has a basic syntax:
less [options] filepath
In addition, we can also use less in a pipeline to view the output of the previous command:
$ command 1 | less
In this case, we open the output of command 1 in less.
3. Example File Content
We’ll be seeing how to write lines from less to a file in various ways.
For this, we’ll create a sample file sampleText.txt. The text file consists of approximately 100 paragraphs and 9000 words.
Of course, we want to make sure that our command writes all of the content from less to a new file. Therefore, we’ll first determine the exact number of lines using less for verification:
$ less sampleText.txt
Once the file is open inside less, we’ll press the = key to get more information about the contents:
sampleText.txt lines 1-14/204 byte 3726/63002 6% (press RETURN)
The information includes the filename and the number of lines on the current page in less. Furthermore, we also see the total number of lines in the file. Next, we have the total size in bytes followed by the portion of the file visible on the current page as a percentage.
We’ll use q to quit the less command once we’ve viewed the file information.
4. Using tee
The first method to save the output from less is to use the tee command. In this approach, we pipe the output of less to tee:
$ less sampleText.txt | tee output.txt
Thus, the less command redirects the output to the tee command. The tee command reads the input from less and displays the content on the terminal. In addition, the pipeline writes all sampleText.txt lines to output.txt.
5. Using Redirection Operator
Similarly, we can also use the redirection operator to save the output from less to another file:
$ less sampleText.txt > output.txt
In this method, the > operator redirects the content of sampleText.txt to output.txt overwriting it if it already exists.
Alternatively, to prevent the command from overwriting the file, we can use >>:
$ less sampleText.txt >> output.txt
After executing this command, the content from sampleText.txt is appended to output.txt.
6. Using the s Command
The above two methods focused on options external to the less command. However, we can also write all lines to a new file by using features of less itself. In this case, we’ll use the s command.
The s command of less enables us to save the input to a file from less. It’s crucial to note that the s option only works if we pipe the file contents to less from the terminal.
Thus, to demonstrate the result, we first pass the output from the cat command to less:
$ cat sampleText.txt | less
Once the file is opened in less, we press the s key to see the log file option:
Now, we can type the name of the file where we’ll save the entire content:
After that, we hit the Return key so that all lines from less are written to a file. Lastly, we use q to exit.
To verify the new file has been created, we use the ls command:
$ ls -l out.txt -rwxrwxrwx 1 sidrah sidrah 63002 Jun 16 23:32 out.txt
Alternatively, we can also recheck the file content using the cat command:
$ cat out.txt
This should show us the same content as our original file.
7. Write Lines Using the g Command
Alternatively, we can also use the g command inside less to write all lines to an external file. Moreover, we can use less with g without needing to pipe data in.
The g command brings the cursor to the N-th line in the file. In other words, if we press g, the cursor automatically goes to the default line number which is usually the first line of the file.
For this method, we don’t have to pipe the file contents to less. So, we can directly open any file using the less command:
$ less sampleText.txt
Once inside the less viewer, we press g or < to get to the first line and see the : prompt. After that, we type the pipe operator (|):
Next, we see the mark option:
Now, we press $ to request everything up to the last line:
Once we press the dollar sign, we’ll see the ! symbol on the screen:
Lastly, let’s use cat to write the complete file content to a different file, out.txt:
! cat > out.txt
This way, we’re able to write all lines from less to a file.
Finally, we press the Return key to complete the operation:
| done (press RETURN)
Now, we hit q to exit less.
What we did above is get all lines from the first (g, <) to the last ($) within less and direct them to out.txt.
Once done, we can again verify that the new file has been created:
$ ls -l out.txt -rwxrwxrwx 1 sidrah sidrah 63002 Jun 16 23:41 out.txt
Unlike the s command, the g command works regardless of whether we pipe content from the terminal or directly open a file in less.
In this article, we’ve looked at the basic usage of the less command. In addition, we’ve learned several methods we can use to write all lines from less. First, we discussed setting up the basic file for our examples. Next, we saw two methods to write all lines from one file to another using less in the terminal.