Now I have a need to replace the middle portion of filenames from their
html code, '&', to 'and'.
e.g. list of filenames
Payables & Receivables
Sales & Marketing
Shipping & Receiving
Normally, I would use something like, for i in '*&*'; do mv "$i"
(this is where I'm stumped); done
TIA!
for i in *;do mv "$i" "${i/\&/} done;
-So basically the for loop runs through every file in that particular
directory.
-Uses the "mv" command to rename files, the second statement is just
search and replace
-The "/\&/" means I want to search the term "&" and replace it
with "something".
-NOTE: the "\" infront of the "&", because you need to escape special
symbols such as "%" "-" etc ...
-After the second "/" in the search and replace term you can put
whatever you want...
eg.) if I wanted to replace "&" with "HELLO" the move command will
be:
mv "$i" "${i/\&/HELLO}"
Hope this helps :)
Chris
Thanks, chutsu!
for $i in *
which I declared a bash variable called "i", and what ever it finds
(filenames), the filename will be assigned to "i", then later on I
used "i/search/replace" to say in variable "i" search and replace....
Hope my explanations where clear!
I use this substitution form in Vim all the time, just never occurred to
me to use it in the commandline like that.
for i in "*\&*"; do echo "${i/\&/and/}"; done
Curiously, there's a forgiving anomaly. The 1st time I left off the last
trailing slash and it worked.
Thanks again, chutsu!
Yes, you were clear. I wasn't.... My sentence was missing a word, so it
didn't make sense to you.
As for the anomaly of the trailing slash, I turned out to be wrong. Your
syntax of leaving off the trailing slash is the only syntax that worked
for me. The substitution syntax of
/<search-string>/<replacement-string/ as used by sed and vim gave errors
until I left off the trailing slash.
For anyone else that needs to rename part of a filename, here's an
example that worked for me--thanks to chutsu's suggestions and help:
#"for i in *\&*" limits the list of filenames passed to the mv
command to ONLY filenames containing '&'.
#
#mv "$i" "${i/\&/and}" replaces '&' with 'and' in each filename
passed to it.
for i in *\&*; do mv "$i" "${i/\&/and}"; done
------------------------------
Where can I find documentation on the search criteria you're using in
the shell?
Thanks again!