简体   繁体   中英

Match with RegExp in Linux Find Command with iname

I need to do a massive file renaming that are scattered in different folders and subfolders. But I need to only change those files that have a specific value in an specific sequence group. The first sequence will always have a number value but the other sequences may be empty (see row 4 in example)

Example: Change the sequence value in the files that matches '123' with '888' in the second sequence group only.

From this group of files It should only accept the first four filenames

023-123-491-716-11-20170921.txt
215-123-789-014-11-20170312.txt
053-123-123-066-09-20180101.txt
088-123--066-01-20180101.txt
123-345-678-910-08-20160321.txt
966-067-076-322-08-20170312.txt
321-673-123-916-20-20170921.txt
476-976-021-123-20-20170921.txt
987-211-322-677-09-20180101.txt
123-987-123-001-09-20180101.txt
986--123-061-14-20170101.txt
186-563-123--14-20170101.txt    

For this task I will use this command

$ find . -iname 'regexp' -exec rename -v '-123-' '-888-' {} \;

This is a simple valid RegExp for the task "\\d+-123-\\d*-\\d*-\\d*-\\d*.txt" tested in https://regex101.com/ but is not working in find.

With the purpose of testing the regex i only need the find part, but the regexp is not matching the desired results. I tried this expressions without good results

All this shows no results:

$ find . -iname '\d+-123-\d*-\d*-\d*-\d*.txt'
$ find . -iname "[0-9]+-123-*.txt"
$ find . -iname "\([0-9]\)+-123-*.txt"
$ find . -iname "\([0-9]\)*-123-*.txt"
$ find . -iname "([0-9])+-123-*.txt"
$ find . -iname "([0-9])*-123-*.txt"

(0 results)

And this one is showing wrong results because it's not taking into account that the match value has to be exactly in the second sequence group and also ignoring the fourth row example:

$ find . -iname "[0-9]*-123-*.txt"
023-123-491-716-11-20170921.txt
215-123-789-014-11-20170312.txt
053-123-123-066-09-20180101.txt
321-673-123-916-20-20170921.txt
476-976-021-123-20-20170921.txt
123-987-123-001-09-20180101.txt

I've been checking the Emacs Syntax, but is not working either or I'm doing something really wrong. Any help is appreciated.

Thanks

-iname doensn't use regex for searching, it uses glob syntax. If you want to use regex, use -iregex instead.

However, -iregex matches the full path name, not just the file name, and for searches in . that will include a leading ./ .

Also, none of the regextypes supported by find supports the \\d character class, so you'll have to use [0-9] or [[:digit:]] instead.

This should work:

find . -regextype posix-extended -iregex '.*/[0-9]+-123(-[0-9]+){4}\.txt'

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