stdin, stdout and stderr
stdin <
stdout 1>
stderr 2>
Redirecting output
$cat file.txt
2
1
4
6
5
$sort file.txt
1
2
4
5
6
$sort file.txt > sortedfile.txt
OR
$sort file.txt 1> sortedfile.txt
$cat sortedfile.txt
1
2
4
5
6
## 1> or > , Overrides the file, sometimes you may not want to override instead you may want to append
$date >> file.txt
$date >> file.txt
$cat file.txt
Mon Jul 4 12:47:13 IST 2022
Mon Jul 4 12:47:15 IST 2022
## tee -a for appending in file instead of overriding .
$echo $SHELL | tee shell.txt
$echo $SHELL | tee -a shell.txt
2
1
4
6
5
$sort file.txt
1
2
4
5
6
$sort file.txt > sortedfile.txt
OR
$sort file.txt 1> sortedfile.txt
$cat sortedfile.txt
1
2
4
5
6
## 1> or > , Overrides the file, sometimes you may not want to override instead you may want to append
$date >> file.txt
$date >> file.txt
$cat file.txt
Mon Jul 4 12:47:13 IST 2022
Mon Jul 4 12:47:15 IST 2022
## tee -a for appending in file instead of overriding .
$echo $SHELL | tee shell.txt
$echo $SHELL | tee -a shell.txt
Redirecting error
$grep -r '^The' /etc/
grep: /etc/cups/ssl: Permission denited
$grep -r '^The' /etc/ 2>/dev/null
It will give clean output
/dev/null is blackhole in the linux file system
### Redirect normal output and error output at the same to 2 different files
## For overwrite
$grep -r '^The' /etc/ 1>output.txt 2>errors.txt
## For appending
grep -r '^The' /etc/ 1>>output.txt 2>>errors.txt
grep: /etc/cups/ssl: Permission denited
$grep -r '^The' /etc/ 2>/dev/null
It will give clean output
/dev/null is blackhole in the linux file system
### Redirect normal output and error output at the same to 2 different files
## For overwrite
$grep -r '^The' /etc/ 1>output.txt 2>errors.txt
## For appending
grep -r '^The' /etc/ 1>>output.txt 2>>errors.txt
Redirecting error as well as normal output to file
## writing errror text as well as normal text to a file
$grep -r '^The' /etc/
grep: /etc/cups/ssl: Permission denied
/etc/sfd/Input/all.txt:The two keys at the left
$grep -r '^The' /etc/ > all_output.txt 2>&1
OR
$grep -r '^The' /etc/ 1>all_output.txt 2>&1
$grep -r '^The' /etc/
grep: /etc/cups/ssl: Permission denied
/etc/sfd/Input/all.txt:The two keys at the left
$grep -r '^The' /etc/ > all_output.txt 2>&1
OR
$grep -r '^The' /etc/ 1>all_output.txt 2>&1
Redirecting Input
$sort file.txt FROM FILE
$sendemail someone@example.com FROM KEYBOARD
$someone@example.com < emailcontent.txt FROM FILE
$sendemail someone@example.com FROM KEYBOARD
$someone@example.com < emailcontent.txt FROM FILE
Heredoc and Here String
$sort <<EOF Here document or heredoc
> 4
> 6
> 3
> 1
> EOF
1
3
4
6
$ bc <<<1+2+3+4 Here string
> 4
> 6
> 3
> 1
> EOF
1
3
4
6
$ bc <<<1+2+3+4 Here string
grep searching, sort for sorting
$grep -v "^#" /etc/login.defs
$grep -v "^#" /etc/login.defs | sort
$grep -v "^#" /etc/login.defs | sort | column -t
$grep -v "^#" /etc/login.defs | sort
$grep -v "^#" /etc/login.defs | sort | column -t


0 Comments