< Standard output >
Many Linux commands print their output to screen. For example, ls
does this when it lists the contents of a directory: you see the output, the directory listing, on your screen. cat
does the same: it concatenates a file and sends the results to your screen, and thus you can see the file's contents. But the screen isn't the only place where the commands can print their output because you can redirect the output of several commands to files, devices, and even to the input of other commands.
The CLI programs that display their results do so usually by sending the results to standard output, or stdout for short. By default, standard output directs its contents to the screen, as you've seen with the ls
and cat
commands. But if you want to direct the output to somewhere else, you can use the > character. For example, to redirect the output to a file, you can use the > character like this:
$ ls > dir_listing.txt
The above redirects the output of the ls
command to a file called dir_listing.txt
. Because the output is redirected to a file, you don't see any results of ls
on your screen.
Each time you repeat the above command, the file dir_listing.txt
is overwritten with the results of the ls
command. If you want to append the new results to the file instead of rewriting it, you can use >> instead:
$ ls >> dir_listing.txt
Each time you repeat the above command, the new output of ls
is added at the end of the dir_listing.tx
t file instead of overwriting the file.
The following adds the contents of File1
at the end of File2
:
$ cat File1 >> File2
Like I told you before, files aren't the only places where you can redirect the standard output. You can redirect it to devices, too:
$ cat sound.wav > /dev/audio
As you saw, in the above example the cat
command concatenates a file named sound.wav
and the results are sent to a device called /dev/audio
. What's the fun here, then? /dev/audio
is the audio device, and when you send there the contents of a sound file, that file is played. So if your sound is properly configured, the above command plays the file sound.wav
!