WEB TUTORIALS
PRACTICE EXAMPLES
HTML REFERENCES
CSS REFERENCES
PHP REFERENCES

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:

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.


Related FAQ

Here are some more FAQ related to this topic:

Bootstrap UI Design Templates Property Marvels - A Leading Real Estate Portal for Premium Properties