Pablo Iranzo Gómez, Pedro Ibáñez Requena, Miguel Pérez Colino, Scott McCarty - Red Hat Enterprise Linux 9 Administration-Packt Publishing (2022) -chap 3 82 - 180
21 We can also use it to count the entries in /etc: [root@rhel-instance ]# ls -m etc | wc -w 199 Pipes, |, are great for reusing the output of one command and sending it to another command to process that output. Now, we know more about using the more common operators to redirect input and output. There are several ways to process that output and we will see more examples in the following section. Filtering output with grep and sed The grep command is heavily used (and commonly mistyped) in system administration. It helps when finding a pattern in a line, whether in a file or via standard input (STDIN). Let’s do a recursive search of the files in /usr with find and put it in /root/usr-files.txt: [root@rhel-instance ]# find /usr/ > /root/usr-files.txt [root@rhel-instance ]# ls -lh usr-files.txt -rw-r--r--. 1 root root M Feb 18 12:38 usr-files.txt As you can see, it’s a 2.1 MB file and it isn’t easy to go through. There is a utility in the system called gzip and we want to know which files in /usr contain the gzip pattern. To do so, we can run the following command: [root@rhel-instance ]# grep gzip usr-files.txt /usr/bin/gzip /usr/lib64/python3.9/__pycache__/gzip.cpython-39.opt-2.pyc /usr/lib64/python3.9/__pycache__/gzip.cpython-39.opt-1.pyc /usr/lib64/python3.9/__pycache__/gzip.cpython-39.pyc /usr/lib64/python3.9/gzip.py /usr/share/licenses/gzip /usr/share/licenses/gzip/COPYING /usr/share/licenses/gzip/fdl-1.3.txt /usr/share/man/man1/gzip.1.gz
Filtering output with grep and sed 81 /usr/share/doc/gzip /usr/share/doc/gzip/AUTHORS /usr/share/doc/gzip/ChangeLog /usr/share/doc/gzip/NEWS /usr/share/doc/gzip/README /usr/share/doc/gzip/THANKS