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.
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" " -f32. Find the maximum partition usage percentage
df | tr -s " " | cut -d" " -f53. 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 14. Show permissions of /tmp
stat /tmp | head -n 4 | tail -n 1 | cut -c10-135. 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,37. 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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
