I was trying to prepend a string to the filename of a bunch of files. I read through several questions and all of them recommended different syntax for basically the same command, only one of which worked.
I'm using cmder as my console. I was trying to prepend crypto-
to all files in the CWD and here's what I tried:
This answer recommended a syntax like:
for i in *.png ; do mv "$i" "crypto-$i" ; done
i was unexpected at this time.
This answer recommended a double percent sign %%i
:
for %%i in *.png ; do mv "$i" "crypto-$i" ; done
%%i was unexpected at this time.
This answer recommended a single percent sign instead of dollar sign for i
:
for %i in *.png ; do mv "$i" "crypto-$i" ; done
*.png was unexpected at this time.
I got further this time and noticed that in the answer, the filename was enclosed in brackets, so I tried:
for %i in (*.png) ; do mv "$i" "crypto-$i" ; done
mv "$i" "crypto-$i" ; done
mv: target 'done' is not a directory
Then I figured that the command should also be in brackets:
for %i in (*.png) ; do (mv "$i" "crypto-$i") ; done
done was unexpected at this time.
After that, I decided to try without ;
:
for %i in (*.png) do (mv "$i" "crypto-$i")
(mv "$i" "crypto-$i" )
mv: cannot stat '$i': No such file or directory
And finally, I thought that since I used %
to refer to i
the first time, I should use %
again later on and tried:
for %i in (*.png) do (mv "%i" "crypto-%i")
This variant worked and all files were renamed successfully.
My question is - why did the last command work and the others didn't? Why are there so many different syntaxes that appear to work only in certain occasions? What are those occasions? Why do you put brackets sometimes and sometimes not? Why do you put semicolons sometimes and sometimes not? What's the difference between $i
, %i
and %%i
?