Operations 5 min read

Essential Linux Regex Commands for Quick System Queries

A concise collection of Linux command-line one‑liners demonstrates how to extract IPv4 addresses, disk usage, user information, permissions, and pattern matches using regular expressions and tools like ifconfig, df, cat, cut, sort, and egrep.

ITPUB
ITPUB
ITPUB
Essential Linux Regex Commands for Quick System Queries

This guide presents a series of practical Linux one‑liners that leverage regular expressions to retrieve common system information.

1. Extract the IPv4 address from ifconfig

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 default shell of the user with the highest UID

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

4. Show permissions of /tmp

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

5. List all system users with their UID

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

6. Display UID and default shell for specific users (root, linuxmi, mi)

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 login occurrences per host IP for root in last output

(Command omitted in source; image only illustrates the result.)

10. Represent numeric ranges with extended regular expressions

0‑9: egrep "<[0-9]>" 10‑99: egrep "<1[0-9]>" 100‑199: egrep "<1[0-9][0-9]>" 200‑249: egrep "<2[0-4][0-9]>" 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 summary, regular expressions provide a powerful, concise way to filter and extract specific text patterns from Linux command outputs, enabling quick system diagnostics and data collection.

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.

LinuxSystem AdministrationregexShell scriptingcommand-line
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

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.