【发布时间】:2013-07-18 15:54:43
【问题描述】:
我有一个文件列表。他们遵循这种模式:_000n_NAME.PNG(文件编号为 n)
在终端(Linux 或 Mac OS)中,是否可以在单个命令中删除我所有文件的“000n”?
【问题讨论】:
-
是否也要删除下划线?这些文件是在同一个目录中还是分散在多个(子)目录中?
标签: bash file shell terminal rename
我有一个文件列表。他们遵循这种模式:_000n_NAME.PNG(文件编号为 n)
在终端(Linux 或 Mac OS)中,是否可以在单个命令中删除我所有文件的“000n”?
【问题讨论】:
标签: bash file shell terminal rename
Sed 可以胜任:
sed 's/^_[0-9]*\(_.*\)$/_\1/'
如果您还想去掉下划线(数字前后):
sed 's/^_[0-9]*_\(.*\)$/\1/'
完整脚本:
while read f; do
mv "$f" $(echo "$f"|sed 's#^.*_[0-9]*_\(.*\)$#\1#')
done < <(find . -name "_0*_NAME.PNG")
【讨论】:
rename 's/000\([0-9]\)/\1/' _000*,但它不起作用。
sed 表达式并工作,然后移至 rename 并没有工作。
你可以使用它:
find . -iname '_000n*.png' | sed -e 's/\(_000n_\(.*.png\)\)/\1 \2/g' | xargs -n 2 mv
使用_000n_toto1.png、_000n_toto2.png 和文件夹中的其他文件进行测试
【讨论】:
sed -e 'p;s/_[0-9]*_//' 或 sed -e 'p;s/_[0-9]*n_//' 但 +1 用于使用 xargs -n2 ;)