Home Command line utilities How To Save Linux Command Output To An Image Or A Text File
Save Linux Command Output To An Image Or A File

How To Save Linux Command Output To An Image Or A Text File

By sk
967 Views

Have you ever been in a situation where you wanted to send the output of a Linux command to your colleague or friend to get help? This simple Linux hack will definitely be useful to you. You can save a Linux command output to an image or a file. All you need to have is only ImageMagick. This can be helpful when you need to send the output to a technical support person or a colleague.

Save Linux Command Output To An Image

Install ImageMagick tool first. It is available in the default repositories of most Linux distributions.

For example, to install ImageMagick in Arch Linux and its derivatives, run:

$ sudo pacman -S imagemagick

On Debian, Ubuntu and other DEB-based systems, you can install it a shown below.

$ sudo apt-get install imagemagick

On Fedora:

$ sudo dnf install ImageMagick

On CentOS:

$ sudo yum install ImageMagick

On openSUSE:

$ sudo zypper install ImageMagick

Now, to save a output of any Linux command to an image file, just run the following command:

$ ip a | convert label:@- myipaddress.png

The above command will save the ifconfig command output to an Image and save it in the current working directory. Let us bread down the above command and see what each option does.

  • ip a will display the IP address of your Linux system.
  • convert command will save the output to an Image.
  • label:@- myipaddress.png will save the command output to the image named myipaddress.png.

Here is the output of the above command in my Ubuntu 18.04 desktop system.

Save Linux Command Output To An Image Using ImageMagick

Here is another one. I saved my Linux Kernel output to an Image.

$ uname -a | convert label:@- mylinuxkernel.png

Sample output:

Save Linux Command Output To An Image Using ImageMagick on Ubuntu Linux

What we have seen above is we have saved the command's output in an Image file.We also can save the output on an existing Image file. To do this, run:

$ convert -font -misc-fixed-*-*-*-*-*-*-*-*-*-*-*-* -fill black -draw "text 270,260 \" `lsb_release -a` \"" image.png systemdetails.png

This command will print the output of "lsb_release -a" command to an image called image.png and saves it with a new name "systemdetails.png".

Here is the output of the above command:

Save Linux Command Output To An Image File Using Convert Command

Pretty easy, isn't? You can save the output of any command in an image file and send it to anyone who can help you to fix your system.

Troubleshooting

In recent Ubuntu Linux distributions, certain convert operations are restricted for security reasons. If you tried the above commands in Ubuntu OS, you might be encountered with the following error message:

convert-im6.q16: not authorized `@-' @ error/property.c/InterpretImageProperties/3516.
convert-im6.q16: no images defined `myipaddress.png' @ error/convert.c/ConvertImageCommand/3258.

In that case, you need to edit policy.xml configuration file and change the policy.

$ sudo nano /etc/ImageMagick-6/policy.xml

Find the following line:

<policy domain="path" rights="none" pattern="@*" />

Comment out or remove the following line:

[...]
<!--  <policy domain="path" rights="none" pattern="@*"/> -->
[...]

Save and close the file. Now you can be able to save the command's in the image file.

Save Linux Command Output To A Text File

We know how to save a command's output to/into a image. We can also save the output of a Linux command to a file too.

For example, to save the "ip addr" command's output to a file called myipaddress.txt, run:

$ ip addr > myipaddress.txt

To verify it, view the text file using your favorite text viewers. Or, we can do using "cat" command like below.

$ cat myipaddress.txt

The following command will save my pacman.log to a file called mylogs.txt.

$ tail -f /var/log/pacman.log  > mylogs.txt

And, we can easily save the IP details using command:

$ ip a > mynetworkdetails.txt

You might wanted to write the output of a command to multiple files. Here is how to do it.

$ uname -a | tee file1 file2

The above command will write the output of "uname -a" command to file1 and file2. If the files doesn't exist already, it will create them.

By default, it will overwrite the contents of file1 and file2. If you want to append the output to the existing contents, use -a flag like below.

$ ip a | tee -a file1 file2

This command will not overwrite the existing contents of file1 and file2. Instead, it will simply append the output of the ifconfig command to file1 and file2. In other words, these two files now have the outputs of "uname -a" and "ip a" commands.

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like

2 comments

Erik Lundmark January 22, 2020 - 5:54 am

Doesn’t work on Debian (Testing): I get this error after fixing the policy error:
convert-im6.q16: width or height exceeds limit `@-‘ @ error/label.c/ReadLABELImage/138.
convert-im6.q16: no images defined `myipaddress.png’ @ error/convert.c/ConvertImageCommand/3258.

Reply
sk January 22, 2020 - 12:07 pm

Hi Erik,

This is probably related to large images. Please try with smaller size images. Also check this thread. It might help. https://www.imagemagick.org/discourse-server/viewtopic.php?t=31438

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More