1.9 KiB
Redirection Examples
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'
[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
[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.
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
The Easiest way