Lesson 1.2: Use input-output redirection (>, >>, |, 2>, etc.)
Redirection Operators
The Linux command prompt offers users access to a wide range of useful tools, with shells supporting advanced methods for handling their output. By utilizing pipes and redirects, users can manipulate the output of a program, either sending it to a file or another program. Here are some basic tools and their usage:
- > filename → Redirects the standard output to the given file ( overwrites the file if the file already exists )
- >> filename → Appends the standard output at the bottom of the given file ( does not overwrite the content of the file if the file already exists )
- 2> filename → Redirects the standard error to the given file ( overwrites content of the file if the file already exists)
- 2>> filename → Appends the standard error at the bottom of the given file ( does not overwrite content of the file if the file already exists )
- > filename 2>&1 → Redirects both the standard output and standard error to the given file
- < filename → Receives the standard input from the given file
Example 1: Save the details of recently created 4 users into /root/usersinfo file.
[root@client ~]# tail -4 /etc/passwd > userinfo [root@client ~]# cat userinfo sanjeeb:x:1000:1000:Sanjeeb KC:/home/sanjeeb:/bin/bash apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
Example 2: Save the details of those users who use bash shell into /root/usersinfo file. Make sure that the existing content of the file is not lost.
[root@client ~]# grep /bin/bash /etc/passwd >> userinfo [root@client ~]# cat userinfo sanjeeb:x:1000:1000:Sanjeeb KC:/home/sanjeeb:/bin/bash apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin root:x:0:0:root:/root:/bin/bash sanjeeb:x:1000:1000:Sanjeeb KC:/home/sanjeeb:/bin/bash
Example 3: As sanjeeb, run the command ‘find /etc –name group’ and save the error message into /home/sanjeeb/grouperror file.
[sanjeeb@client ~]$ find /etc -name group 2> grouperror /etc/iproute2/group /etc/group [sanjeeb@client ~]$ ls Desktop dfout Documents Downloads grouperror Music Pictures Public scripts Templates Videos [sanjeeb@client ~]$ cat grouperror find: ‘/etc/lvm/devices’: Permission denied find: ‘/etc/lvm/archive’: Permission denied find: ‘/etc/lvm/backup’: Permission denied find: ‘/etc/lvm/cache’: Permission denied find: ‘/etc/pki/rsyslog’: Permission denied find: ‘/etc/sos/cleaner’: Permission denied find: ‘/etc/sssd’: Permission denied find: ‘/etc/nftables’: Permission denied find: ‘/etc/cups/ssl’: Permission denied find: ‘/etc/polkit-1/rules.d’: Permission denied find: ‘/etc/polkit-1/localauthority’: Permission denied find: ‘/etc/grub.d’: Permission denied find: ‘/etc/ssh/sshd_config.d’: Permission denied find: ‘/etc/sudoers.d’: Permission denied find: ‘/etc/audit’: Permission denied find: ‘/etc/firewalld’: Permission denied
Example 4: As sanjeeb, run the command ‘find /etc –name profile’ and ensure that only output is displayed on the screen and the error is lost.
[sanjeeb@client ~]$ find /etc -name profile 2> /dev/null /etc/lvm/profile /etc/profile /etc/dconf/profile
Example 5: Search the pattern ‘ful’ in the file /usr/share/dict/words and save the output into the file /root/ful_out.u
[root@client ~]# grep ful /usr/share/dict/words > /root/ful_out [root@client ~]# cat ful_out abuseful abusefully abusefulness ...
Example 6: Using 2>&1
[sanjeeb@client ~]$ ls folder1 >> test_out 2>&1 [sanjeeb@client ~]$ cat test_out ls: cannot open directory 'folder1': Permission denied ls: cannot open directory 'folder1': Permission denied ls: cannot open directory 'folder1': Permission denied ls: cannot open directory 'folder1': Permission denied ls: cannot open directory 'folder1': Permission denied