Linux commands need input and display the output. The input may be a file or any other attribute. By default, the input is given by the keyboard, and the output or error is displayed on the system/terminal screen. The keyboard is called the “Standard Input” device, whereas the output and error are called the “Standard Output” and the “Standard Error”. In this article, we will demonstrate the procedure to redirect the stdout/stderr to a bash file in Linux.
- Stdout in Linux
- Redirecting stdout to a File
- Stderr in Linux
- Redirecting stderr to a File
- Redirecting stdout and stderr to a File in Bash
Stdout in Linux
When we run a command on the terminal, three files are created in which the “stdout” is one of them. It stands for “Standard Output”. The file descriptor regarding the “stdout” is “1”. An “Output Redirection” operator is used to perform the output redirection.
Redirecting stdout to a File
The “Output Redirection” operators are used to performing the “stdout” redirection to a file. The very first operator is the “>”. This is used for creating a new file in the system. To understand the concept of standard output redirection, follow the below example.
Example 1: Overwrite Output to a File
Suppose we have a file “file1.txt” that contains some content as shown in the screenshot:
$ cat file1.txt
Now, we want to store the above file content in the new file “file2”. To do so, type the below command on the terminal:
$ cat file1.txt > file2.txt
Here, the output displays that the “file1” content has been stored in “file2”.
Example 2: Append Output to a File
But when we want to store one file’s content in the other existing file, then the “>” is not useful as it removes all the content of the existing file and overwrites the content of the first file.
In that situation, we use the “>>” operator that appends the file content. As it will append the “file2” content, not overwrite, as shown in the below screenshot:
$ cat file1.txt >> file2.txt
Stderr in Linux
The “stderr” error stands for “Standard Error” displayed on the terminal or the screen. The operator that is used for error redirection is “2>”. The file descriptor regarding the “stderr” is “2”. This is used to store the output error in the newly created or existing file. An example below shows how the “stderr” redirects to a file.
Redirecting stderr to a File
Suppose we want to store the output of the file “test” in the new file “file3”. For this purpose, use the command written below:
$ cat test >> file3.txt
The output displays an error that the “test” file/directory is unavailable in the system.
We will use the “stderr redirection” operator to store this error in the file “file3.txt”. Type the below-mentioned command in the terminal and press the enter key:
$ cat test 2> file3.txt
The output represents that the “test” file error has been redirected to the “file3.txt”.
Redirecting stdout and stderr to a File in Bash
In Linux, bash scripting is not a programming language. It is used to automate different tasks and to work on administrative tasks as well. Furthermore, it is also beneficial to run a lot of commands in Linux parallel. This section tells us how bash script can be used to redirect stdout and stderr in a file.
Step 1: Create bash Script
First, create the bash script on the “nano” editor by running the command in the terminal (use Ctrl+Alt+T to open the terminal):
$ nano Script.sh
Step 2: Redirect the stdout and stderr
The output displays the “nano” editor window on the terminal. Type the header file and the below-written command in the editor window:
$ cat Newfile1.txt > NewFile2.txt 2> Error.txt
In the syntax, the “>>” writes one file’s output to another’s input. While the error output is redirected to the standard output. Save the file and press the “Ctrl+X” shortcut key to quit the nano editor.
Step 3: Make the Script Executable
To redirect both the “stdout” and “stderr” in a file, then run the bash script following on the terminal:
The output displays that the permission is denied. To access the permission, use the “chmod” command to make the bash script executable:
$ sudo chmod a+x ./Script.sh
Step 4: Execute the Script
Again, run the bash script “Script.sh” and verify the output using the “cat” utility:
$ ./Script.sh $ cat Error.txt
The output displays that the bash script has been successfully run and denotes the redirection output that the “Newfile1” is not available in the system.
In Linux, bash redirects stdout and stderr to a file by utilizing the ” $ cat filename.txt > filename.txt 2> errorfile.txt ” command. In this command, the “>>” is the “Output Redirection” operator, and “2>” is the “Error Redirection” operator. This command takes the output of the file as the input of another file and redirects it to the other file. All these stdout and stderr operations are briefly explained in this post.