如何在Unix Solaris全文件系统中提取含任意邮箱的文件列表
Alright, let's sort this out for your Solaris system. You've been using a command to hunt down files with emails from a specific domain, but now you need to expand that to catch any valid email address instead. Here's how to adjust your approach properly:
First, a quick note on your original command: the regex /*@ isn't valid syntax (you probably meant .*@ to match any characters before the @ symbol). But to match standard emails of any domain, we'll need a more robust regex, plus tweaks to work smoothly with Solaris's tools.
Recommended Solutions
Option 1: Use find with -exec (Most Efficient)
This approach avoids potential issues with filenames containing spaces and is generally more efficient than piping to xargs:
find / -type f -exec grep -lE "[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}" {} + 2>/dev/null
Let's break this down:
find / -type f: Scans the entire root filesystem for regular files (skips directories, symlinks, and special files like device nodes)-exec grep -lE ... {} +: Passes batches of found files togrep(the+means it groups files instead of runninggreponce per file)-l: Makesgrepoutput only the filenames that contain a match, not the matching lines-E: Enables extended regular expressions (ERE) for a cleaner, more readable email pattern- The regex
[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}:[A-Za-z0-9._%+-]+: Matches the local part of the email (before@) — covers all standard valid characters@: The mandatory separator between local and domain parts[A-Za-z0-9.-]+: Matches the main domain name (e.g.,exampleinexample.com)\.[A-Za-z]{2,}: Matches the top-level domain (e.g.,.com,.org,.co.uk) requiring at least 2 characters
2>/dev/null: Suppresses permission-denied errors when accessing restricted system directories like/procor/dev
Option 2: Stick with xargs (If You Prefer)
If you want to keep using xargs, add -print0 to find and -0 to xargs to handle filenames with spaces or special characters:
find / -type f -print0 | xargs -0 grep -lE "[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}" 2>/dev/null
Solaris-Specific Notes
- If your Solaris version doesn't support
grep -E, replace it withegrep(it's the equivalent tool for extended regex on older Solaris systems):find / -type f -exec egrep -l "[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}" {} + 2>/dev/null - If you don't need to scan the entire filesystem, replace
/with your target path (e.g.,/opt/myuser/V9/server/instance1/default/packs*) like you did in your example.
内容的提问来源于stack exchange,提问作者user9439034

