# Linux I/O Redirection

Standard Input, Output, and Error Overview

<table class="table" id="bkmrk-name-default-destina"><thead><tr><th class="border">Name

</th><th class="border">Default Destination

</th><th class="border">Use in Redirection

</th><th class="border">File Descriptor Number

</th></tr></thead><tbody><tr><td class="border">***<a id="bkmrk-"></a>STDIN***

</td><td class="border">Computer keyboard

</td><td class="border">&lt; (same as 0&lt;)

</td><td class="border">0

</td></tr><tr><td class="border">***<a id="bkmrk--1"></a>STDOUT***

</td><td class="border">Computer monitor

</td><td class="border">&gt; (same as 1&gt;)

</td><td class="border">1

</td></tr><tr><td class="border">***<a id="bkmrk--2"></a>STDERR***

</td><td class="border">Computer monitor

</td><td class="border">2&gt;

</td><td class="border">2

</td></tr></tbody></table>

Common Bash Redirectors

<table class="table" id="bkmrk-redirector-explanati"><thead><tr><th class="border">Redirector

</th><th class="border">Explanation

</th></tr></thead><tbody><tr><td class="border">&gt; (same as 1&gt;)

</td><td class="border">Redirects STDOUT. If redirection is to a file, the current contents of that file are overwritten.

</td></tr><tr><td class="border">&gt;&gt; (same as 1&gt;&gt;)

</td><td class="border">Redirects STDOUT in append mode. If output is written to a file, the output is appended to that file.

</td></tr><tr><td class="border">2&gt;

</td><td class="border">Redirects STDERR.

</td></tr><tr><td class="border">2&gt;&amp;1

</td><td class="border">Redirects STDERR to the same destination as STDOUT. Notice that this has to be used in combination with normal output redirection, as in **ls whuhiu &gt; errout 2&gt;&amp;1**.

</td></tr><tr><td class="border">&lt; (same as 0&lt;)

</td><td class="border">Redirects STDIN.

</td></tr></tbody></table>

Exercise 2-2 Using I/O Redirection and Pipes

<div class="exer-side" id="bkmrk-open-a-shell-as-user"><div class="exer-side">1. Open a shell as user **student** and type **cd** without any arguments. This ensures that the home directory of this user is the current directory while working on this exercise. Type **pwd** to verify this.
2. Type **ls**. You’ll see the **ls** command output onscreen.
3. Type **ls &gt; /dev/null**. This redirects STDOUT to the null device, with the result that you will not see it.
4. Type **ls ilwehgi &gt; /dev/null**. This command shows a “no such file or directory” message onscreen. You see the message because it is not STDOUT, but rather an error message that is written to STDERR.
5. Type **ls ilwehgi 2&gt; /dev/null**. Now you will no longer see the error message.
6. Type **ls ilwehgi /etc 2&gt; /dev/null**. This shows the contents of the /etc folder while hiding the error message.
7. Type **ls ilwehgi /etc 2&gt; /dev/null &gt; output**. In this command, you still write the error message to /dev/null while sending STDOUT to a file with the name output that will be created in your home directory.
8. Type **cat output** to show the contents of this file.
9. Type **echo hello &gt; output**. This overwrites the contents of the output file. Verify this by using **cat output** again.
10. <span id="bkmrk--3"></span>Type **ls &gt;&gt; output**. This appends the result of the **ls** command to the output file. Type **cat output** to verify.
11. Type **ls -R /**. This shows a long list of files and folders scrolling over your computer monitor. (You might want to press Ctrl-C to stop \[or wait some time\]).
12. Type **ls -R /. | less**. This shows the same result, but in the **less** pager, where you can scroll up and down using the arrow keys on your keyboard.
13. Type **q** to close **less**. This will also end the **ls** program.
14. Type **ls &gt; /dev/tty1**. This gives an error message because you are executing the command as an ordinary user, and ordinary users cannot address device files directly (unless you were logged in to tty1). Only the user root has permission to write to device files directly.

</div></div><section id="bkmrk-lab-2.1-modify-your-">### Lab 2.1

1. Modify your shell environment so that on every subshell that is started, a variable is set. The name of the variable should be **COLOR**, and the value should be set to **red**. Verify that it is working.
2. Use the appropriate tools to find the command that you can use to change a user password. Do you need root permissions to use this command?
3. From your home directory, type the command **ls -al wergihl \*** and ensure that errors as well as regular output are redirected to a file with the name /tmp/lsoutput.

</section>1

```bash
[csr@rhel-lab1 ~]$ cat .bashrc 
# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# User specific environment
if ! [[ "$PATH" =~ "$HOME/.local/bin:$HOME/bin:" ]]
then
    PATH="$HOME/.local/bin:$HOME/bin:$PATH"
fi
export PATH

# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=

# User specific aliases and functions
if [ -d ~/.bashrc.d ]; then
        for rc in ~/.bashrc.d/*; do
                if [ -f "$rc" ]; then
                        . "$rc"
                fi
        done
fi

unset rc
COLOR=red
[csr@rhel-lab1 ~]$ echo $COLOR
red
[csr@rhel-lab1 ~]$ 
```

2 yes password needs root access

```bash
[csr@rhel-lab1 ~]$ sudo passwd student
Changing password for user student.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.
[csr@rhel-lab1 ~]$ 


```

3

```bash
[csr@rhel-lab1 tmp]$ sudo ls -la wergihi * > /tmp/lsoutput 2>&1
[csr@rhel-lab1 tmp]$ ls
hosts                                                                        systemd-private-fbeff084055846399d65d269a3a13921-kdump.service-Hve1CF
lsoutput                                                                     systemd-private-fbeff084055846399d65d269a3a13921-ModemManager.service-tvX68Q
systemd-private-fbeff084055846399d65d269a3a13921-chronyd.service-VtzKw9      systemd-private-fbeff084055846399d65d269a3a13921-systemd-logind.service-lMEyTH
systemd-private-fbeff084055846399d65d269a3a13921-dbus-broker.service-mPKNBR
[csr@rhel-lab1 tmp]$ cat lsoutput 
ls: cannot access 'wergihi': No such file or directory
-rw-r--r--. 1 csr  csr  209 Apr  9 17:15 hosts

systemd-private-fbeff084055846399d65d269a3a13921-chronyd.service-VtzKw9:
total 4
drwx------.  3 root root   17 Apr 11 17:23 .
drwxrwxrwt. 11 root root 4096 Apr 11 19:11 ..
drwxrwxrwt.  2 root root    6 Apr 11 17:23 tmp

systemd-private-fbeff084055846399d65d269a3a13921-dbus-broker.service-mPKNBR:
total 4
drwx------.  3 root root   17 Apr 11 17:23 .
drwxrwxrwt. 11 root root 4096 Apr 11 19:11 ..
drwxrwxrwt.  2 root root    6 Apr 11 17:23 tmp

systemd-private-fbeff084055846399d65d269a3a13921-kdump.service-Hve1CF:
total 4
drwx------.  3 root root   17 Apr 11 17:23 .
drwxrwxrwt. 11 root root 4096 Apr 11 19:11 ..
drwxrwxrwt.  2 root root    6 Apr 11 17:24 tmp

systemd-private-fbeff084055846399d65d269a3a13921-ModemManager.service-tvX68Q:
total 4
drwx------.  3 root root   17 Apr 11 17:23 .
drwxrwxrwt. 11 root root 4096 Apr 11 19:11 ..
drwxrwxrwt.  2 root root    6 Apr 11 17:23 tmp

systemd-private-fbeff084055846399d65d269a3a13921-systemd-logind.service-lMEyTH:
total 4
drwx------.  3 root root   17 Apr 11 17:23 .
drwxrwxrwt. 11 root root 4096 Apr 11 19:11 ..
drwxrwxrwt.  2 root root    6 Apr 11 17:23 tmp

```

<div class="exer-side" id="bkmrk--4"></div>