How to redirect standard output and error to a file.

by admin July 26, 2018 at 3:06 pm

1. To redirect standard output to a file

This will redirect the content of /etc/issue to a file /tmp/issue.txt

# cat /etc/issue > /tmp/issue.txt

To redirect the output to a file which is already having a content and don’t want to delete it then use “>>” to append as shown below. This will append the motd content to the file issue.txt without overwriting the existing content.

# cat /etc/motd >> /tmp/issue.txt

2. To redirect standard error to a file.

Here, I’m trying to cat the file which doesn’t exists so getting error as shown below.

# cat test.txt
cat: test.txt: No such file or directory

 

To redirect standard errorĀ  to a file

# cat test.txt 2> /tmp/test.txt

# cat /tmp/test.txt 
cat: test.txt: No such file or directory

3. To redirect standard output and standare error to a file.

# cat test.txt > /tmp/test.txt 2>&1

Add Comment