We need to remember the following standard input and output data/error stream code-
Code | Type |
---|---|
0 | Standard input |
1 | Standard output |
2 | Standard error |
Another thing to note here is, we can either send completely new output or append to the existing content, using the following signs-
Symbol | Output Behavior |
---|---|
> | Completely new output |
>> | Append to existing output |
< | New input stream |
By default, the standard output will show on the terminal. But we can redirect the output to some other source, like a file.
Use the following command structure to redirect input, output, and error-
command 0< input_source 1> output_destination 2> error_destination
BashNote: here “input_source”, “output_destination” and “error_destination” are resources from/to which we want the input/output.
For example, the “date” command shows us date on the terminal-
$ date
Wed Jul 24 01:24:42 +06 2024
BashWe can redirect the output to a file by using “>” sign. Use the follwoign command-
$ date > output.txt
BashNow check the content of file output.txt, you will see the “date” command output written in the file.
$ cat output.txt
Wed Jul 24 01:25:56 +06 2024
BashTry the same thing again-
$ date > output.txt
BashCheck the file again-
$ cat output.txt
Wed Jul 24 01:28:34 +06 2024
BashYou will see the new content replacing the old content.
If you want to append the new content, instead of replacing the old, then use “>>”.
$ date >> output.txt
$ cat output.txt
Wed Jul 24 01:28:34 +06 2024
Wed Jul 24 01:31:08 +06 2024
BashLet’s try another example. In this exmaple we are redirecting the output from “ls” command to the text file.
$ ls >> output.txt
$ cat output.txt
Wed Jul 24 01:28:34 +06 2024
Wed Jul 24 01:31:08 +06 2024
go
output.txt
redis
redis_backup
www
BashOr, you can use the output from “cat” command and write that to some file.
# After this command terminal will wait for intput
# When then input is complete, press CTRL + D to end input
# Or you can press CTRL + C to end the process.
$ cat > test_content.txt
this is first line
second line
abc
d
def ghi
# Check content of the file
$ cat test_content.txt
this is first line
second line
abc
d
def ghi
BashLet’s take input form “test_cotent.txt” and write that to “new_content.txt”
$ cat 0< test_content.txt > new_content.txt
# Or you can use
# cat 0< test_content.txt 1> new_content.txt
Bash“0<” take input and “>” writes the output. Check content of “new_content.txt” it should have the content from “test_content.txt”.
$ cat new_content.txt
this is first line
second line
abc
d
def ghi
Bashif you want to redirect the error to some file then use “2>”.
$ ls -some_wron_option 2> error.log
BashCheck the error saved into the “error.log”.
$ cat error.log
ls: invalid option -- 'e'
Try 'ls --help' for more information.
BashWe can also redirect the output to another terminal.
Open a new terminal and use “tty” command to get the file name of the terminal.
# Execute this in another terminal
$ tty
/dev/pts/1
BashNow from our first terminal we can send the output to the “/dev/pst/1”.
$ cat 0< test_content.txt > /dev/pts/1
BashHere we are taking the content input from “test_content.txt” and sending it to teriminal “/dev/pts/1”.
You should see the content appearing on the second terminal.