简体   繁体   中英

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:

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

Files in this directories 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$' . 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 .

I tried to grep the output, but it didn't help much since I still need to get info about files I find

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

Solution

You'll need to strip all ^ symbols from the beginning of filename's REs

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

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$ . The .*/ should match the path preceding the filename.

if you dont need regex you could try :

find /foo -name "fgg*" | xargs ls -l

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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