Terminal Activity

Combining Commands

You can combine commands using the Terminal Activity. There are many situations where this is very useful. For example, if you were to use ls the output to the terminal would be a list of all the files and folders in the directory you are currently in. If this is a very long list then the names of the files and folders flash past so quickly you cannot read them. So we can combine the ls command with another command so that we see the list of names one "page" at a time. To do this combine the ls command with the less command like this:

ls | less

Now the list of file and folder names fills up the display area in the terminal but stops when the terminal window is full. It then waits for you to press the spacebar to display the next "page".

You combine commands by using the vertical line that you see in the example command. This must always go between the commands you wish to combine. This line is known as the "pipe" and in the above example we would say that we "pipe ls through less". That is to say, the output of the ls command is fed through the less command.

You can actually string many commands together in this way. However it's good to remember that this does not always work!

What do you think this example does?

ls | sort -r | cut -c1-3

You can try it in various combinations to see what happens:

ls | sort -r
ls | cut -c1-3