Linux redirected output not showing in file
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.