Unix/Linux command normally takes input from keyboard and display output to
computer screen. something while doing some complex scripting or admin tasks,
need arises to take input from a file or take input from some other command and
output to a file.here is where we resort to command redirection and piping in Linux command.
- Character ">" is used to redirect output of command to a file, if file already exist replace it.
ls -l > Outputfile.txt
To append the output to a existing file we can use “>>”
date >> Outputfile.txt
- Character “<” is used to pass input from a file to a command
grep “myword” < Imputfile.txt
- To redirect standard error to a file using “2>”
Mycommand 2> errorfile.txt
- We can also direct one command’s output to another command by using character “|” (pipe).
ls -l | grep “myword” | sort -r
In the above example firsts requests a “ls
–l” command is being redirected using “|” pipe to “grep myword” command to
search the "myword" from previous command output and ultimately output is
redirected to “sort -r” command to sort the result.
No comments:
Post a Comment