# Stream Redirection This page has a few examples of some things that can be done using redirection and pipes ## Pipes Pipes are usefull when you need to do something with an programs output In the following command output we may be looking for a file that is starting with 'pas' ```bash [root@gunicorn paste]# ls -alh total 16K drwxr-xr-x. 6 root nginx 158 Feb 20 06:38 . drwxr-xr-x. 4 root nginx 30 Feb 19 09:32 .. drwxr-xr-x. 8 root nginx 163 Feb 19 09:29 .git -rw-r--r--. 1 root nginx 20 Feb 19 09:29 .gitignore -rwxr-xr-x. 1 root nginx 2.4K Feb 19 09:36 paste.py drwxr-xr-x. 2 root nginx 6 Feb 19 09:33 pastes drwxr-xr-x. 2 root nginx 61 Feb 19 09:36 __pycache__ -rw-r--r--. 1 root nginx 0 Feb 19 09:29 README.md -rw-r--r--. 1 root nginx 6 Feb 19 09:29 requirements.txt drwxr-xr-x. 2 root nginx 40 Feb 20 05:23 templates -rw-r--r--. 1 root nginx 77 Feb 19 09:36 wsgi.py ``` We could just look through the output but that can take some time with larger file lists, in this case we can use [`grep`](/Commands/commands#grep) ```bash [root@gunicorn paste]# ls -alh | grep pas -rwxr-xr-x. 1 root nginx 2.4K Feb 19 09:36 paste.py drwxr-xr-x. 2 root nginx 6 Feb 19 09:33 pastes ``` As you can see this has filtered the output of the command and is now just showing the output that matches the 'pas' string provided to grep, something to keep in mind is by default grep matches the entire line Another example of piping is the following command, this will find the message ID of all frozen emails on a mail server running exim and then pass the ID one at a time to `exim -Mrm` which removes the message from the queue. ```bash exim -iz | xargs exim -Mrm ``` ## Redirection 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 `[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