I need to rename all the files in a directory. Some examples of the source filenames are:
alpha--sometext.381928
comp--moretext.7294058
The resultant files would be renamed as:
alpha.sometext.381928
comp.moretext.7294058
The number of cha开发者_高级运维racters before and after the -- is not consistant.
The script needs to work on current installations of Ubuntu and FreeBSD. These are lean LAMP servers so only the necessary packages have been installed.
Thanks
As an alternative to pure shell, you can use rename
, a standard utility that comes with Perl. It's more convenient for simple cases.
rename 's/--/./' *
Via bash:
for file in *--*; do
mv "${file}" "${file/--/.}"
done
The magic is in ${file/--/.}
which is the value of ${file}
except with each "--" changed to a "."
精彩评论