Linux Basics : Part07 : I/O Channels & Redirections

In the last post we saw the file operations, today we will different I/O channels in Linux.

So basically Linux provides 3 I/O channels:

1. Standard Input [STDIN]: This is the channel via which a Linux OS accepts INPUT, by default it is the Keyboard.

2. Standard Output [STDOUT]: This is the channel on which the outputs are displayed, by default it is the Terminal Window.

3. Standard Error [STDERR]: This is the channel on which standard errors are displayed, which is again by default it is the Terminal Window.

Expressions to redirect standard I/O:

The standard Output and the Standard error can be redirected to files using different operators.

1. ">" : Redirects standard output.

2. "2>" : Redirects standard error.

3. "$>" : Redirects all output to file.

Redirection:

  1. Redirecting to file:

    To redirect contents to a file you can use greater than sign ">".

    While redirecting if you use ">" to redirect and if the file exists then all existing contents of the file are overwritten.
    To avoid it make sure to use ">>", so when redirecting contents are appended and not over written.

    It is very important to use the right sign to avoid unwanted loss of data, so be sure before redirecting to an existing file.

  2. Redirecting output to programs or [PIPING]:

    To redirect output of a certain file to a program or another command you can use the pipe symbol "|".

    With this you can redirect output to programs like "less", "more", etc.
    These programs help you read the contents of the files.

    #cat file-a | less

    This command will help you ready the contents of file-a page wise where you can scroll down using down arrow key one line at a time. You can also do "less file-a" to achieve the same result though.

    You may also use "|" to redirect output of one command as an input to another command.

    #command1 | command2

    Here the standard output of "command1" becomes the standard input for "command2"

Viewing Files:

To view files you can either open then to edit or view it without actually opening the file.

To open the file to edit you can use programs like “vi”, “vim”, “nano”, etc.
these programs open the file to edit then and then we can save them, with vi and vim we can go through the file without going to edit mode as well.

To view the files without opening them you can use programs like “cat” or “less”.

To view contents of file-a:

#cat file-a

If the file is too long "cat" command will scroll straight to the end and you will actually have to go back up and start reading, instead you can use the command "less"

#less file-a

This will let you read the file page wise and you scroll down one line at a time.

That’s all for today, hope you liked the post, see you in the next post of blog.avoidingtech.com

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

error: Content is protected !!