How to Redirect Output to a File in Linux

Are you struggling with homework assignments that require you to submit a text file with command line output from the shell? Do you need to redirect this output to a file so that your professor can see that you correctly executed the commands? If so, keep reading.

First, let’s take a look at how this works. If you execute the LS command, you will see the contents of the files and folders in your home directory. If you execute LS -l (long form), you will get more detailed information about these files and folders. If you execute PWD (present working directory), it will return the output to the screen.

To redirect this output to a file, use the greater than sign (>). For example, if you execute PWD > Documents/ex1.txt, the output will be redirected to a file called ex1.txt in the Documents folder. Notice that no output was echoed to the screen.

If you want to append the output of a second command to the end of this file, use two greater than signs (>>). For example, if you execute LS -l >> Documents/ex1.txt, the output of this second command will be appended to the end of the ex1.txt file in the Documents folder.

You can continue appending each new command to the end of this file and then edit the file to include headings before the specific command outputs. This edited file can then be saved to a USB drive or emailed to yourself.

To access this file in the graphical user interface, go to Places and look in the Documents folder. Double-click on the file to open it in the editor.

I hope this tutorial was helpful. If you have any questions, please do not hesitate to ask your professor.