开发者

Change -- to . for all files in a directory

开发者 https://www.devze.com 2022-12-31 20:10 出处:网络
I need to rename all the files in a directory. Some examples of the source filenames are: alpha--sometext.381928

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 "."

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号