When utilizing the Linux terminal, you could end up wishing you could possibly save the output of the command-line to a textual content file for later, however you don’t perceive how to do it. Saving command outputs to a file is simpler than you suppose! It simply requires a little understanding of the redirection image in Bash. Right here’s how one can output Linux commands to a file.

What’s a redirection image?

The redirection image (aka redirection operator), in primary phrases, makes it doable to ship inputs and outputs from one place to one other.

The < redirection means enter and the > redirection means output. These two operators can do many issues, and there are dozens of makes use of for the redirection on Linux and Unix-like working methods typically. With that mentioned, on this article, we’ll solely be specializing in how redirection can be utilized to output Linux commands to a file.

Output terminal commands to file

Saving a command output from the terminal to a textual content file could be very straightforward, and it really works by using the > image to ship outputs.

In Bash phrases, utilizing > will take any enter and redirect it someplace else. In our use case, we are able to take a redirection and use it to transfer the output of a terminal command and put it in a good, neat textual content file for secure conserving.

For instance, in the event you’re utilizing Ubuntu, and also you need to save the present model of your working system to a file, you’d run the lsb_release command and add a > image on the finish.

9k=

lsb_release -a > ubuntu_version.txt

There you’ve gotten it! You’ve efficiently output a command to a textual content file. That’s all there’s to it! Be at liberty to customise the command above. Simply comply with the syntax beneath.

command > filename.fileextension

Including to an current file

It’s your decision to add a number of commands to a single textual content file, fairly than a single command output.

To print a number of outputs to a single, begin with the primary command and use the > image to create a new file.

command1 > filename.fileextension

Thanks to the redirection image, our output is saved. Now, be at liberty to add extra stuff to the identical file, by profiting from the >> image.

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 beneficial, particularly once you want to save one thing for later. However have you ever ever needed to save a particular part of textual content in your terminal to a file? Or, maybe, textual content with specific key phrases? Thanks to the assistance of the grep command, that is doable.

What’s grep? Grep is a command-line software that permits customers on Linux (and different Unix-like working methods) to search by strains of plain-text for specified patterns. Principally, it’s a tremendous helpful search software for the terminal.

Selective save examples

Maybe top-of-the-line “selective save” makes use of is combining the grep software with the ls command to ship a filtered checklist of folders/recordsdata in a given listing to a file for fast studying:

ls ~/listing/ | grep "search term" > ~/Paperwork/folder-list.txt

One other use for selective saving is filtering out command assist pages. To save lots of a particular part of a command’s assist web page to a textual content file, strive the instance beneath.

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

Suffice it to say; there are dozens of makes use of for selective saving. You are able to do absolutely anything with it, so long as you comply with the syntax beneath.

command | grep "search term" > file.fileextension

Need to add extra to your file, use;

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

Viewing recordsdata

So, you’ve saved your terminal command output to a file. Now you want to view it — however how? Effectively, within the Linux terminal, there are a few methods of viewing textual content recordsdata.

One of many major methods of viewing the contents of recordsdata within the terminal is cat. It’s a easy utility that reads the contents of recordsdata and prints it on display. Better of all, the cat software works with all sorts of recordsdata, so it doesn’t matter what file extension your output is saved as, it’ll learn it.

To view your file, launch a terminal and write cat adopted by the placement. For instance:

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

If the output is lengthy, contemplate including on extra to the top. It’s going to enable you to flick through the file slowly by urgent the Area or Enter on the keyboard.

Want to edit the textual content of your command output? Contemplate opening it up with a textual content editor as a substitute! There are numerous textual content editors on Linux, however the best one to take care of for a easy edit is Nano.

Be aware: not all Linux distributions ship with Nano, so you could want to set up it first earlier than getting into the command beneath.

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




Source link

Share.
Leave A Reply

Exit mobile version