0

I have multiple files in a directory:

$ ls -1
04PD.001
04PD.002
04PD.003
04PD.004
04PD.005

Now I'd like to change the name of each file to something like 04PD.7z.0*

Can someone tell me what's wrong with that command:

$ find 04PD* -type f -print -exec mv \{\} `echo \{\} | sed "s/04PD\.0/04PD\.7z\.0/"` \;

The result is utmost strange:

$ find 04PD* -type f -print -exec mv \{\} `echo \{\} | sed "s/04PD\.0/04PD\.7z\.0/"` \;
04PD.001
mv: '04PD.001' and '04PD.001' are the same file
04PD.002
mv: '04PD.002' and '04PD.002' are the same file
04PD.003
mv: '04PD.003' and '04PD.003' are the same file
04PD.004
mv: '04PD.004' and '04PD.004' are the same file
04PD.005
mv: '04PD.005' and '04PD.005' are the same file

To test the command I changed the command to simple echo:

$ find Pop*04PD* -type f -print -exec echo mv \{\} `echo \{\} | sed "s/04PD\.0/04PD\.7z\.0/"` \;
04PD.001
mv 04PD.001 04PD.001
04PD.002
mv 04PD.002 04PD.002
04PD.003
mv 04PD.003 04PD.003
04PD.004
mv 04PD.004 04PD.004
04PD.005
mv 04PD.005 04PD.005

Note: I've already found a very nice & simple solution with a loop, so please focus on what's wrong with this command, rather that proposing another solution.

I am pretty curious where the error is.

5
  • 1
    Change the mv command to echo mv and see what you get
    – roaima
    Jun 4 '20 at 17:00
  • Oh @roaima , you're spoiling all the fun for the others. Of course I did. Jun 4 '20 at 17:05
  • Sensei @muru , thanks. Perhaps I shall add an Q&A answer based on the linked questions? Would you please reopen the question... Jun 4 '20 at 17:25
  • 1
    @Pawel your question is answered by the “Pipelined Sed ...” question, it doesn’t seem particularly useful to add another variant of that as an answer here. Jun 5 '20 at 7:15
  • Hmm, actually I deemed that it would be worthwhile to explain what makes the problem in my case and how to circumvent it, but you have far more experience so perhaps you're right. Jun 5 '20 at 18:00

Browse other questions tagged or ask your own question.