Saturday, April 11, 2009

Input-Output redirection on linux

Whenever you issue "ls" command on the console and press enter you see it's output is on your console. Sometimes you may wish to redirect the output to your file. With the ">" command you can send output to an OS file. Several redirector symbol along with examples are discussed below.

1)< Redirector symbol:
Usage syntax is
linux_commnads< filename
The output of linux_commands will be written to file filename.

Note that, if the file does not exist in the location, a new file will be created and if the file already exist in the location it will be overwritten.

Example:
# ls>ls_output.txt

# ls
command.sh ls_output.txt quote.sh test_echo.bash test_user.txt t_sep.sh
echo.sh practice read.sh test_echo.sh test_v.sh

# cat ls_output.txt
command.sh
echo.sh
ls_output.txt
practice
quote.sh
read.sh
test_echo.bash
test_echo.sh
test_user.txt
test_v.sh
t_sep.sh

Be cautious of the filename when redirecting output to a file. If filename already exist it will be overwritten.

2)>> Redirector Symbol:
>> redirector symbol works just like > but in this case if file already exist, data will be appended to the end of the file. If file is not there, then as of > symbol, >> symbol also create a new file and write data into it.
Example:
Let's see the contents inside ls_output.txt file.
# cat ls_output.txt
command.sh
echo.sh
ls_output.txt
practice
quote.sh
read.sh
test_echo.bash
test_echo.sh
test_user.txt
test_v.sh
t_sep.sh

Append output of ls -a to the end of the file ls_output.txt
# ls -a>>ls_output.txt

Let's see what ls -a do. It does not ignore directory contents with . enties.
# ls -a
. command.sh ls_output.txt quote.sh test_echo.bash test_user.txt t_sep.sh
.. echo.sh practice read.sh test_echo.sh test_v.sh

Let's see contents of ls_output.txt and we see output of ls -a is appended to the end.

# cat ls_output.txt
command.sh
echo.sh
ls_output.txt
practice
quote.sh
read.sh
test_echo.bash
test_echo.sh
test_user.txt
test_v.sh
t_sep.sh
.
..
command.sh
echo.sh
ls_output.txt
practice
quote.sh
read.sh
test_echo.bash
test_echo.sh
test_user.txt
test_v.sh
t_sep.sh

3)< Redirector Symbol
> redirector symbol is used to direct output to a file and < redirector is used to read contents from a file. In order to make it clear let's create a file named names.txt and below is it contents.
# cat names.txt
momin
arju
bony
tany
azmeri
queen
tasreen

Now my target is to sort the names inside names.txt and save the sorted order into another file named names_sorted.txt
# sort <names.txt>names_sorted.txt
By above command the sorted output will be stored into names_sorted.txt. If we would only type the first part sort<names.txt then it would simply sort the names inside txt and display to console as below.
#sort<names.txt

arju
azmeri
bony
momin
queen
tany
tasreen

with > redirection we would not let it happen to display on console rather redirect output to a file named names_sorted.txt.


# cat names_sorted.txt


arju
azmeri
bony
momin
queen
tany
tasreen

Similarly if you want to make all letters inside the file names_sorted.txt into uppercase then we can do it with tr command (which will translates all lowercase letter into uppercase). Below is such an example.

# tr "[a-z]" "[A-Z]" <names_sorted.txt>names_uppercase.txt
# cat names_uppercase.txt


ARJU
AZMERI
BONY
MOMIN
QUEEN
TANY
TASREEN

Important Tips
Note that, each program you run on linux (line ls, rm or anything) access to three important files. You can add parameter(1 or 2 or 3) in order to get information about particular things while running any command.

These files are standard input, standard output and standard error file. And each of the files (standard input, output and error) have the file descriptors 0, 1 and 2 respectively. With while you do command you can pass 1 or 2 or 3 in order to get information of certain type information.

0=Standard Input
1=Standard Output
2=Standard Error


An example will make you clear.
Whenever you issue ls b.txt it says no file on the console.
# ls b.txt
ls: b.txt: No such file or directory

Whenever you add 1 with ls command and redirect output then in the output file there is nothing as no expected result came rather than an error generated.
# ls b.txt 1>output_b.txt
ls: b.txt: No such file or directory

# cat output_b.txt

However if we want to catch the error then we have to add 2 with the command as below.
# ls b.txt 2>output_b.txt

# cat output_b.txt
ls: b.txt: No such file or directory

Related Documents

http://arjudba.blogspot.com/2009/04/arithmatic-operation-and-expression-in.html

http://arjudba.blogspot.com/2009/04/system-notes-org-how-to-copy-and-paste.html

No comments:

Post a Comment