The pipe (|) character is used to combine multiple commands. A pipe is a temporary storage place where the output of one command is stored and then passed as the input for a second command. Pipes are used to run more than two commands from the same command line. The sort command is used to sort data. When you run the ls –l command, you will see a listing of the files in a directory with each file shown on a separate line. When you use a pipe after the command and then sort your results, you will sort the data listed on the screen by the list command. The sort command has a variety of arguments that allow you to sort files using different options:
- -n, sorts by name.
- -n .3 Use the first field but the third position in that field.
- -r Sort from last to first.
- -c Check for whether the data has already been sorted.
- -i Ignore nonstandard characters.
- -f Use lowercase letters the same as uppercase would be used when sorting.
- -b Ignore blanks when they are found as the first letter in a string.
- -d Sort based on only letters, digits, and blanks.
- +POS1, +POS2, +POS3, etc. Sort by any position in a line.
A field in the sort command is what you are sorting by. The fields are separated by spaces. When you use sort –n +1, you are sorting the data by the second field of the name. To combine all of this, if you used the ls –l | sort –n +2, you would create a directory listing and sort the data according to the third character of a string of data. For example, the output of some files might end up being:
Input user data.xml
Input my data.xml
Input user files.xml
Input my files.xml
Input my password.xml
Use my password.xml
Another great use for the pipe is to combine the tail command with a pipe followed by a grep command to search for strings inside of files of a certain name or type. This can be helpful when reviewing logs for web servers. An example of the syntax for this command would be tail –f /var/log/httpd/access_log | grep 404, which would show an administrator all accesses to their websites that resulted in a 404 error page (provided the logs are working).