In the examples on the piping page, we had learned how to combine several commands together. If that were not possible, the only way to complete all 4 tasks would have been to do each one separately.
But that creates a problem: how to save the data from 1 task so it can be used in the next task?
This is where redirection comes in.
There are 3 I/O (Input / Output) streams that are used to manage any input and output a command or script needs or creates:
The numbers are the file descriptor, which refers to an open file. Recall everything is basically a file of some type.
By default STDOUT & STDERR both go to the screen. STDIN is usually provided by the user typing on the keyboards.
Redirecting the output allows us to save the data to a file, or send it to another input stream. This is similar to a pipeline, but a pipeline only works with commands. To send output to a file, we need to redirect it.
Which is exactly what we need to do with our previous exercise involving track times of a CD.
There are 3 redirection features available as well:
Redirecting output to a file is accomplished with the greater than character: > . It looks like a funnel or arrow pointing somewhere else.
Yes, this is the same math symbol used in a comparison, but we are not using it in a mathematical sense. Most (if not all) programming languages allow redirection using this character. They know what it means from the context it is used.
It points to an alternative place for output, which in our case is a file.
So this is another way to create a file, besides touch.
Recall we had a pipeline of 4 commands that resulted in a list of track times. But the list only existed on the screen. Not much use there.
If we saved it to a file, could we use it to do something with? ABSOLUTELY.
To do so, we do something like:
exiftool *.flac | grep Duration | cut -c 35- > tracks.txt
This creates a file tracks.txt in whatever directory you are in, and puts that list of tracks times in it, exactly as shown on the monitor.
If you wanted to create that file somewhere else, you would include the fully qualified path to it:
exiftool *.flac | grep Duration | cut -c 35- > ~/Desktop/tracks.txt
The whole point of getting the track times into a file, is so that we can calculate the total play time of a CD.
/dev/null