Skip to main content

两个 rename

wKevin

windows 中有个 rename 命令,linux、macOS 中对应 mv,但其实 linux、macOS 中也有 rename,并且不止一个,语法和入参还不一样,容易让人混淆:

  1. perl rename:
  2. util-linux rename:
perl renameutil-linux rename
安装 in ubuntu默认都有apt i util-linux
安装 in macOSbrew i rename
安装 in Alpineapk add util-linux
man 手册man1man2
用法rename perlexpr [ files ]rename expression replacement file...
foo.md -> bar.mdrename 's/foo/bar/' *.mdrename foo bar *.md
foo*.md -> foo0*.mdrename 's/foo/foo0/' foo*.mdrename foo foo0 foo*.md
_.css -> _.scssrename 's/.css$/.css$/' *.cssrename .css .scss *.css

所以,我以前有一个脚本中的命令:

rename "s/.html/-${theme}.html/" *.html

就可能不再通用,修改为:

for old in *.html; do
new=$(echo $old | sed -e "s/.html/-${theme}.html/")
mv $old $new
done