Graphical user interfaces (GUIs) allow us to accomplish daily tasks by interacting with windows and icons, and they come in handy for many tasks. However, several users find it better to input text commands into the PC directly rather than dealing with windows and icons for more efficiency and flexibility. This is done via terminals.
Terminals, alias consoles, or command lines are used to enter and transcribe data from a PC system. They also allow us to carry out and automate tasks on a PC without implementing a GUI.
The terminal is the heart of any Linux system. Every program that runs in Linux runs under a terminal command line. This ranges from massive programs like web browsers to simple ones like text editors. Due to this, using the terminal confidently is a vital step in understanding how the Linux operating system works.
Also, if you are a DevOP, mainly a backend one, you inevitably need to do something on a Linux terminal rather than the Graphical User interface. One palpable complication is that the terminal is not visual-friendly, especially when checking out some vast standard output. As such, you must be keen.
Saving the terminal output to files in Linux
While working on the terminal, you may want to save the terminal output to a file for later use as a source of info for another operation or log terminal activity. If you have been looking for ways to do that, you are at the right place. This article has compiled four ways to save the terminal output to a file.
Method 1: Tee
This is one tried and tested terminal to file output method that saves the output of a terminal command using a program known as “tee.” Tee is a GNU program that permits you to read from and write to standard input. It combines with Unix pipes to write the output of a command to a file.
Another critical point to note while using tee is that the tee command simultaneously writes the result of one or more files, unlike other commands. It follows the following syntax:
[command] | tee [options] [filename]
Consider the following example:
echo "fosslinux" | tee hello.txt
Here, we piped (|) the output to the tee command
Note: A pipe conveys the output of one program and feeds it into the input of another. This permits you to glue several programs together, as long as they all utilize inputs and outputs.
In case you want to add content to the hello.txt, append the -a option as shown below:
echo | tee -a fosslinux.txt
Method 2: Output redirection
Another improvised method to deal with this is to utilize the in-built operators of the UNIX shell. These operators are default functions in Linux that can redirect the text output of a program to a file. There are only three operators that help you out while using this method.
>operator will either generate or replace the contents of a file with the output of your program. This is key when you want to dump and view the current result of your program in the same file.
>>operator allows you to generate a file with the output of your program. Although, this operator only adds the output rather than replacing it. This is also key when you want to dump your program’s results into a single file continuously.
- Finally is the
2>operator is a special kind of operator that will print errors reported back. This proves vital during debugging, as it displays the issue encountered at hand by the program before crashing it.
After going through that essential piece of info, let us take you through the output redirection. Consider the following example:
In the above command, we instructed the “echo” program to output the word “fosslinux,” then appended the “>” operator and a file name. The operator then instructed the UNIX shell to write the output to the “hello.txt” file rather than printing it to the screen.
Method 3: Script
This is another excellent way of pulling an output from the terminal. The script is an in-built Linux program that records everything you key in the terminal and its output.
You can even use the script to record the log of your command line session in a parsable text format. This is handy when sending an error log online to get support from other users. To put this method to the test, issue the following command:
Then consider the following example
The above command will start a scripting environment where you can begin issuing commands. One key point to keep in mind is that the script captures every step you carry out in that session.
Ideally, the script comprises all the keypresses and control characters you press. Like in our case, it recorded all of the “invisible” keys that I pressed, including the backspace and the tab keys.
After completing the session, type in “exit,” The session will end the script program and write the whole log to the file you specified.
Method 4: Use of particular terminals
Another simplified way of getting an output from the terminal is utilizing a terminal that supports saving the output to a file. In this instance, we will look at the Konsole terminal that whips with a standard KDE installation. This installation includes an option to write the output to a file which is what we are after.
To achieve this, press the “File” button on the left corner of the terminal window. After that, a drop-down menu with several options will appear. Locate the “Save Output As…” button, and click on it to open.
Hitting the “Save Output As” button will open up a Window dialogue that will permit you to save the terminal output. You can select the storage location to which you want to save the file from this point.
Alternatively, you can also save the output of a command in the GNOME terminal by selecting the intended data to save and right-clicking on the terminal. After that, select “Copy output as HTML” from the drop-down menu. This will, in turn, load the terminal text into your clipboard. You can paste it to any text editor of your liking.
This guide has covered four different methods to help you save the terminal output to a file using Linux or Unix-like OSs. We hope this came in handy for those struggling to save the terminal output to a file. Thanks for reading, and keep following FOSS Linux for more.