How to Save Terminal Output of a Command to a File in Ubuntu
Topic: Ubuntu / LinuxPrev|Next
Answer: Redirect the Output to a File
You can use the following syntax to save the terminal output of a command to a file in Ubuntu. It basically redirect the standard output (stdout
) to a file:
For example, if your current working directory is /var/www
and you want to save the output of the lscpu
command to the file named output.txt
inside this directory, you can use:
Or, if you prefer absolute path you can run the following command.
Both commands will have the same effect, because current working directory doesn't matter when you use absolute path. Any path that starts with a forward slash /
is an absolute path.
The lscpu
command display information about the CPU architecture.
Also, if you want to append the data to output.txt
file instead of replacing it, you can use:
The lsblk
lists information about all available block devices such as hard drive.
Moreover, if you want to save errors to the output.txt
file as well, place &
before >
, like this:
Similarly, to append the data to output.txt
file instead of replacing it, you can use:
Tip: Standard error (stderr
) is a stream independent of standard output (stdout
) and can be redirected separately. This allows output and errors to be distinguished.
Related FAQ
Here are some more FAQ related to this topic: