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:
command > /path/to/filename.txt
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:
lscpu > output.txt
Or, if you prefer absolute path you can run the following command.
lscpu > /var/www/output.txt
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:
lsblk >> output.txt
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:
lsblk &> output.txt
Similarly, to append the data to output.txt file instead of replacing it, you can use:
lsblk &>> output.txt
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.
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 thelscpu
command to the file namedoutput.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.