This command basically consists of 2 commands joined by a pipe. The first command gets the listing of the current directory and then pipes it to (sends it to) the second command which is a grep command.
The grep command selects those lines from the directory listing (which it received from the ls command) having the string 'mp3' in them.
As a result of this command, you would get a list of files / directories that have the letters 'mp3' in their names.
You might think this is not really useful, when you could have used some form of ls command itself (probably shorter) to get the some work done. The use of pipes becomes evident as you design more complex commands.
$ ls | grep 'mp3' | sort -r
This command would do the same as the above one, but this time instead of displaying all the files/directories having the string 'mp3' in their names, this result is passed on to the sort command through a pipe. The sort command with the -r option sorts this result in the reverse order. And then finally displays the result.
Thus as you can see, pipes let you pass the output of one command to the input of another command. You can carry on this chain as long as you want (you can use pipes between 5,6,7..commands..how many ever you want) ..and you can get extremely customized outputs. The capabilities of Unix shell when pipes are used effectively is left to your imagination.
Basically pipes can be used with most of the Unix commands. But the basic concept remains the same - the output of the first command acts as the input to the second command. You should take care to check that the output of the first command is acceptable input to the second command.
The first command should not have its output in any format other than text and the second command works with only text input. If this happens, you would mostly get an error message or it would be necessary to type
-C to quit the execution of the command and come back to the prompt.
- A.