Monday, 8 August 2011

Unix : I/O Redirection

There are three main redirection symbols >,>>,<
1) Linux-command > filename
eg:-

$ ls > myfiles
Now if 'myfiles' file exist in your current directory it will be overwritten without any type of warning.

2) Linux-command >> filename 
 eg:-
$ date >> myfiles 
To output Linux-commands result (output of command or shell script) to END of file. Note that if file exist , it will be opened and new information/data will be written to END of file, without losing previous information/data, And if file is not exist, then new file is created.

3) Linux-command < filename
eg:-
$ cat < myfiles 
To take input to Linux-command from file instead of key-board. For e.g. To take input for cat command give 


$cat > sname
vivek
ashish
zebra
babu
Press CTRL + D to save.

$ sort < sname > sorted_names
$ cat sorted_names
ashish
babu
vivek
zebra

$ tr "[a-z]" "[A-Z]" < sname > cap_names
$ cat cap_names
VIVEK
ASHISH
ZEBRA
BABU

tr command is used to translate all lower case characters to upper-case letters. It take input from sname file, and tr's output is redirected to cap_names file 

For sorting in descending order:-
$ sort -r 


 
 

No comments:

Post a Comment