How to Save Terminal Output of a command to a File on a Ubuntu/Linux machine?

You need to redirect the output to a file on a Ubuntu/Linux machine.

The output displayed on the terminal is considered a standard output stdout on the machine, which needs to be redirected to a file.

Syntax

You need to configure a specific command to redirect its output or errors to a file by using the below command.

command > /path/to/filename.txt

Here the command can be any valid Linux command.

  • The command lscpu displays the CPU infrastructure information of the machine.
  • The command lsblk displays information about the list of all available block devices, such as hard drives.

Use the below commands to save the output to a file.

For example, if your current working directory is /var/www and you wish to save the lscpu command's output to the output.txt file, then use the below command.

lscpu > output.txt

Similarly, you can even use an absolute path for the file location as shown below.

lscpu > /var/www/output.txt

In case, if you want to append the output to the file's content, instead of replacing what it already had, then use the below command which has an extra > in the command.

lscpu >> /var/www/output.txt

Use the below commands to save the errors to a file.

For example, if your current working directory is /var/www and you wish to save the lscpu command's errors to the errors.txt file, then use the below command.

lscpu & errors.txt

Similarly, you can even use an absolute path for the file location as shown below.

lscpu & /var/www/errors.txt

Use the below commands to save both the output and errors to the same file.

For example, if your current working directory is /var/www and you wish to save the lscpu command's output and errors to the output.txt file, then use the below command.

lscpu &> output.txt

Similarly, you can even use an absolute path for the file location as shown below.

lscpu &> /var/www/output.txt

In case, if you want to append the information to the file's content, instead of replacing what it already had, then use the below command which has an extra > in the command.

lscpu &>> /var/www/output.txt

Related Links