One of the problems with running Linux commands on a dedicated server or virtual private server (VPS) is that some commands spit out a lot of information. Even something as simple as “ls”, which lists the files in a directory, can go on for pages. In such situations, a terminal program that allows you to scroll is usually sufficient. You can also use the “more” or “less” commands to section the results.
There are times, however, when simply scrolling up is not enough. You may run a command and want to look at the output of the command again at a later time. You may even want to save the data and download it to your computer. To do that, you will need to redirect that output from the screen to a file.
As an example, let’s suppose you want to run through the list of virtual hosts on your server that use “.net” domains rather than “.com”. All of the sub-directories in your virtual host directory are named after the domains of the accounts, so all you have to do is run a command like:
Unfortunately, that action lists 200 domains. You could scroll through all of them, but you would not be able to look at the list later. Now, add the following to the end of the command:
ls *.net > /home/username/dot-net-domains.txt
This will tell the “ls” command to send the results to a text file called “dot-net-domains.txt” in your home directory, rather than the screen.
You can also append command output to the end of an existing file. For example, if you have domains in other directories, rather than creating separate text files for each, you can append each new directory output to the end of the original file.
ls *.net >> /home/username/dot-net-domains.txt
The possibilities are really endless, and you can use the same technique for other commands, even if they are complex.