Q:

How to Save Terminal Output of a Command to a File in Ubuntu

0

How to Save Terminal Output of a Command to a File in Ubuntu

All Answers

need an explanation for this answer? contact us directly to get an explanation for this 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:

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Ubuntu / Linux Frequently Asked Questions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to Search For Available Packages from the Comm... >>
<< How to Unzip a Zip File from the Terminal in Ubunt...