The Linux commands on their own are pretty powerful and useful tools but until you know how to combine them they show just a shadow of their true power. In this series of 4 tutorials, we will look at various tricks and tools you can use to make your shell work easier, faster and more powerfully.
This first section covers linking commands together so that the output of one command is immediately sent to another. This is called piping and is very simple to use but makes the shell a lot more of a friendly place. We also cover redirections which is where we send the output of a command to a file or we take a file as an input. Finally, we will look at backticks which are a seriously powerful tool which is rarely used. The backtick allows you to execute a command in the middle of another command string and use that command’s output as a parameter for the primary command. That may sound confusing but you will see it is easy and very useful.
The pipe symbol looks like this: |
The location of it varies depending on your keyboard layout. Some keyboards have the pipe symbol placed above the right shift key, while others have placed it next to the left shift key. Still others have it placed to the left of the number 1 key at the top left of your keyboard.
The pipe command does what its name suggests and pipes the output of one command into another command. The simplest example of this is if you go to your root directory with cd / and enter du, the output will scroll past you in a totally unhelpful manner. You can use the pipe command to send the output to less or more. Try using this: du | less. Piping is not limited to once per command, however, so if you wanted to filter those lines to only show those containing home in them you could use du | grep “home” | less.
The more commands you learn the more ways you can use pipe to join them together.
The redirection symbols are < and >. The most useful usage of redirection is to send the output of commands to a .txt file for later use. To redirect the output of a command to a filename rather than to the screen use >. As an example: du >du.txt. This will send the output of du to a text file called du.txt.
In addition to redirecting all output to a file, you can specify which output to send to a file. Linux has 3 standard open file handles. These are stdout (standard output stream), stderr (standard error stream) and stdin (standard input stream). These have the following numeric id’s respectively: 1,2 and 0.
If you execute a command that outputs error messages and other information, you may want to separate these out into different files. To do this you would redirect each type of output like this. 1> and 2>.
For this example, we will use a folder with one file in it called file.txt. If you execute this command: ls -la file.txt file2.txt, you will get the following displayed on the screen:
[root@plesk redirect]# ls -la file.txt file2.txt ls: cannot access file2.txt: No such file or directory -rw-r--r--. 1 root root 0 Sep 18 18:46 file.txt
The error is mixed up with the normal output. For a simple command like this that is not an issue. However, if you are running a complex script or a program with complicated output you may want to split the errors from the standard output. To do this for the above example you would use ls -la file.txt file2.txt 1>std.txt 2>err.txt.
If you now run ls -l again you will see this:
[root@plesk redirect]# ls -la total 36 drwxr-xr-x. 2 root root 4096 Sep 18 18:49 . dr-xr-x---. 16 root root 4096 Sep 18 18:46 .. -rw-r--r--. 1 root root 55 Sep 18 18:49 err.txt -rw-r--r--. 1 root root 0 Sep 18 18:46 file.txt -rw-r--r--. 1 root root 48 Sep 18 18:49 std.txt
You can now cat each file to view the errors separate from the output:
[root@plesk redirect]# cat err.txt ls: cannot access file2.txt: No such file or directory [root@plesk redirect]# cat std.txt -rw-r--r--. 1 root root 0 Sep 18 18:46 file.txt
You can, of course, omit either option in the previous example so that the errors go to the file but the main info goes to the screen.
If you want to save both the error and the output to the same file then use &>.
The final part of redirection is that the > symbol redirects the output to a file but truncates the file first. If you want your output to be added to an existing file then you need to use >> which appends the output. You can combine any of the above options with >> such as 1>> 2>> or &>>.
Redirecting input is the opposite but you never need to use the <0 option as there is only one input stream. Simply use command < inputfile.txt.
Note: The backtick should not be mistaken for the single quote. They do look the same and can be easily confused. The backtick is to the left of the number 1 on most keyboards.
The backtick symbol is `. Anything between the backticks is executed first and the output is inserted into the full command. This may sound complicated so here is an example. This example is a little bit contrived in order to keep things simple. echo `cat filename.txt | wc -l` The output of this command would be the number of lines in filename.txt. Of course, in this case, the echo and the backticks are unnecessary but it does show what is being done. When you hit enter on this command the commands between the “ are executed first. in this case cat filename.txt | wc -l. The results of this will then be placed in the remainder of the command to replace the backtick command. So if the file had 23 lines in it the resulting line would be echo 23.
Here is a real-world example:
mysql -uadmin -p`cat /etc/psa/.psa.shadow` psa.
This is used on plesk servers to access the database at the command line. The password for the plesk database is stored in the file /etc/psa/.psa.shadow. So you could look at this file and copy and paste the password. Or you can run this command which will cat the password directly into the command for you.