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
Make sure you've installed ImageMagick application on your Linux system. 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
$ sudo dnf install ImageMagick
$ sudo yum install ImageMagick
$ 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.
Here is another one. I saved my Linux Kernel output to an Image.
$ uname -a | convert label:@- mylinuxkernel.png
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:
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.
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.
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.
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
Thank you for the info. especially the security policy mod. thanks.