Saturday, February 6, 2016

RHEL - Input/Output redirection


Input/Output redirection

1. Redirect output to a file
$ ls -l life is beautiful cool >/var/tmp/ls1.out
ls: cannot access cool: No such file or directory

2. Read the output content
$ cat /var/tmp/ls1.out
-rw-rw-r--. 1 sam sam 0 Feb  6 01:18 beautiful
-rw-rw-r--. 1 sam sam 0 Feb  6 01:18 is
-rw-rw-r--. 1 sam sam 0 Feb  6 01:18 life

3. Redirect the error message to a file
$ ls -l life is beautiful cool 2>/var/tmp/error.out
-rw-rw-r--. 1 sam sam 0 Feb  6 01:18 beautiful
-rw-rw-r--. 1 sam sam 0 Feb  6 01:18 is
-rw-rw-r--. 1 sam sam 0 Feb  6 01:18 life

4. Read the error message
$ cat /var/tmp/error.out
ls: cannot access cool: No such file or directory

5. Redirect output and error message to a same file
$ ls -l life is beautiful cool >/var/tmp/all1.out 2>&1

6. Read the output/error message
$ cat /var/tmp/all1.out
ls: cannot access cool: No such file or directory
-rw-rw-r--. 1 sam sam 0 Feb  6 01:18 beautiful
-rw-rw-r--. 1 sam sam 0 Feb  6 01:18 is
-rw-rw-r--. 1 sam sam 0 Feb  6 01:18 life

7. List user in a file users
$ cat users
ram
sam
sita
gita

8. Input redirect to the cat command
$ cat < users
ram
sam
sita
gita

9. Using pipe
$ cat /etc/passwd -b | grep ravi
    39  ravi:x:1000:1000:ravi:/home/ravi:/bin/bash

10. Here document
$ cat <<COOL
This is test
I am testing
and enjoying
COOL

11. Pipes takes output from one command and seens to second command as an input
$ command | command2
$ cat /etc/passwd | grep sam

No comments:

Post a Comment