Linux

Linux redirected output not showing in file

You may need to redirect output in linux to a file. However, recently, I had a command that didn't redirect in the normal expected way. Here is what I found.

Recently, when working with Nginx, I wanted to export out the details of my Nginx build so I could see all modules the webserver was compiled with. ย However, each time I tried to redirect the output of theย nginx -Vย command, the linux redirected output not showing in the file. ย I would see the command output on the screen, the file I was redirecting to was created, but nothing was being added to the file. ย The command I was using was: ย nginx -V >> output.txt

The terminal never complained about syntax issues with the command, however, just wasn’t appending any data to the file. ย After a search session on Google, I found the error of my ways. ย The command needed to be structured: ย nginx -V &> output.txt

The “&” addition to the command syntax, tells the terminal processor to redirectย both standard outputย andย standard error outputย to the file. ย If you don’t redirect both with some commands, the standard output is simply displayed on screen and the file is never used for output even though it is created. ย Other commands such asย ls ย however work as expected without the “&” in the command string.

If you run into an issue where you are needing to capture the output of a command in the terminal in Linux and can’t seem to get the command to output correctly to the file, most likely, you have an issue where you need to redirect the standard and error output both for the command to work. ย The symptoms also can be the command output is displayed on the screen if not redirected correctly, and not displayed on the screen when it does redirect correctly.

Take a look at the screenshot below for the results of the command that was executed and redirected properly. ย Notice that we get only the command and returned back to a terminal prompt.

redirect1

Subscribe to VirtualizationHowto via Email ๐Ÿ””

Enter your email address to subscribe to this blog and receive notifications of new posts by email.



Brandon Lee

Brandon Lee is the Senior Writer, Engineer and owner at Virtualizationhowto.com, and a 7-time VMware vExpert, with over two decades of experience in Information Technology. Having worked for numerous Fortune 500 companies as well as in various industries, He has extensive experience in various IT segments and is a strong advocate for open source technologies. Brandon holds many industry certifications, loves the outdoors and spending time with family. Also, he goes through the effort of testing and troubleshooting issues, so you don't have to.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

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