简体   繁体   English

如何基于正则表达式递归查找当前文件夹和子文件夹中的所有文件

[英]How can I recursively find all files in current and subfolders based on regular expressions

I need to find all files with names matching different regular expressions in directories and subdirectories and print info about them. 我需要在目录和子目录中找到所有名称与不同正则表达式匹配的文件,并打印有关它们的信息。

For example, I have 3 directories: 例如,我有3个目录:

/foo1
/foo1/foo2
/foo1/foo2/foo3

Files in this directories fgg1_1 fgg2_1 fgg3_1 : 该目录中的文件fgg1_1 fgg2_1 fgg3_1

/foo1/fgg1_1
/foo1/foo2/fgg2_1
/foo1/foo2/foo3/fgg3_1

And I need to find all files matching RE 'f.+1$' . 我需要找到所有与RE'f 'f.+1$'匹配的文件。 One might think to use 有人可能会想用

find /foo -regex 'f.+1$' -printf '%f %A@ %s \n'

but shurely it woun't match anything, because -regex evaluates full filenames eg /foo1/foo2/fgg2_1 . 但肯定不会匹配任何内容,因为-regex会评估完整文件名,例如/foo1/foo2/fgg2_1

I tried to grep the output, but it didn't help much since I still need to get info about files I find 我尝试对输出进行grep,但是它并没有太大帮助,因为我仍然需要获取有关我找到的文件的信息

find /foo -print '%f\n' | grep 'f.+1$'

It's important to note, that I'm looking for a somewhat universal solution since this command is executed on the remote host through ssh and compiled using python script, thus paths and RE's for file names can be different every time 重要的是要注意,我正在寻找某种通用的解决方案,因为此命令是通过ssh在远程主机上执行并使用python脚本编译的,因此文件名的路径和RE每次都可能不同

Solution

You'll need to strip all ^ symbols from the beginning of filename's REs 您需要删除文件名RE开头的所有^符号

find /foo -regex '.*\/f.+1$' -printf '%p %f %h %Y %G %U %A@ %C@ %T@ %s\n'

A bit of overkill, but I ended up using this because of how stat displays c_time 有点矫kill过正,但是我最终使用了它,因为stat如何显示c_time

find /foo | grep -e '.*\/f.+1$' | xargs stat --printf '%n %F %g %u %X %Z %Y %s %A\n'

To match whole paths that end in a filename matching a given regular expression, you could prepend .*/ to it, for example .*/f.+1$ . 要匹配以给定正则表达式匹配的文件名结尾的整个路径,可以在其前面加上.*/ ,例如.*/f.+1$ The .*/ should match the path preceding the filename. .*/应该与文件名前面的路径匹配。

if you dont need regex you could try : 如果您不需要正则表达式,可以尝试:

find /foo -name "fgg*" | 查找/ foo -name“ fgg *” | xargs ls -l xargs ls -l

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM