【问题标题】:Removing first 3 characters of file names in linux在linux中删除文件名的前3个字符
【发布时间】:2020-05-15 21:13:05
【问题描述】:

我需要一个 sh 脚本来删除文件名的前 3 个字符,例如:

"AB file 1.pdf"
"BC file 2.pdf"
"DB file 3.pdf"
"AD file 4.pdf"
...

到:

"file 1.pdf"
"file 2.pdf"
"file 3.pdf"
"file 4.pdf"
...

我认为脚本会是这样的:

#!/bin/sh
for i in *.pdf; do
   newName= ????
   mv $i $newName
done

【问题讨论】:

  • 我不知道如何在 sh 中做到这一点。在 bash 中(我意识到这是题外话),您可以使用 newName=${i:3}。你会想要做mv "$i" "$newName" 以防止空间做坏事。

标签: linux terminal sh


【解决方案1】:

使用cut 命令:

newName=$(echo "$i" | cut -c4-)

bash 中,您可以使用Parameter Expansion 扩展:

newName=${i:3}

另外,不要忘记引用你的变量:

mv "$i" "$newName"

否则它会认为您正在尝试将名为 ABfile1.pdf 的文件移动到名为 1.pdf 的目录中。

如果您还没有rename 命令,您也可以安装它:

rename 's/^...//' *.pdf

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-17
    • 2012-02-10
    • 2023-01-16
    相关资源
    最近更新 更多