Pipes in Linux
Pipe is a very important concept in Linux. With the help of Pipes, output from one command can be used as an input as the other command.
For example – Consider a scenario wherein you want to view all files whose name starts with letter s in sorted order. We can accomplish this task using 2 commands as shown below.
- Find files starting with s.
- Sort the list.
Command to do this is –
$find -name ‘s*’ | sort
Explanation – find command generates the list of files matching given conditions. The output of the command is displayed on the standard output i.e console. But in above case, since we have used pipe (|), output of the command is sent as input to the sort command.
Another example – Let us say you want to number each line in the file. Then you can use below command to do it.
cat abc.txt | nl
xargs and pipes
xargs is very handy command to work on multiple files. It is usually used along with pipes.