1. Home
  2. Linux

How to output Linux commands to a file

When using the Linux terminal, you may find yourself wishing you could save the output of the command-line to a text file for later, but you don’t understand how to do it. Saving command outputs to a file is easier than you think! It just requires a little understanding of the redirection symbol in Bash. Here’s how you can output Linux commands to a file.

What’s a redirection symbol?

The redirection symbol (aka redirection operator), in basic terms, makes it possible to send inputs and outputs from one place to another.

The < redirection means input and the > redirection means output. These two operators can do many things, and there are dozens of uses for the redirection on Linux and Unix-like operating systems in general. With that said, in this article, we’ll only be focusing on how redirection can be used to output Linux commands to a file.

Output terminal commands to file

Saving a command output from the terminal to a text file is very easy, and it works by utilizing the > symbol to send outputs.

In Bash terms, using > will take any input and redirect it somewhere else. In our use case, we can take a redirection and use it to move the output of a terminal command and put it in a nice, neat text file for safe keeping.

For example, if you’re using Ubuntu, and you want to save the current version of your operating system to a file, you’d run the lsb_release command and add a > symbol at the end.

lsb_release -a > ubuntu_version.txt

There you have it! You’ve successfully output a command to a text file. That’s all there is to it! Feel free to customize the command above. Just follow the syntax below.

command > filename.fileextension

Adding to an existing file

You may want to add multiple commands to a single text file, rather than a single command output.

To print multiple outputs to a single, start with the first command and use the > symbol to create a new file.

command1 > filename.fileextension

Thanks to the redirection symbol, our output is saved. Now, feel free to add more stuff to the same file, by taking advantage of the >> symbol.

command2 >> filename.fileextension

command3 >> filename.fileextension

command4 >> filename.fileextension

command5 >> filename.fileextension

Selectively save commands

Sending the output of a command to a file is useful, especially when you need to save something for later. But have you ever wanted to save a specific section of text in your terminal to a file? Or, perhaps, text with particular keywords? Thanks to the help of the grep command, this is possible.

What is grep? Grep is a command-line tool that allows users on Linux (and other Unix-like operating systems) to search through lines of plain-text for specified patterns. Basically, it’s a super useful search tool for the terminal.

Selective save examples

Perhaps one of the best “selective save” uses is combining the grep tool with the ls command to send a filtered list of folders/files in a given directory to a file for quick reading:

ls ~/directory/ | grep "search term" > ~/Documents/folder-list.txt

Another use for selective saving is filtering out command help pages. To save a specific section of a command’s help page to a text file, try the example below.

command --help | grep "search term" > ~/Documents/command-help.txt

Suffice it to say; there are dozens of uses for selective saving. You can do just about anything with it, as long as you follow the syntax below.

command | grep "search term" > file.fileextension

Want to add more to your file, use;

command | grep "search term" >> file.fileextension

Viewing files

So, you’ve saved your terminal command output to a file. Now you need to view it — but how? Well, in the Linux terminal, there are a few ways of viewing text files.

One of the primary ways of viewing the contents of files in the terminal is cat. It’s a simple utility that reads the contents of files and prints it on screen. Best of all, the cat tool works with all types of files, so no matter what file extension your output is saved as, it’ll read it.

To view your file, launch a terminal and write cat followed by the location. For example:

cat ~/location/of/command-output.txt

If the output is long, consider adding on more to the end. It will allow you to browse through the file slowly by pressing the Space or Enter on the keyboard.

Need to edit the text of your command output? Consider opening it up with a text editor instead! There are many text editors on Linux, but the easiest one to deal with for a simple edit is Nano.

Note: not all Linux distributions ship with Nano, so you may need to install it first before entering the command below.

nano -w ~/location/of/command-output.txt


Leave a comment