Exercise I: Redirection
Exercise I: Redirection
Exercise I: Redirection
Most Unix system commands take input from your terminal and send the resulting output back to
your terminal. A command normally reads its input from the standard input, which happens to be
your terminal by default. Similarly, a command normally writes its output to standard output, which
is again your terminal by default.
1. Output Redirection
The output from a command normally intended for standard output can be easily diverted to a file
instead. This capability is known as output redirection.
If the notation > symbol file is appended to any command that normally writes its output to
standard output, the output of that command will be written to file instead of your terminal.
Check the following ls command which redirects the complete output of the command in the
c_lab__files .
$ ls > c_lab_files.txt
Notice that no output appears at the terminal. This is because the output has been redirected
from the default standard output device (the terminal) into the specified file. You can check
the users file for the complete content −
$ cat c_lab_files.txt
If a command has its output redirected to a file and the file already contains some data, that data
will be lost. Consider the following example −
$ ls -l > c_lab_files.txt
$ cat c_lab_files.txt