Verify group with grep command
In the previous step, we viewed the entire contents of the /etc/group
file. While informative, it's not efficient if you only want to find information about a specific group. This is where the grep
command comes in handy.
grep
is a powerful command-line utility for searching plain-text data sets for lines that match a regular expression. In simpler terms, it helps you find lines in a file that contain a specific word or pattern.
We can combine cat
and grep
using a pipe (|
). The pipe sends the output of the command on the left as input to the command on the right.
To search for the line containing the labex
group in the /etc/group
file, type the following command and press Enter:
cat /etc/group | grep labex
This command first outputs the content of /etc/group
using cat
, and then grep labex
searches that output for lines containing the word "labex".
You should see the line for the labex
group displayed:
labex:x:5000:labex
Now, let's try searching for the sudo
group:
cat /etc/group | grep sudo
This will display the line for the sudo
group:
sudo:x:27:labex
Using grep
with cat
is a common pattern in Linux for filtering output. It allows you to quickly find the specific information you need within large files or command outputs.
You have now learned three ways to get information about groups: whoami
(for your own user's primary group), id
(for your user's groups), getent group
(for specific group details), and grep
on /etc/group
(for searching the group file).
Click Continue to complete this lab.