If you’re following OS Radar for quite a while, then you may already have noticed that lots of the posts tell about exporting the result to a text file. This is an awesome feature of Linux where you can export the output of a command to a file at your desired location.
It’s extremely useful in tons of cases. For example, you may end up with a very large output that’s giving you a hard time tracking on the terminal window. Or, you think that you need the information later on. Then, using this feature, you can save the output for later usage.
Today, we’ll dive deeper into the tricks.
Exporting output to a text file.
This is the most basic format of exporting output to a help file.
command > /file/path/command.txt
For example, let’s export the output of “help” command.
help
help > ~/Desktop/help.txt
Here, the “>” tells the terminal to export the output to somewhere else. This is the default behavior of the Bash shell. The output of “help” command is redirected to the “help.txt” file.
The file didn’t exist, so it will be created.
Selective export
It’s also possible to selectively export certain part of the output.
For example, when we shared the usage of “history” command in terminal, the entire list is pretty long! You can export the output with the above example with no problem.
Say, you only want to find out the commands you ran with “apt”. For that purpose, we shall be using “grep” – a handy search tool for the Linux environment.
For performing the task, the structure of the command will be something like this –
command | grep “search_term” > /file/path/command.txt
Now, let’s return to the scenario. Check out all the commands performed with “apt” –
history | grep apt
Export the result to a text file –
history | grep apt > ~/Desktop/apt_commands.txt
Multiple exports to a single file
Need to export all your command outputs into a single file? No need to worry.
The command structure will be –
command1 >> /file/path/command.file_extension command2 >> /file/path/command.file_extension command3 >> /file/path/command.file_extension
Let’s check out the following example –
lsb_release -a >> ~/Desktop/demo.txt screenfetch >> ~/Desktop/demo.txt neofetch >> ~/Desktop/demo.txt
Enjoy!