How to Redirect Output to a File and Stdout in Linux

At some point where you want to save command output into a specific file for any reason, like debugging. In Linux, to save the output of a file, we use stdout. This is also known as stream command.

In computing, the stream is something that transfers data. In our case, it is text data. Using stdout, we can stream and save that data into a text file for future use.

We will guide you on how to save the output of the command directly into a file instead of doing copy-paste.

Save Command Output to a File in Linux

Before proceeding ahead, First, you should know what is Redirection and a combination of symbols?

A redirection symbol directly redirects the command into a file instead of showing output on a terminal. There is a combination of redirection symbols which you can use like “>”,”>>”,”&>”,”&>>”.

Related Read: How to Redirect Output to /dev/null in Linux

In Linux, what we type is called “stdin”, and the output we receive is known as “stdout”. If the output file does not exist in a specific location, it will recreate automatically and save the file.

Make sure if you have used “>”, then the past data will replace it with fresh command output. If you want to redirect both “stdout” and “stderr”, then use “&>”.

Now we will use this redirection symbol to redirect the output into the file.

How to Use “>”, “>>” to Redirect the stdout into a Text File?

When you use “>” redirection symbol, it will redirect command output into a specific file. If you used the same file again to redirect, then the last output will be overwritten.

Redirect Output to File Using “>” Symbol

We will demonstrate it using pwd command and hostnamectl command to show system info with redirect “>” to save output into demofile.txt.

First, we will save the current working directory output to a file.

[root@linuxshelltips:~]# pwd > demofile.txt

Now we will redirect the second output into the file.

[root@linuxshelltips:~]# hostnamectl > demofile.txt

Now view the contents of the file.

[root@linuxshelltips:~]# cat demofile.txt

   Static hostname: linuxshelltips
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 1e2b46dbc0c04b05b592c837c366bb76
           Boot ID: b1cc891c679c41c5a3502be3dca679c6
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 4.20.0-1.el7.elrepo.x86_64
      Architecture: x86-64

Redirect Output to File Using “>>” Symbol

When you use this “>>” redirection symbol, it will redirect command output into a specific file, and ensure that the last saved data should not get a wipe and append new output into the same file.

For Example, we will again use the pwd and hostnamectl command to show system information with a redirect “>>” to save output into demofile.txt.

[root@linuxshelltips:~]# pwd > demofile.txt
[root@linuxshelltips:~]# hostnamectl >> demofile.txt
[root@linuxshelltips:~]# cat demofile.txt

/root
   Static hostname: linuxshelltips
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 1e2b46dbc0c04b05b592c837c366bb76
           Boot ID: b1cc891c679c41c5a3502be3dca679c6
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 4.20.0-1.el7.elrepo.x86_64
      Architecture: x86-64

From the above output, you can see all the past outputs are still available in the same file.

If you have any query feel free to ask in the comment section.

Vivek Gagan
Innovative tech mind with 10 years of experience working as a computer programmer, web developer, and security researcher. Capable of working with a variety of technology and software solutions, and managing databases.

Each tutorial at LinuxShellTips is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

Got something to say? Join the discussion.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.