Operations 5 min read

Master Linux Regex: 11 Essential Commands to Extract System Information

This tutorial presents a collection of eleven practical Linux commands and regular‑expression patterns that retrieve network details, disk usage, user information, file permissions, and IP address matching, each illustrated with clear examples and output screenshots.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Linux Regex: 11 Essential Commands to Extract System Information

Regular expressions (regex) are powerful patterns used to match and filter text streams, making it easy to extract the exact information you need from command‑line output.

1. Retrieve the IPv4 address from ifconfig output

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

2. Find the maximum partition usage percentage

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

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

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

4. Display the permissions of /tmp

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 (placeholder A8 )

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

7. Use egrep to 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 a path using egrep

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

9. Count how many times each host IP logged in as root (output shown in image)

10. Express IP octet ranges with extended regular expressions

Match a single digit (0‑9): egrep "\<[0-9]\>" Match numbers 10‑99: egrep "\<1[0-9]\>" Match numbers 100‑199: egrep "\<1[0-9][0-9]\>" Match numbers 200‑249: egrep "\<2[0-4][0-9]\>" Match numbers 250‑255:

egrep "\<25[0-5]\>"

11. Show all IPv4 addresses from ifconfig output

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 patterns in text, enabling powerful search, extraction, and validation operations directly from the Linux shell.

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
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

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.