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 2,1M 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...