Operations 5 min read

Master Linux Regex: 11 Essential Commands to Extract System Info

This guide explains how regular expressions can be used on a CentOS 8 system to quickly retrieve network details, disk usage, user information, file permissions, and other key data through concise shell commands and pattern matching techniques.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Linux Regex: 11 Essential Commands to Extract System Info

Regular expressions are patterns used to match each line of input, offering powerful text search capabilities.

1. Find the IPv4 address from the ifconfig output

ifconfig | head -n 2 | tail -n 1 | tr -s " " | cut -d " " -f3

2. Get the maximum partition usage percentage

df | tr -s " " | cut -d " " -f5

3. Retrieve the username, UID, and shell of the user with the highest UID

cat /etc/passwd | cut -d: -f1,3,7 | sort -n -t: -k2 | tail -n 1

4. Show the permissions of the /tmp directory

stat /tmp | head -n 4 | tail -n 1 | cut -c10-13

5. List all system users with their usernames and UIDs

cat /etc/passwd | cut -d: -f1,3 | egrep -v "[0-9]{4,}"

6. Show UID and default shell for users root, linuxmi, and mi (A8 as placeholder)

cat /etc/passwd | egrep "^(root|A8)" | cut -d: -f1,3

7. Extract lines ending with a lowercase letter from /etc/rc.d/init.d/functions

echo /etc/rc.d/init.d/functions | egrep "[a-z]$"

8. Extract the directory name from the same path using egrep

echo /etc/rc.d/init.d/functions | egrep "/.*/"

9. Count how many times each host IP logged in as root via the last command

10. Use extended regular expressions to match numeric ranges (0‑9, 10‑99, 100‑199, 200‑249, 250‑255)

echo {1..255} | egrep "\<[0-9]\>"
egrep "\<1[0-9]\>"
egrep "\<1[0-9][0-9]\>"
egrep "\<2[0-4][0-9]\>"
egrep "\<25[0-5]\>"

11. Display all IPv4 addresses from the ifconfig command

ifconfig | egrep "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"

In short, regular expressions provide a concise way to describe and process text patterns on Linux systems.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Linuxregex
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.