Previous Page
PCLinuxOS Magazine
Article List
Next Page

Send Command Line Output To A File Or Image

by Paul Arnote (parnote)

It's understandable that some Linux users shun the use of the command line. Sure, there's a lot to remember. And considering how good the various Linux desktops have become, those users with a command line phobia almost never have to face their fear and nemesis. But those users are missing out on the speed and robustness of the command line that can't be matched with a GUI desktop.

So, if you're still reading this article by this point, you must fall into one of two categories. First, you might be a regular user of the command line. Or second, you might have a desire to use the command line (or become more proficient at it).

One thing you can do that might be helpful is to send the output from your commands to either a file or image. This will enable you to easily share the information with others, something that is desirable when you are having difficulties and asking for help.

Send Output To A File

Probably the easiest thing to do is to send the output of a Linux command to a file. This is called redirection. We simply redirect the output to the file that we specify.

So, entering ifconfig > ifconfig.txt will create a text file with the output of the ifconfig command in the current working directory. Here's the output:

eth0      Link encap:Ethernet  HWaddr 00:1E:33:CC:38:41  
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

lo        Link encap:Local Loopback  
          inet addr:  Mask:
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:8117497 errors:0 dropped:0 overruns:0 frame:0
          TX packets:8117497 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:3646040980 (3.3 GiB)  TX bytes:3646040980 (3.3 GiB)

tun0      Link encap:UNSPEC  HWaddr 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00  
          inet addr:  P-t-P:  Mask:
          RX packets:265420 errors:0 dropped:0 overruns:0 frame:0
          TX packets:230357 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:100
          RX bytes:239563564 (228.4 MiB)  TX bytes:30136829 (28.7 MiB)

wlan0     Link encap:Ethernet  HWaddr 00:22:5F:C0:4F:87  
          inet addr:  Bcast:  Mask:
          RX packets:1117090 errors:0 dropped:0 overruns:0 frame:0
          TX packets:945128 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:974768414 (929.6 MiB)  TX bytes:225041909 (214.6 MiB)

Of course, I've obliterated the IP address that I'm using in the output above, replacing the entire address with zeroes (under tun0, which represents my VPN).

But what if you have results of another command that you want to share? You could just redirect it to its own file (and share two separate files), or you could append the results of the second (and third, fourth, fifth, and so on) command to the file made with the first command (and just have one file to share). For example, let's say that besides the results from the ifconfig command, you also need to include the results from the ip netconf command. You can append the results of the second command to the end of the results from the first command by entering ip netconf >> ifconfig.txt.

Redirection, however, doesn't always work as well as you might expect or hope. For instance, if you were to run the command inxi -v3 and try to redirect it to a text file, the special formatting of the output that gives colors and bold text to certain aspects of the output come out confusing when redirected to a text file.

This is what you expect to get, and what you get when the command results are displayed on your screen:

Instead, you end up with this (shortened for space considerations):

[1;34mSystem: [0;37m [1;34mHost:[0;37m localhost.localdomain [1;34mKernel
:[0;37m 4.11.4-pclos1 x86_64 (64 bit [1;34mgcc:[0;37m 4.9.2)[0;37m
[1;34m [0;37m [1;34mDesktop:[0;37m Xfce 4.12.3 (Gtk 2.24.26)
[1;34mDistro:[0;37m PCLinuxOS[0;37m
[1;34mMachine: [0;37m [1;34mDevice:[0;37m laptop [1;34mSystem:[0;37m TOSHIBA
[1;34mproduct:[0;37m Satellite L305 [1;34mv:[0;37m PSLB8U-14F025[0;37m
[1;34m [0;37m [1;34mMobo:[0;37m TOSHIBA [1;34mmodel:[0;37m Portable PC [1;34mBIOS:[0;37m INSYDE [1;34mv:[0;37m 1.80 [1;34mdate:[0;37m 03/20/2009[0;37m

[1;34mCPU: [0;37m [1;34mSingle core[0;37m Intel Celeron 900 (-UP-)[0;37m [1;34mcache:[0;37m 1024 KB[0;37m
[1;34m [0;37m [1;34mflags:[0;37m (lm nx sse sse2 sse3 ssse3) [1;34mbmips:[0;37m 4389 [1;34mspeed:[0;37m 2194 MHz (max)[0;37m
[1;34mGraphics: [0;37m [1;34mCard:[0;37m Intel Mobile 4 Series Integrated Graphics Controller [1;34mbus-ID:[0;37m 00:02.0[0;37m
[1;34m [0;37m [1;34mDisplay Server:[0;37m X.Org 1.19.3 [1;34mdrivers:[0;37m v4l,intel [1;34mResolution:[0;37m 1280x800@60.00hz[0;37m
[1;34m [0;37m [1;34mGLX Renderer:[0;37m Mesa DRI Mobile Intel GM45 Express[0;37m
[1;34m [0;37m [1;34mGLX Version:[0;37m 2.1 Mesa 17.1.2 [1;34mDirect Rendering:[0;37m Yes[0;37m
[1;34mNetwork: [0;37m [1;34mCard-1:[0;37m Realtek RTL8101/2/6E PCI Express Fast/Gigabit Ethernet controller[0;37m
[1;34m [0;37m [1;34mdriver:[0;37m r8169 [1;34mv:[0;37m 2.3LK-NAPI [1;34mport:[0;37m 2000 [1;34mbus-ID:[0;37m 02:00.0[0;37m

If you look closely, you can see that the desired information is in there, but I wouldn't call this a very readable format.

So, any command line program that outputs formatted text isn't a good candidate for redirection. But any program that outputs plain text is an excellent candidate for redirection, if you need it.

Send Output To An Image

Why, yes, you can send the output to an image. Before you squish up your nose and ask why, just follow along. I'm sure that by the end of this discussion, you are likely to see the value of sending your terminal output to an image.

First, you will need to insure that the ImageMagick package is installed. It is in the PCLinuxOS repository. Just open Synaptic and install it ... that is, if it's not already installed. If you're not familiar with the ImageMagick set of tools, you owe it to yourself to familiarize yourself with them. They literally are the proverbial "Swiss knife" of the graphics world.

In the first example, we're going to use a "pipe" to send the output to the ImageMagick convert command, which will then "print" the text into a graphic.

Here's the command:

ifconfig | convert label:@- myipaddress.png

Here is the resulting image:

It's very simple, with black text on a white background. As I did before, I obscured the IP address I'm using, via my VPN provider (under tun0), by pixelizing the information. If you want a colored background, add the -background [color] flag right after the convert command. If you want to use a different color than black for the text, use the -fill [color] flag somewhere between the convert command and the label portion of the command. You can find a whole list of colors used by ImageMagick here. There is a "chart" about one quarter of the way down the page. You can use the color name (easiest), the RGB color code, or the hex color code.

Thus, the following will give you the same thing as above, but with green text on a gold background:

ifconfig | convert -background gold -fill green label:@- myipadd.png

(Hint: using -background black and -fill green1 will mimic the traditional terminal text, with green text on a black background).

If you want to get really fancy, you can also overlay the text over the top of another image. Here's the command (entered all on one line), followed by the result:

convert -font -misc-fixed-*-*-*-*-*-*-*-*-*-*-*-* -fill black -draw "text 25,50 \" `ip addr` \"" FuzzyBackground.jpg myip.jpg

Just like with the last command, changing the -fill black flag to some other color will create text in the specified color overlaying the background image.

Just like with redirection above, attempting to send command output to an image file when the output is formatted (as with the inxi -v3 command previously used) results in gobbledygook for output. Below is a snippet from the image created from attempting to pipe the inxi -v3 output to an image:

As with the results from trying to use redirection to capture the output in a file, the information is all there, but it isn't in a format that could be described as a human-readable output. So, just as with redirection, if a program formats the output with colored text and bold text, it probably isn't a good choice for piping the output to an image. But, if the output from a program is plain text (no formatting other than line breaks), then you can easily capture the output to an image.


I suspect that this might be of somewhat limited use, but when you need the ability to capture the output of a command, there is little else that accomplishes it as easily or completely. Sure, you could use a screen capture utility (for images), and it works well ... until the output exceeds your screen's height. Sure, you could highlight and copy the text from the terminal, then paste it into a text editor, but that takes multiple steps. Why do multiple steps (and create more work) when you can simplify the task by cutting out steps?

Previous Page              Top              Next Page