When you use Terminal there may arise a situation where you want to save the text or output that you have generated. This can be useful as a backup or a method to record what you have been doing. For example, if you want to save the list of files within a folder, this text can be saved for future reference. There are two methods to save out Terminal output. The first is to use the built in menu bar command, the second is to use a Terminal command. Both have there advantages and disadvantages.
Using Terminal’s Menu Command
This first method is to use the built in command within Terminal. If you go to Shell > Export Text As you can save the current Terminal output as text.
This command will save everything within Terminal since you have started the session. This includes commands, outputs of those command and the small bit of text (the current folder, and user) before the command. This method is useful in saving everything, since you have a complete record. It can also save output text that is constantly updating, such as the
top command, which is kind of hard to output with the next trick.
The problem with command, is that it does save everything. Most of the time you don’t need the command you entered 50 lines previous.
Saving Text Within Terminal
Terminal has a method in which you can save the output of a command, such as
ls for listing the files within a directory, to a text file. This has the advantage of being very clean in terms of text output and the ability to customise its output.
To save the text output open terminal and write the follow:
[command] > textfile.txt
Note that this will save the text file to the current directory. You can specify a full path for the location of the text file. This command will create a new file each time. So if you repeat the command, it will write out a new file. If you want to append the text to the end of a text file you can use the following command.
[command] >> textfile.txt
This trick has the advantage in that it is very clean, it will only save the output of the command.
The disadvantage of this command is that you have to set up your command each time, which can be tricky if it is long. Plus the text output has to finish, for example it doesn’t work with the
top command, since the command will never exit thus the text file cannot be generated.
Each command has its own advantages and disadvantages. I prefer the latter trick as it only saves out what I want. At the end of the day, you can still select the text from within Terminal and copy the text out using Command + C or the menu bar command.