Linux I/O Redirection
Standard Input, Output, and Error Overview
|
Name |
Default Destination |
Use in Redirection |
File Descriptor Number |
|---|---|---|---|
|
Computer keyboard |
< (same as 0<) |
0 |
|
|
Computer monitor |
> (same as 1>) |
1 |
|
|
Computer monitor |
2> |
2 |
Common Bash Redirectors
|
Redirector |
Explanation |
|---|---|
|
> (same as 1>) |
Redirects STDOUT. If redirection is to a file, the current contents of that file are overwritten. |
|
>> (same as 1>>) |
Redirects STDOUT in append mode. If output is written to a file, the output is appended to that file. |
|
2> |
Redirects STDERR. |
|
2>&1 |
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 > errout 2>&1. |
|
< (same as 0<) |
Redirects STDIN. |
Exercise 2-2 Using I/O Redirection and Pipes
-
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.
-
Type ls. You’ll see the ls command output onscreen.
-
Type ls > /dev/null. This redirects STDOUT to the null device, with the result that you will not see it.
-
Type ls ilwehgi > /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.
-
Type ls ilwehgi 2> /dev/null. Now you will no longer see the error message.
-
Type ls ilwehgi /etc 2> /dev/null. This shows the contents of the /etc folder while hiding the error message.
-
Type ls ilwehgi /etc 2> /dev/null > 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.
-
Type cat output to show the contents of this file.
-
Type echo hello > output. This overwrites the contents of the output file. Verify this by using cat output again.
-
Type ls >> output. This appends the result of the ls command to the output file. Type cat output to verify.
-
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]).
-
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.
-
Type q to close less. This will also end the ls program.
-
Type ls > /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.
Lab 2.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.
-
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?
-
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.
1
[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
[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
[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
No Comments