3. All about redirection
3.1 Theory and quick reference
There are 3 file descriptors, stdin, stdout and stderr (std=standard).Basically you can:
- redirect stdout to a file
- redirect stderr to a file
- redirect stdout to a stderr
- redirect stderr to a stdout
- redirect stderr and stdout to a file
- redirect stderr and stdout to stdout
- redirect stderr and stdout to stderr
3.2 Sample: stdout 2 file
This will cause the ouput of a program to be written to a file.Here, a file called 'ls-l.txt' will be created and it will contain what you would see on the screen if you type the command 'ls -l' and execute it.
ls -l > ls-l.txt
3.3 Sample: stderr 2 file
This will cause the stderr ouput of a program to be written to a file.Here, a file called 'grep-errors.txt' will be created and it will contain what you would see the stderr portion of the output of the 'grep da *' command.
grep da * 2> grep-errors.txt
3.4 Sample: stdout 2 stderr
This will cause the stderr ouput of a program to be written to the same filedescriptor than stdout.Here, the stdout portion of the command is sent to stderr, you may notice that in differen ways.
grep da * 1>&2
3.5 Sample: stderr 2 stdout
This will cause the stderr ouput of a program to be written to the same filedescriptor than stdout.Here, the stderr portion of the command is sent to stdout, if you pipe to less, you'll see that lines that normally 'dissapear' (as they are written to stderr) are being kept now (because they're on stdout).
grep * 2>&1
3.6 Sample: stderr and stdout 2 file
This will place every output of a program to a file. This is suitable sometimes for cron entries, if you want a command to pass in absolute silence.This (thinking on the cron entry) will delete every file called 'core' in any directory. Notice that you should be pretty sure of what a command is doing if you are going to wipe it's output.
rm -f $(find / -name core) &> /dev/null
No comments:
Post a Comment