File Redirection and Encryption in Red Hat Linux
File Redirection and Encryption in Red Hat Linux
# cat >> file2 // it will open file2 to write data and append existing
data
This is my 2nd line of data in this file2
This is my 3rd line of data in this file
File Encryption
Output Redirection: The '>' symbol is used for output (STDOUT) redirection.
Example:
ls -al > listings
Here the output of command ls -al is re-directed to file "listings" instead of your
screen.
Note: Use the correct file name while redirecting command output to a file. If there
is an existing file with the same name, the redirected command will delete the
contents of that file and then it may be overwritten."
If you do not want a file to be overwritten but want to add more content to an
existing file, then you should use '>>' operator.
You can redirect standard output, to not just files, but also devices!
The cat command reads the file music.mp3 and sends the output to /dev/audio
which is the audio device. If the sound configurations in your PC are correct, this
command will play the file music.mp3
Input redirection: The '<' symbol is used for input(STDIN) redirection
Example: The mail program in Linux can help you send emails from the Terminal.
You can type the contents of the email using the standard device keyboard. But if
you want to attach a File to email you can use the input re-direction operator in the
following format.
This would attach the file with the email, and it would be sent to the recipient.
The above examples were simple. Let's look at some advance re-direction
techniques which make use of File Descriptors.
File Descriptors (FD)
In Linux/Unix, everything is a file. Regular file, Directories, and even Devices are
files. Every File has an associated number called File Descriptor (FD).
Your screen also has a File Descriptor. When a program is executed the output is
sent to File Descriptor of the screen, and you see program output on your monitor.
If the output is sent to File Descriptor of the printer, the program output would
have been printed.
Error Redirection
By default, error stream is displayed on the screen. Error redirection is routing the
errors to a file other than the screen.
Why Error Redirection?
Error re-direction is one of the very popular features of Unix/Linux.
Frequent UNIX users will reckon that many commands give you massive amounts
of errors.
For instance, while searching for files, one typically gets permission denied
errors. These errors usually do not help the person searching for a particular
file.
While executing shell scripts, you often do NOT want error messages
cluttering up the normal program output.
The solution is to re-direct the error messages to a file.
Example 1
$ myprogram 2>errorsfile
Using the "find" command, we are searching the "." current directory for a file with
"name" starting with "my"
Server Administrators frequently, list directories and store both error and standard
output into a file, which can be processed later. Here is the command.
ls Documents ABC> dirlist 2>&1
Here,
which writes the output from one file to the input of another file. 2>&1
means that STDERR redirects to the target of STDOUT (which is the file
dirlist)
We are redirecting error output to standard output which in turn is being re-
directed to file dirlist. Hence, both the output is written to file dirlist