Bash Log Output to File

Bash log output to file

Using Redirect Operator

To log the output of a bash command to a file, use the redirect operator.

There are two types of redirect Operator:

  • > (Standard Output Redirect Operator): It redirects the output of a command to a file and overwrites the file’s contents.

  • >> (Standard Output Append Operator): It appends the command’s output to the end of the file without overwriting.

Suppose a scenario in which you want to store the names and emails of students in a log file.

In this example, the student’s name is taken as input and stored in a file called studentLog.txt. Here, > redirected the standard output of the echo command to the studentLog.txt file instead of displaying it on the terminal. On execution of the above command, check the file studentLog.txt is created in your current working directory containing the student’s name.

The > operator will create a file if it does not exist, and if it does exist, the existing contents will be overwritten.

The redirect operator can be helpful when saving the output to the file. It redirects the command output to a file instead of displaying it on the console. This is beneficial for storing information that may be required later or generating reports based on the command output. Output can be anything like maintaining records of student results or student information.

Let’s take some more information from the user and store it in the file.

The student’s email is taken and stored in the email variable in this code snippet. Here, the > operator redirects the standard output of the echo command to the studentLog.txt file, which will overwrite its previous contents (which have the student’s name).

Therefore, after running this code, check the studentLog.txt file; you will see that it now contains the email address instead of the previous content. Note that the file name studentLog.txt is the same as the one generated in the previous example.

If you do not want to overwrite the file’s content, use the >> operator.

In Bash, the append operator >> append the command output to an existing file instead of overwriting it. We can observe the output of the echo command will append at the end of the studentLog.txt file without deleting its existing contents.

Let’s see another example where you want to redirect the list of all the files in the current directory to the myfiles.txt file.

In this example, the ls command output is logged to the myfiles.txt file. Note that only the standard output is redirected to the file. The command will still be displayed on the terminal if it generates any error message. To redirect the standard error to a file as well, you can use the 2> operator instead of the > operator:

In this example, the standard output of the ls command is redirected to filelist.txt, and the standard error is redirected to errorlog.txt. If either file already exists, it will be overwritten; otherwise, it will be created.

Using tee command:

To log the output of a bash command to a file, use the tee command.

In this example, the tee command is used with the echo command to write the output to the file named studentLog2.txt and display it in the terminal window. In Bash, the tee command reads from standard input, writes to standard output on the console window, and writes the same output to a log file. If the given file does not exist, it will be created. The tee command will overwrite its contents if it does exist.

Using the tee command, we can display the output on the terminal and save it to a file simultaneously.

If you want to append the output to an existing file instead of overwriting it use the tee command with the -a parameter :

On the execution of the command, the email john@gmail address taken in the above code is displayed on the terminal window and appended at the end of the studentLog2.txt file without deleting its existing contents.

That’s all about bash log output to file.

Was this post helpful?

Leave a Reply

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