Updated redirection example

This commit is contained in:
Benjamyn Love 2019-02-26 00:17:45 +11:00
parent 923250a570
commit e0b837bdb7

View File

@ -44,4 +44,57 @@ exim -iz | xargs exim -Mrm
There will be quite a few times when you will want to redirect the output of a command into a file, this could be a temporary file with some data in it that you need to work with
The Easiest way
`[root@gunicorn scratch]# ps aux > ps.out` will save the output of `ps aux` to ps.out, this is useful as we now have a snapshot of what was running and we can run some commands on it without the data changing
You also may need to redirect errors from a command so they do not make a mess of your output that you have, for example you are running a find command on a directory and your user account does not have full permissions for all the directories/files your output will have
```bash
find: /boot/efi/EFI/centos: Permission denied
find: /boot/grub2: Permission denied
find: /proc/tty/driver: Permission denied
find: /proc/1/task/1/fd: Permission denied
find: /proc/1/task/1/fdinfo: Permission denied
find: /proc/1/task/1/ns: Permission denied
```
In this case we can redirect the STDERR out to a special file that nulls all data that is sent to it, the file in this case is `/dev/null`
```bash
[testingacc@gunicorn testing]$ find
.
./dir1
./dir1/file1
./dir1/file2
./dir1/file3
./dir2
find: ./dir2: Permission denied
```
If we run the same command but append `2> /dev/null` it will remove the errors from the output
```bash
[testingacc@gunicorn testing]$ find 2> /dev/null
.
./dir1
./dir1/file1
./dir1/file2
./dir1/file3
./dir2
```
You can also redirect the errors to a file for later use if required, this is done in the same way but you need to swap `/dev/null` with the filename/path you want to save the errors to
```bash
[testingacc@gunicorn testing]$ find 2> error.log
.
./dir1
./dir1/file1
./dir1/file2
./dir1/file3
./dir2
./error.log
[testingacc@gunicorn testing]$ cat error.log
find: ./dir2: Permission denied
```
You can see we now have the error.log file and it contains the 'Permission denied' error and it was not printed to the screen