FLOSS Manuals

 English |  Español |  Français |  Italiano |  Português |  Русский |  Shqip

Terminal

Terminal: CombiningCommands

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 you can combine the ls command with the more command like this:

ls | more

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".

The less command gives you even greater flexibility. The command

 ls | less

initially gives the same display as the command using more, and lets you page down with the spacebar. However, you can page up and down with the up and down arrow keys.

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 standard output of the ls command is fed into the standard input of the less command.

You can actually string many commands together in this way.

What do you think this example does? For example, the sort command sorts its input line by line. The -r option tells it to sort in reverse order. The cut command can return specified columns from its input. What, then, do you think the following might do? You can, of course, just try it, or you can read the help for each command in order to understand it in more detail.

ls | sort -r | cut -c1-3

You can also try various combinations to see what happens:

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


There has been error in communication with Booktype server. Not sure right now where is the problem.

You should refresh this page.