[英]How to find files in Solaris using multiple regular expressions
How to use find
or any other command to find files on Solaris using regular expressions. 如何使用
find
或任何其他命令在Solaris上使用正则表达式查找文件。
I have managed to do it in Mac OSX and Ubuntu, but it seems that Solaris doesn't support -regex
flag. 我已经在Mac OSX和Ubuntu中做到了这一点,但是看来Solaris不支持
-regex
标志。
Example in Mac OSX: Mac OSX中的示例:
find / -type f \( -perm +111 -regex ".*[0-9]$" -o -regex ".*[mh]$" \) -exec ls -lT {} \;
Example in Ubuntu: 在Ubuntu中的示例:
sudo find / -type f \( -executable -regex ".*[0-9]$" -o -regex ".*[mh]$" \) -print0 -exec ls -l --time-style=long-iso {} \;
I need to find all files that match one or multiple regular expressions at the same time in Solaris. 我需要在Solaris中同时查找与一个或多个正则表达式匹配的所有文件。
To be more specific, there are two files that are copied to Solaris machine: 1. script.sh 2. patterns 更具体地说,有两个文件被复制到Solaris计算机:1. script.sh 2.模式
The script reads the patterns file line by line and create a one line find command out of all the regex patterns in the patterns file and execute is at once to list all the files that match the regex patterns. 该脚本逐行读取病毒码文件,并从病毒码文件中的所有正则表达式病毒码中创建一行查找命令,然后立即执行以列出所有与正则表达式病毒码匹配的文件。
Pattern file example: 模式文件示例:
\/usr[a-zA-Z0-9_.\/]+msg_\d+.txt
\/home[\S]+\.txt
.*[0-9]
script intended for above example: 上面示例的脚本:
find / -type f \( -perm +111 -regex "\/usr[a-zA-Z0-9_.\/]+msg_\d+.txt" -o -regex "\/home[\S]+\.txt" -o -regex ".*[0-9]" \) -exec ls -lT {} \;
Thanks in advance :) 提前致谢 :)
If the code you show in your first example is your real code, you don't need full regular expressions, the wildcard patterns supported by standard find
are enough. 如果在第一个示例中显示的代码是真实代码,则不需要完整的正则表达式,则标准
find
支持的通配符模式就足够了。
find / -type f \( -perm +111 -name "*[0-9]" -o -name "*[mh]" \) -exec ls -lT {} \;
Note that this command looks for executable files whose name ends with a digit, and for files with any permission whose name ends with m
or h
. 请注意,此命令查找名称以数字结尾的可执行文件,以及名称以
m
或h
结尾的具有任何权限的文件。 If you meant the permissions to apply regardless of the name, the -perm
condition would have to be outside of the parentheses. 如果您是指不管名称如何都可以应用的权限,则
-perm
条件必须在括号之外。
You can install GNU find on Solaris. 您可以在Solaris上安装GNU find。 It may even be available as
gfind
already. 它甚至可能已经作为
gfind
提供。 It's in the findutils
package . 它在
findutils
包中 。
If you need to stick to the minimal installation and you need full regexps and not just wildcard patterns, you can run ksh to do some additional filtering. 如果您需要坚持最少的安装,并且需要完整的正则表达式而不仅仅是通配符模式,则可以运行ksh进行一些其他过滤。 Use wildcard patterns that match all the file names you want and more, and then use ksh93 and the
=~
conditional operator to refine the matching. 使用匹配所需的所有文件名以及更多文件的通配符模式,然后使用ksh93和
=~
条件运算符来优化匹配。
find … -exec ksh -c 'for x do [[ $x =~ REGEX ]] || continue; ls -lT "$0"; done' _ {} +
Note that these use the extended regular expression syntax (ERE), not the basic regular expression syntax (BRE) which find -regex
uses. 请注意,它们使用扩展的正则表达式语法(ERE),而不是
find -regex
使用的基本正则表达式语法(BRE)。 You can make GNU find use ERE by passing -regextype posix-extended
before -regex
. 您可以通过在
-regex
之前传递-regextype posix-extended
来使GNU查找使用ERE。
Alternatively, if you aren't using the full power of find
(which is rarely needed), use ksh's recursive globbing to match the files, and conditionals to filter permissions and file types. 或者,如果您没有使用
find
的全部功能(很少需要),请使用ksh的递归glob匹配文件,并使用条件过滤权限和文件类型。 Ksh's globs ( @(…|…)
, *(…)
, etc.) have equivalent power to regular expressions, even though the syntax is different. 即使语法不同,Ksh的glob(
@(…|…)
, *(…)
等)也具有与正则表达式相同的功能。
set -o globstar
for x in **/*; do
[[ -f $x && -x $x ]] || continue
ls -lT -- "$x"
done
If it can be assumed that no file name contains a newline then the output of find
can be filtered through another utility, sed
in this case. 如果可以假设没有文件名包含换行符,那么可以通过另一个实用程序(在此情况下为
sed
过滤find
的输出。
find / -type f -perm +111 | sed '/^.*[0-9]$/n;/^.*[mh]$/n;d' |
sed 's/./\\&/' | xargs ls -lT
The first use of sed
prints a line if either regular-expression matches, and the second escapes characters for xargs
ensuring that the line in interpreted literally up to the newline. 如果正则表达式匹配,则第一次使用
sed
打印一行,第二次使用xargs
转义字符,以确保该行的字面含义最多为换行符。
The uses of sed
could be combined into a multi-line script: sed
的用法可以组合成多行脚本:
/^.*[0-9]$/bp
/^.*[mh]$/bp
d
:p
s/./\\&/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.