How to send Linux command output to a file

If you have a command that outputs a lot of data to the terminal, you might want to send that output to a file for easier (or later) viewing or sharing. Jack Wallen shows you how.

linuxhero1.jpg

Image: Jack Wallen

If you're new to the world of the Linux command line, then you know how eye-openingly powerful it can be. In fact, the sky's the limit with what you can do from the Command Line interface. But, sometimes you'll run a command and the output flies by so fast you couldn't possibly catch everything necessary to get the task done. 

This is especially so when the command is of the informative nature. When you're working remotely, or on hardware that doesn't allow you the option to scroll back through the output, what do you do? 

You can always pipe the command through the likes of less, so you can page through the output. What if the output of the command is too long, or you need to save it for later or share it with someone to get a second set of eyes on an issue? 

SEE: Kubernetes security guide (free PDF) (TechRepublic)

For that, you can send the output of the command to a file. It's really easy and incredibly handy. Let me show you how. 

We'll demonstrate with the simple ip a command, which lists out all the information for your internet connections. If you issue the command ip a, you'll see all of the details for all of your interfaces. It's fine if you only have a couple of interfaces. 

If you're also dealing with virtual interfaces or containers, that list can get considerably larger. That's when you'll want to send the output to a file. 

To do that, you'd issue a command like: 

ip a > output_filename 

Where output_filename is the name of the file you want to create. 

You can then view the contents of the file or attach them to an email. If you run that command again, it'll overwrite the contents of the file with the new output. If you'd like to append the output to the end of the file (so you can keep multiple instances of command output in one location), the command could be: 

ip a >> output_filename

When appending, you can help yourself out by adding the date to the beginning of the new section like: 

date >> output_filename && ip a >> output_filename

This would insert a timestamp ahead of the next ip a output. 

And that's all there is to sending the output of a command to a file in Linux. 

Subscribe to TechRepublic's How To Make Tech Work on YouTube for all the latest tech advice for business pros from Jack Wallen.

Also see