Predictive Hacks

How to Redirect and Save Errors in Unix

bash

In Unix, there are three types of redirection such as:

  • Standard Input (stdin) that is denoted by 0. Usually, it’s the user’s keyboard and we use the “<” sign
  • Standard Output (stdout) that is denoted by 1 and we use the “>” sign
  • Standard Error (stderr) that is denoted by 2
How to Redirect and Save Errors in Unix 1
Coursera
How to Redirect and Save Errors in Unix 2
Coursera

Example of Standard Input

Let’s see how we can store a user input to a text file using the cat command. We start with the cat command the > and the file name that we want to store the user input. In our case, the input file is called myfile.txt. Then, we can start typing the content of the file and when we are done, we can type Ctrl+d to end the cat command.

gpipis:~/project$ cat > myfile.txt
this is the first line
this is the second line
 

Then, we can see the content of the file by typing:

cat < myfile.txt
 

And we get:

this is the first line
this is the second line

Example of Standard Output

In this example, we will show you how to store the output of a command in a text file. Let’s say that we want to store the output of the ls command in the my_ls.txt file. We run the command ls -ltr and then we use the > to redirect the output to the outputs/my_ltr.txt file. Finally, we can see the content of the file by using the cat command. Below, we implement the steps that we described earlier.

gpipis:~/project$ ls -ltr > outputs/my_ltr.txt
gpipis:~/project$ cat outputs/my_ltr.txt 
total 16
-rwxrwxrwx 1 nobody nogroup 1826 Jul  7 12:32 README.md
drwxr-xr-x 4 coder  coder   6144 Oct 31 15:33 lab
-rw-r--r-- 1 coder  coder     47 Oct 31 16:17 myfile.txt
drwxr-xr-x 2 coder  coder   6144 Oct 31 17:30 outputs
 

Example of Standard Error

In this example, we will show you how to store any error you may get by running a command. Let’s say that we want to store the output of the ls -ltr command from a specific path. Let’s list the files from the outputs directory.

gpipis:~/project$ ls -ltr outputs/
total 8
-rw-r--r-- 1 coder coder   2 Oct 31 17:26 number_of_files.txt
-rw-r--r-- 1 coder coder 226 Oct 31 17:30 my_ltr.txt
 

But what happens when the directory does not exist?

gpipis:~/project$ ls -ltr mydir/
ls: cannot access 'mydir/': No such file or directory
 

In this case, we get an error. Let’s see how we can store the error in a file called error.txt. We have to use the 2 which is from the standard error and the > to redirect the output to the error.txt file.

gpipis:~/project$ ls -ltr mydir/ 2> error.txt
gpipis:~/project$ cat error.txt
ls: cannot access 'mydir/': No such file or directory
 

As we can see, we stored the error output to the error.txt file.

Redirect Standard Output and Standard Error to the Same File

If we want to handle both cases where it may find data or may not find data, we can pass in a different redirection. It handles each one, both for output and for error. So we start with the > and the file name, in this case error.txt followed by 2>&1

gpipis:~/project$ ls -ltr outputs/ >  error.txt 2>&1
gpipis:~/project$ cat error.txt
total 8
-rw-r--r-- 1 coder coder   2 Oct 31 17:26 number_of_files.txt
-rw-r--r-- 1 coder coder 226 Oct 31 17:30 my_ltr.txt
gpipis:~/project$ ls -ltr mydir/ >  error.txt 2>&1
gpipis:~/project$ cat error.txt
ls: cannot access 'mydir/': No such file or directory

Note that if you want to append the data to the file, you can use the >> as follows:

gpipis:~/project$ ls -ltr outputs/ >>  error.txt 2>&1
gpipis:~/project$ cat error.txt
ls: cannot access 'mydir/': No such file or directory
ls: cannot access 'mydir/': No such file or directory
total 8
-rw-r--r-- 1 coder coder   2 Oct 31 17:26 number_of_files.txt
-rw-r--r-- 1 coder coder 226 Oct 31 17:30 my_ltr.txt

Redirect Standard Output and Standard Error to the Different Files

If we want to store the output of a command to a specific file, let’s say output.txt when there is no error and the error.txt file when there is an error we can work as follows:

ls -ltr mydir/ > output.txt 2> error.txt
 

Redirect Standard Error to Null or Zero

If we want to send the errors to null or zero we can use the following syntax.

[my command] 2> /dev/null
[my command] 2> /dev/zero

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Leave a Comment

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

snowflake
Uncategorized

Get Started with Python UDFs in Snowflake

Finally, Snowflake supports UDF (user-define functions) in Python. Thank you Snowflake! Apart from Python, we can write UDFs in Java,