Linux Command: Redirecting Input/Output Stream Flow

We need to remember the following standard input and output data/error stream code-

CodeType
0Standard input
1Standard output
2Standard error
Linux Command Input/Output
Linux Command Input/Output

Another thing to note here is, we can either send completely new output or append to the existing content, using the following signs-

SymbolOutput 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
Bash

Note: 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
Bash

We can redirect the output to a file by using “>” sign. Use the follwoign command-

$ date > output.txt
Bash

Now 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
Bash

Try the same thing again-

$ date > output.txt
Bash

Check the file again-

$ cat output.txt

Wed Jul 24 01:28:34 +06 2024
Bash

You 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
Bash

Let’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
Bash

Or, 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
Bash

Let’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
Bash

if you want to redirect the error to some file then use “2>”.

$ ls -some_wron_option 2> error.log
Bash

Check the error saved into the “error.log”.

$ cat error.log

ls: invalid option -- 'e'
Try 'ls --help' for more information.
Bash

We 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
Bash

Now from our first terminal we can send the output to the “/dev/pst/1”.

$ cat 0< test_content.txt > /dev/pts/1
Bash

Here 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.

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.