Redirecting program input and output in Linux and Windows
When working with console applications redirecting of program input and output is a must. It is used for piping one program's output to another's input, generating files with output for further examination and so on. Every program has three standard streams - one for reading input data (STDIN), one for output data (STDOUT) and one for outputting errors (STDERR). Here are a few examples how it's done on both Windows (cmd) and Linux (bash) interpreters.
Bash
Redirecting ls output to grep input:
ls -al . | grep -v notneeded
Reading grep input from file.txt
grep word_to_find < file.txt
Redirecting gcc output to out.txt and errors to err.txt
gcc source.c >out.txt 2>err.txt
Redirecting both output and errors to file out.txt
gcc source.c 2>&1 >out.txt
Append both output and errors to file out.txt
gcc source.c 2>&1 >>out.txt
Paging results from find output using more
find / -name myfile* | more
Cmd
Redirecting dir output to sortdir /b log.* | sort
Reading sort input from file.txt
sort < file.txt
Redirecting cl.exe output to out.txt and errors to err.txt
cl.exe file.c >out.txt 2>err.txt
Append both output and errors to out.txt (mind the reversed sign)
cl.exe file.c >>out.txt 2<&1
Paging results
dir /b c:\my_large_dir | more
No comments yet
This page was last modified on 2024-12-05 11:24:48