0

Change the name of a file

I want to understand how to change the name of a file with the terminal and a Mac. I have been searching and I found many solutions. For example here: Using sed to mass rename files

This changes a file from F00001-070 to F000-070.

for file in *.txt; do echo mv -v "$file" "${file/#F0000/F000}"; done

I have tested and it works well. But now I try to change that from old-file.txt
to new-file.txt

for file in *.txt; do echo mv -v "$file" "${file/#old/new}”; done

That last one does not work.

I thought that file/#F0000/F000 mean change files beginning F0000 to F000. I must be wrong?

Can someone explain the code and apply to how to change part of the name of a file at the begin, end or in the middle

Community
  • 1
  • 1
segon
  • 273
  • 1
  • 4
  • 16
  • Aside from using the wrong closing quote (Unicode right double quote `”` instead of ASCII straight double quote `"` ), your second example is fine. Always report the *problem* with your code; don't just say "it doesn't work". – chepner Jan 24 '17 at 15:13
  • @chepner your answer is the right one. The only problem was the quote. You should put that as an answer. I will accept it – segon Jan 24 '17 at 15:46
  • 1
    Adding an answer for now; the question should probably just be closed as off-topic (type/cannot reproduce) though. – chepner Jan 24 '17 at 15:56

2 Answers2

1

Your closing quote was the wrong kind, Unicode right quote instead of ASCII straight quote:

# bad
for file in *.txt; do echo mv -v "$file" "${file/#old/new}”; done
                                                          ^
# good
for file in *.txt; do echo mv -v "$file" "${file/#old/new}"; done
                                                          ^
chepner
  • 497,756
  • 71
  • 530
  • 681
  • right for this sample but i guess this is a bad rewrite and not original code. In case of bad ending quotation, the code will not be interpreted waiting for a closing quote with an error like `unexpected EOF while looking for matching \`"'` – NeronLeVelu Jan 25 '17 at 05:57
  • @NeronLeVelu I only posted this answer because the OP literally said this was the problem and requested it. – chepner Jan 25 '17 at 12:31
  • I read it like this (comment say it's right), my comment was just to say that this was not the real problem (my interpreation) but more the file name itself because OP don't write anything about an error message, just that result is not what expected. – NeronLeVelu Jan 25 '17 at 13:29
0
for file in *.txt; do echo mv -v "$file" "${file/old/new}”; done

the # mean until where a % mean after in a regex like behaviour, so remove your #

# var=old-file.txt;echo ${var/%file*/new}
old-new
# var=old-file.txt;echo ${var/#*file/new}
new.txt

%% and ## mean the longest possible expression where # and % the first occurence

NeronLeVelu
  • 9,908
  • 1
  • 23
  • 43
  • Your version works well, thank you. But I still do not understand. What does # mean, when can I apply it? – segon Jan 24 '17 at 14:58
  • # is like % an expression to define how to treat the seach form replacement. # mean everything *from begin until end of pattern* corresponding to following pattern included so `#F00` mean content *starting by F00* where `#*F00` mean *from beginning until F00*, `%` do the same from ther other side of the content – NeronLeVelu Jan 24 '17 at 15:11
  • There is nothing wrong with the OP's parameter expansion; his understanding is correct. It's the quoting that is wrong. – chepner Jan 24 '17 at 15:14
  • @NeronLeVelu if #F00 mean starting by F00, why #old does not work? – segon Jan 24 '17 at 15:19
  • @segon it works if the content begin with *old* `var=old-file.txt;echo ${var/#old/new}` -> `new-file.txt` – NeronLeVelu Jan 25 '17 at 05:50