Unix / Linux: Save Output To File

In Unix/Linux, you can easily save the output of a command or script to a file using the “>” or “>>” operator. The “>” operator redirects the standard output of a command to a file, overwriting the file if it already exists. The “>>” operator appends the output to the end of the file, creating the file if it does not exist.

Here are some examples:

  1. Save the output of a command to a file:
$ ls > output.txt

This command will save the output of the “ls” command to a file called “output.txt” in the current directory, overwriting the file if it already exists.

  1. Append the output of a command to a file:
$ date >> log.txt

This command will append the output of the “date” command to a file called “log.txt” in the current directory, creating the file if it does not exist.

  1. Save the output and errors of a command to separate files:
$ mycommand > output.txt 2> errors.txt

This command will save the standard output of “mycommand” to a file called “output.txt” and the standard error output to a file called “errors.txt” in the current directory.

  1. Save the output and errors of a command to the same file:
$ mycommand > output.txt 2>&1

This command will save both the standard output and standard error output of “mycommand” to a file called “output.txt” in the current directory.

By using these operators, you can easily redirect the output of a command or script to a file and save the results for later analysis or use.

Leave a Comment