在linux中删除文件名的前3个字符 [英] Removing first 3 characters of file names in linux
本文介绍了在linux中删除文件名的前3个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一个 sh 脚本来删除文件名的前 3 个字符,例如:
I need a sh script to do remove the first 3 characters of file names, for example:
"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"
...
我认为脚本应该是这样的:
I think the script will be like:
#!/bin/sh
for i in *.pdf; do
newName= ????
mv $i $newName
done
推荐答案
使用 cut
命令:
newName=$(echo "$i" | cut -c4-)
在 bash
中,您可以使用 参数扩展扩展:
In bash
you can use a Parameter Expansion extension:
newName=${i:3}
另外,不要忘记引用你的变量:
Also, don't forget to quote your variables:
mv "$i" "$newName"
否则它会认为您试图将名为 AB
、file
和 1.pdf
的文件移动到名为 <代码>1.pdf.
Otherwise it will think you're trying to move the files named AB
, file
, and 1.pdf
to a directory named 1.pdf
.
如果您还没有 rename
命令,也可以安装它:
You could also install the rename
command if you don't already have it:
rename 's/^...//' *.pdf
这篇关于在linux中删除文件名的前3个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文