查找具有可变字符数的文件名
Find filenames with variable number of characters
使用Ubuntu。
我有一个目录,有一些子目录,其中可能有一些名称格式如下的文件:
core.foo.1234
core.bar.65432
因此,它们都以 'core' 开头。然后是进程名称,然后是 PID 号,通常为 4 或 5 位数字。
我的基于 Python 的工具使用两个 'find' 命令来查找它们,如下所示:
the_cmd4 = "find " + directory_name + " -name \"core\.*\.[0-9][0-9][0-9][0-9]\"";
the_cmd5 = "find " + directory_name + " -name \"core\.*\.[0-9][0-9][0-9][0-9][0-9]\"";
所以,这实际上是像这样调用 'find' 工具:
find . -name "core.*.[0-9][0-9][0-9][0-9]"
必须有一种方法来指定 'find' 具有可变数量的数字字符 - 而不是分两步进行。
有吗?
提前致谢。
一个PID是一个16位的值,表示它可以包含1到5个数字。您可以使用 -regex
而不是 -name
:
find FOLDER -regextype sed -regex '.*core\.[0-9]\{1,5\}
我想你可以用 grep
再次过滤,它支持更强大的 RE:
find . -name "core*" | grep "core.*.[0-9]\+"
使用Ubuntu。
我有一个目录,有一些子目录,其中可能有一些名称格式如下的文件:
core.foo.1234
core.bar.65432
因此,它们都以 'core' 开头。然后是进程名称,然后是 PID 号,通常为 4 或 5 位数字。
我的基于 Python 的工具使用两个 'find' 命令来查找它们,如下所示:
the_cmd4 = "find " + directory_name + " -name \"core\.*\.[0-9][0-9][0-9][0-9]\"";
the_cmd5 = "find " + directory_name + " -name \"core\.*\.[0-9][0-9][0-9][0-9][0-9]\"";
所以,这实际上是像这样调用 'find' 工具:
find . -name "core.*.[0-9][0-9][0-9][0-9]"
必须有一种方法来指定 'find' 具有可变数量的数字字符 - 而不是分两步进行。
有吗?
提前致谢。
一个PID是一个16位的值,表示它可以包含1到5个数字。您可以使用 -regex
而不是 -name
:
find FOLDER -regextype sed -regex '.*core\.[0-9]\{1,5\}
我想你可以用 grep
再次过滤,它支持更强大的 RE:
find . -name "core*" | grep "core.*.[0-9]\+"