6

I have 4 files to rename:

./01:
I0010001  I0020001

./02:
I0010001  I0020001

I want to add auxiliary filename .dcm to each file, so I have tried:

$ mv \(*/*\) \1.dcm
mv: cannot stat '(*/*)': No such file or directory

$ mv \(./*/*\) \1.dcm
mv: cannot stat '(./*/*)': No such file or directory

$ mv \(./\*/\*\) \1.dcm
mv: cannot stat '(./*/*)': No such file or directory

$ mv "\(./*/*\)" "\1.dcm"
mv: cannot stat '\(./*/*\)': No such file or directory

$ mv 0\([1-2]\)/I00\([1-2\)]0001 0\1/I00\20001.dcm
mv: cannot stat '0([1-2])/I00([1-2)]0001': No such file or directory

$ mv "0\([1-2]\)/I00\([1-2\)]0001" "0\1/I00\20001.dcm"
mv: cannot stat '0\([1-2]\)/I00\([1-2\)]0001': No such file or directory

$ mv "0\([1-2]\)/I00\([1-2]\)0001" "0\1/I00\20001.dcm"
mv: cannot stat '0\([1-2]\)/I00\([1-2]\)0001': No such file or directory

$ mv "0\([[:digit:]]\)/I00\([[:digit:]]\)0001" "0\1/I00\20001.dcm"
mv: cannot stat '0\([[:digit:]]\)/I00\([[:digit:]]\)0001': No such file or directory

$ mv "0\([1-2]\)\/I00\([1-2]\)0001" "0\1/I00\20001.dcm"
mv: cannot stat '0\([1-2]\)\/I00\([1-2]\)0001': No such file or directory

$ mv \(*\) \1.dcm
mv: cannot stat '(*)': No such file or directory

None of them yield the result I want.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
Erik Apostol
  • 65
  • 1
  • 1
  • 5
  • 1
    So you know: your issue here is that these are not regexes: a star in bash uses [globbing](https://linux.die.net/man/7/glob). The particular difference here is that there are no capturing groups: the parenthesis are just seen as literals. – Chris Dec 19 '16 at 02:32

1 Answers1

7

You don't really need regular expressions here, this is very simple with a for loop:

for f in 0[12]/I00[12]0001 ; do mv "$f" "${f}.dcm" ; done

For more complex situations, you should be looking into the rename program (prename on some systems), which uses powerful Perl regular expressions to handle the renaming. Though unnecessary here, this simple case would use:

pax> rename -n 's/$/.dcm/' 0[12]/I00[12]0001
rename(I01/I0010001, I01/I0010001.dcm)
rename(I01/I0020001, I01/I0020001.dcm)
rename(I02/I0010001, I02/I0010001.dcm)
rename(I02/I0020001, I02/I0020001.dcm)

That -n is debug mode (print what would happen but don't actually rename). Remove it once you're happy it will do what you want.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 4
    This does not really answers the question. Maybe leaving the troubleshooting as an observation and mainly explaining how to use regex with `mv` would be better – gmelodie Apr 04 '19 at 02:04
  • 1
    @LTKills, while the question *title* asks for a regex way, it is (like so many XY problems) totally unnecessary. The *actual* problem is how to rename files in a specific way. It's no different to if someone asked how to write an accounting package in assembler, an operating system in COBOL, or *anything* in PHP :-) – paxdiablo Apr 04 '19 at 02:52
  • 1
    I agree with @gmelodie . The title "Use of regex in bash with mv" expects an answer related to that specific problem. – Sylvain Jun 26 '19 at 09:29
  • 2
    @Sylvain, if you ask a carpenter how to cut some wood with a hammer, only the most sadistic or deranged ones would suggest anything other than putting down the hammer and using a saw instead :-) The *problem* here is not how to use `mv` (despite what it looks like), it's how to rename files in a certain way. I would suggest the OP also came around to that way of thinking, as they accepted the answer. – paxdiablo Jun 26 '19 at 12:15