我需要在bash脚本中扩展文件之前添加一个数字.例如,我想将文件名名称" abc.efg" 转换为" abc.001.efg.问题是我不知道文件的扩展名是什么(它是脚本的参数之一).
我一直在寻找最快捷的方式.
提前致谢,
fed*_*qui 11
你可以这样做:
extension="${file##*.}" # get the extension
filename="${file%.*}" # get the filename
mv "$file" "${filename}001.${extension}" # rename file by moving it
Run Code Online (Sandbox Code Playgroud)
您可以在bash中提取文件名和扩展名的优秀答案中查看有关这些命令的更多信息.
$ ls hello.*
hello.doc hello.txt
Run Code Online (Sandbox Code Playgroud)
让我们重命名这些文件:
$ for file in hello*; do ext="${file##*.}"; filename="${file%.*}"; mv "$file" "${filename}001.${ext}"; done
Run Code Online (Sandbox Code Playgroud)
Tachan ...
$ ls hello*
hello001.doc hello001.txt
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5326 次 |
| 最近记录: |