Skip to content

Input - Output Redirection

In Linux, you may need to store the output of the commands you run for different purposes. For example, you have analyzed a system and obtained a list of packages that need to be updated and you want to share this list with the relevant teams. Sometimes you may want to give the entire output of your command as input to another program.

In such cases, input - output redirection (IO redirection) will come to your aid.

">" Sign In Linux Shell:

By using the > sign you can redirect the output of your command to a file. The most important point to note here is that this sign deletes existing content.

Let's see an example. First of all, let's redirect a list of processes running on the system to the process.txt file. Then, let's run a different command and observe the change in the file content:

input output redirection in linux
ali@zion:~$ ps aux > process.txt
ali@zion:~$ cat process.txt 
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1  0.2  0.0 168552 11784 ?        Ss   22:04   0:01 /sbin/init splash
root           2  0.0  0.0      0     0 ?        S    22:04   0:00 [kthreadd]
root           3  0.0  0.0      0     0 ?        I<   22:04   0:00 [rcu_gp]
root           4  0.0  0.0      0     0 ?        I<   22:04   0:00 [rcu_par_gp]
root           6  0.0  0.0      0     0 ?        I<   22:04   0:00 [kworker/0:0H-events_highpri]
root           7  0.0  0.0      0     0 ?        I    22:04   0:00 [kworker/0:1-mm_percpu_wq]
root           9  0.0  0.0      0     0 ?        I<   22:04   0:00 [mm_percpu_wq]
root          10  0.0  0.0      0     0 ?        S    22:04   0:00 [rcu_tasks_rude_]
root          11  0.0  0.0      0     0 ?        S    22:04   0:00 [rcu_tasks_trace]
root          12  0.0  0.0      0     0 ?        S    22:04   0:00 [ksoftirqd/0]
root          13  0.1  0.0      0     0 ?        I    22:04   0:00 [rcu_sched]
root          14  0.0  0.0      0     0 ?        S    22:04   0:00 [migration/0]
root          15  0.0  0.0      0     0 ?        S    22:04   0:00 [idle_inject/0]
root          16  0.0  0.0      0     0 ?        S    22:04   0:00 [cpuhp/0]
root          17  0.0  0.0      0     0 ?        S    22:04   0:00 [cpuhp/1]
root          18  0.0  0.0      0     0 ?        S    22:04   0:00 [idle_inject/1]
root          19  0.0  0.0      0     0 ?        S    22:04   0:00 [migration/1]
...........................
ali@zion:~$ echo gnuadm.in > process.txt
ali@zion:~$ cat process.txt 
gnuadm.in

As you can see, redirecting with the > sign deletes the entire contents of the existing file. If the file does not exist, it'll be created.

">>" Sign In Linux Shell:

It behaves quite similar to the > sign. You can still redirect output to a file using >>. Instead of deleting the old content of the file, it appends new data:

io redirection in linux
ali@zion:~$ cat process.txt 
gnuadm.in
ali@zion:~$ echo "Welcome to Linux blog" >> process.txt 
ali@zion:~$ cat process.txt 
gnuadm.in
Welcome to Linux blog

"2>" and "2>>" Signs In Linux Shell:

Same logic with the > and >> signs continues. One deletes the existing file, while the other overwrites it. Differently, we redirect error messages with 2.

In our example, we list 2 files with the ls command. It will throw an error because one of the files is not found. However, we will print this error message in the error.txt file:

redirecting error messages in linux
ali@zion:~$ ls process.txt process2.txt 2> error.txt
process.txt
ali@zion:~$ cat error.txt 
ls: 'process2.txt''e erişilemedi: Böyle bir dosya ya da dizin yok

As you can see, the error output of the ls command is written to the file, not the screen.

"&>" and "&>>" Signs In Linux Shell:

It still works with the same logic. With the & sign, we indicate that we want to see both standard output (stdout) and error messages (stderr).

stdout and stderr redirection in linux
ali@zion:~$ ls process.txt process2.txt &> error.txt
ali@zion:~$ cat error.txt 
ls: 'process2.txt''e erişilemedi: Böyle bir dosya ya da dizin yok
process.txt
ali@zion:~$ ls process.txt process2.txt &>> error.txt
ali@zion:~$ cat error.txt 
ls: 'process2.txt''e erişilemedi: Böyle bir dosya ya da dizin yok
process.txt
ls: 'process2.txt''e erişilemedi: Böyle bir dosya ya da dizin yok
process.txt

Unlike the previous example, the successfully listed process.txt file is also seen in our output.

"<" Sign In Linux Shell:

The file content given at the right of the < sign is sent as input to the command specified at the left.

Let's direct the process.txt file that we prepared in the previous examples as input to the nl command. Of course we don't have to do this to use the nl command. We just make an example:

input redirection in linux
ali@zion:~$ nl < process.txt 
     1  gnuadm.in
     2  Welcome to Linux blog

"<<" Sign In Linux Shell and EOF Statement:

Unlike the previous sign, it redirects the standard input (stdin), not file. EOF stands for end of file. However, you can also use different words instead of EOF. The aim here is to specify the points where the inputs will start and end.

In the following example, we are sending inputs from standard input to the nl command:

standart input redirection in linux
ali@zion:~$ nl << EOF
> line 1
> line 2
> line 3
> line 4
> line 5
> EOF
     1  line 1
     2  line 2
     3  line 3
     4  line 4
     5  line 5

"|" (Pipe) Sign In Linux Shell:

By using the | sign, we can redirect the output of one program to another program.

piping in linux
ali@zion:~$ ps aux | head -4
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1  0.0  0.0 168552 11784 ?        Ss   22:04   0:01 /sbin/init splash
root           2  0.0  0.0      0     0 ?        S    22:04   0:00 [kthreadd]
root           3  0.0  0.0      0     0 ?        I<   22:04   0:00 [rcu_gp]

After listing all processes with the ps aux command, we redirected this output to the head -4 command. In this way, we have listed the first 3 processes (we used 4 because the first line specifies the names of the columns).

Scenarios Where Redirects Can Be Used

This point actually depends on your needs and creativity. For example, let's try importing an alphabetical list of users on the system who using the /bin/false shell into a file with a sequence number at the beginning:

input output redirection in linux
ali@zion:~$ grep /bin/false /etc/passwd | cut -d ":" -f 1 | sort | nl > bin_false_users.txt
ali@zion:~$ cat bin_false_users.txt 
     1  debian-tor
     2  gdm
     3  gnome-initial-setup
     4  hplip
     5  speech-dispatcher
     6  tss
     7  whoopsie