Redirection in Linux I/O| Linux I/O Redirection

Redirection in Linux is a way to change the input or output of a command and it is done using charaacters to specify the source of the data.

In this blog, you will see the Redirection command in Linux. Redirection is mostly used for redirecting the output of one command to another and storing the output and error of the command in the file.

Introduction to Redirection

Redirection in Linux is a way to change the input or output of a command or program. It is done by using special characters to specify the source or destination of the data.

The usage of metacharacters is for redirection.

Streams In I/O Redirection

There are three standard streams in I/O Redirection

A) Standard Input (stdin):

This stream is used for accepting input from the user or from a file. It is numbered as stdin (0).

B) Standard Output (stdout):

This stream is used for displaying output to the user or to a file. It is numbered as stdout (1).

C) Standard Error (stderr):

This stream is used for displaying error messages to the user or to a file. It is numbered as stderr (2).

Redirection into File

Redirection in Linux allows you to control where the standard output and standard error streams are sent.

A) Using the Single Forward Operator

To redirect the output of a command to a file, you can use the > symbol followed by the name of the file. It only adds the content of the file losing your previous content stored in the file.

For example:

Suppose I had a file with the name Student_List.txt in which I have stored all the names of the Student in the class.

Student_List.txt

Alex
Copper
Sam
Travis
Dominic 
Maverick

Now, I have to paste the output of the cat command in the Redirect_Student.txt

$ cat Student_List.txt > Redirect_Student.txt

$ cat Redirect_Student.txt 
Alex
Copper
Sam
Travis
Dominic
Maverick

Here, we can clearly see how we copy and paste the code from one file to another with the help of a single forward operator.

B) Using the Double Forward Operator

To redirect the output of a command to a file, you can also use the >> symbol followed by the name of the file. It appends the content of the file which means it does not lose the content of the file.

For example: In the previous example, we stored the 6 student names, but now I want to add 3 more students’ names without losing my previous student’s name. Here comes the role of >> operator.

$ cat Redirect_Student.txt 
Alex
Copper
Sam
Travis
Dominic
Maverick

$ cat >> Redirect_Student.txt 
Rebel
Madman
Pshyco

$ cat Redirect_Student.txt 
Alex
Copper
Sam
Travis
Dominic
Maverick
Rebel
Madman
Pshyco

Here, you can see we have successfully added 3 more student names without losing my previous content.

Note: While you have to add the new data into the file use > operator, otherwise you want to append the data into your file use >> operator.

C) Redirection into a Program

The pipe operator in Linux (represented by the vertical bar |) is a command-line feature that allows you to take the output of one command and use it as input for another command.

When you use the pipe operator, the output of the command on the left of the pipe is sent as input to the command on the right of the pipe.

Note: While having a similar appearance to the functioning of “>” and “>>,” pipe has a significant distinction. Although brackets are only used to redirect files, pipes redirect data from one program to another.

For Example:

$ ls *.txt | cat > list_files.txt

$ cat list_files.txt 
Country.txt
Redirect_Student.txt
Student_List.txt
Student_marks.txt

Here, the first command will execute which is on the extreme left and then it will move forward to the right side.

Here, on the left side, the ls *.txt command is there, it will list all the files which have an extension of .txt and it will not display on the command terminal and takes that output to the next command is cat > list_files.txt that means it will overwrite the content and write the content of an output of ls *.txt.

Linux Input Redirection

A) Less than Operator (<)

In Linux, the less-than-operator (<) is used for input redirection. It allows you to redirect input from a file or a command to a program.

$ cat < demo.txt 
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday

The above command takes its input from the specified file(demo.txt) instead of the standard input. The contents of the file will be passed as input to the command.

B) EOF (End-of-File) Operator in Linux

In Linux, the <<EOF (also known as a here document) is a special construct used for input redirection. It allows you to provide multiple lines of input directly within a script or command line without the need for an external file

The <<EOF is used to start the here document and EOF serves as the delimiter that indicates the end of the input. You can use any word or string as the delimiter but EOF is commonly used for clarity.

$ cat <<EOF> demo.txt
> April
> May
> June
> EOF

$ cat  demo.txt
April
May
June

Previously, we have stored the week name but now it has changed the content file.

Note: It will override your previous file.

Linux Output Redirection

A) stdout

In Linux, the greater than operator (>) is used for output redirection. It allows you to redirect the output of a command or a program to a file, instead of displaying it on the screen.

$ echo Hello Terminal > file.txt

$ cat file.txt 
Hello Terminal

Here, the output of the left side is executed first then it redirects to the right side command. That’s why it stored the output of the first command and stored in the file.

B) noclobber

The noclobber option helps us avoid file loss while using the ‘>’ sign.

$ echo Hello Terminal > demo.txt

$ cat demo.txt
Hello Terminal

$ set -o noclobber

$ echo New Content adding... > demo.txt 
bash: demo.txt: cannot overwrite existing file

$ set +o noclobber

$ echo New Content adding... > demo.txt 
$ cat demo.txt 
New Content adding...

From the above command, you will conclude that

set -o noclobber is used to prevent overwriting.

set +o noclobber is used for overwriting.

C) >> append

The append ‘>>’ symbol prevents file content from being overwritten, so it displays both new and old file content.

$ cat demo.txt
New Content adding...

$ echo The content is loaded. >> demo.txt 

$ cat demo.txt 
New Content adding...
The content is loaded.

From the above command, we can conclude that our previous content is not overwritten. It is the best way for overwriting the content rather than using the noclobber option.

Linux Error Redirection

A) 2>stderr

The ‘2>’ command redirects an output error. Redirecting error messages, helps us keep our display cleaner.

$ echo "Hello Terminal"
Hello Terminal

$ pd echo "Hello Terminal"
Command 'pd' not found, but can be installed with:
sudo apt install puredata-core

$ pd echo "Hello World" 2> error.txt

$ cat error.txt 
Command 'pd' not found, but can be installed with:
sudo apt install puredata-core

From the above command, we can conclude that if you handle the error message and see your terminal cleaner you should use the 2> option in the command, after the 2> command give the error file in which you have.

B) 2>&1

This command helps us redirect the output and error in the same file.

$ echo Hello World > file.txt and error.txt 2>&1

$ cat file.txt 
Hello World and error.txt

$ pd echo Hello World > file.txt and error.txt 2>&1

$cat file.txt 
Command 'pd' not found, but can be installed with:
sudo apt install puredata-core

Here, is the file.txt holds both the error and output of the command.

FAQ

What is I/O Redirection in Linux?

Redirection in Linux is a way to change the input or output of a command or program. It is done by using special characters to specify the source or destination of the data.
The usage of metacharacters is for redirection.

Recent Articles on Linux

  1. What is Linux Operating System | Introduction to Linux
  2. Directory in Linux Define | Linux Directory & its Commands
  3. Explain the chmod command in Linux | Linux chmod command
  4. Linux User Management || User Management in Linux
  5. Linux Computer Network Advanced Command | Network Command
  1. Continue and Break Statement in Python
  2. Definition of Strings in Python with its Examples
  3. Numbers in Python | Introduction to Numbers in Python
  4. Loop in Python | Different Types of Loop in Python
  5. Sets in Python | Python Sets and Operations performs on them
  6. Conditional Statements in Python (With Examples)
  7. File Handling in Python and Operations performed on File Handling
  8. What are Dictionary in Python | Dictionary in Python, advantages
  9. Variables and Typecasting in Python || Variables in Python

By Vivek Maurya

Write blogs related to Ethical hacking, Computer networks, Linux, Penetration testing and Web3 Security.

Leave a Reply

Your email address will not be published. Required fields are marked *