Search The Site

Linux Standard Output (stdout) Explained

Many Linux command line commands print output to a screen. For example, when you run the ls command on a directory it will output the directory contents to the screen. We can concatenate a file and send the results to a screen by using the cat command, this will allow us to see the files contents. The screen isn't the only option commands have of where to print their output. You can redirect the output of several commands to files, devices, and even to the input of other commands.

The command line interface (CLI) programs that display their results can 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 > my_file.txt

The above example I have provided will redirect the output of the ls command to a file called my_file.txt. Because the output is being redirected to a file called my_file.txt, you will not see any results of ls on your monitor.

Now, keep in mind that each time we repeat the above command, the file my_file.txt is overwritten with the results of the ls command. If you want to append the new results to the file instead of overwriting it, you can use >> as seen in the example below:

ls >> my_file.txt

Now every time we repeat the above command, instead of overwriting my_file.txt the new output of the ls command will append the data to the end of my_file.txt.

The following command will add the contents of my_file.txt to the bottom of my_file2.txt:

cat my_file.txt >> my_file2.txt

As mentioned earlier, files are not the only place that you can redirect standard output. You can redirect stdout to devices also. for example you can redirect the audio from a file called mysong.wav to your audio device like this:

cat mysong.wav > /dev/audio

Any questions?. cheers :)