0

I have multiple files in this directory someDir and I'm trying to move only the ones that have this ending O_010_0000000028181996.xml

I've checked my regular expression online and it matches,the problem is when I run my bash command the files do not get moved to the folder found.

My command bash command is

find /home/someDir/ -regex '\d{10}.{3}O_010_0000000028181996\.xml' -exec mv '{}' /home/found/ \;

Example of filenames in directory someDir:

0208420319-1-O_010_0000000028174248.xml
0208461630-1-O_010_0000000028178356.xml
0208696934-1-O_010_0000000028181996.xml
0208696935-1-O_010_0000000028181996.xml
0208735127-1-O_010_0000000028186468.xml
0208774443-1-O_010_0000000028191308.xml
0208812611-1-O_010_0000000028196104.xml
0208858156-1-O_010_0000000028198984.xml
Kusalananda
  • 320,670
  • 36
  • 633
  • 936
  • You match against the full path with `-regex`, not just the filename. Also, this has _nothing_ to do with `bash`, and everything to do with GNU `find`. – Kusalananda Mar 18 '21 at 09:39
  • The regex needs to match the entire path that `find` sees, including the path you set as argument `-regex '/home/someDir/\d{10}.{3}O_010_0000000028181996\.xml'` or similar depending on whether there are subdirectories – muru Mar 18 '21 at 09:40
  • @muru OK, please explain then why this command gets executed then ```find /home/someDir/ -regex '.*\.xml' -exec mv '{}' /home/found/ \;``` – Cristian Mateica Mar 18 '21 at 09:48
  • `.*` covers the path. – muru Mar 18 '21 at 09:52
  • I've tryed with path but it does not work```find /home/someDir/ -regex '/home/someDir/\d{10}.{3}O_010_0000000028181996\.xml' -exec mv '{}' /home/found/ \;``` – Cristian Mateica Mar 18 '21 at 09:55
  • 2
    What `find` implementation are you using? At least with GNU find, AFAIK none of the built-in regextypes supports the PCRE `\d` for a decimal digit. Probably the closest you'll get is `-regextype egrep -regex '.*/[0-9]{10}.{3}O_010_0000000028181996\.xml'` – steeldriver Mar 18 '21 at 12:11
  • @steeldriver it worked! please aswer the question I have to award you. – Cristian Mateica Mar 18 '21 at 12:32
  • @CristianMateica done - please see below – steeldriver Mar 18 '21 at 12:40

1 Answers1

2

At least with GNU find, you can see what regular expression flavors it supports using

$ find -regextype help
find: Unknown regular expression type ‘help’; valid types are ‘findutils-default’, ‘ed’, ‘emacs’, ‘gnu-awk’, ‘grep’, ‘posix-awk’, ‘awk’, ‘posix-basic’, ‘posix-egrep’, ‘egrep’, ‘posix-extended’, ‘posix-minimal-basic’, ‘sed’.

AFAIK none of these supports the PCRE \d for a decimal digit. Additionally, the findutils-default type that applies when no explicit -regextype is given only supports *, +, and ? as quantifiers.

Probably the closest you will get is to replace \d by [0-9] or [[:digit:]] and use -regextype egrep or -regextype posix-extended ex.

find . -regextype posix-extended -regex '.*/[0-9]{10}.{3}O_010_0000000028181996\.xml'

See also

steeldriver
  • 78,509
  • 12
  • 109
  • 152