加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

在bash脚本中在文件扩展名之前附加字符串

发布时间:2020-12-15 22:42:59 所属栏目:安全 来源:网络整理
导读:我需要在bash脚本中扩展文件之前添加一个数字.例如,我想将文件名称如“abc.efg”转换为“abc.001.efg.问题是我不知道文件的扩展名是什么(它是参数之一脚本). 我一直在寻找最快捷的方式. 提前致谢, 解决方法 你可以这样做: extension="${file##*.}" # get th
我需要在bash脚本中扩展文件之前添加一个数字.例如,我想将文件名称如“abc.efg”转换为“abc.001.efg.问题是我不知道文件的扩展名是什么(它是参数之一脚本).

我一直在寻找最快捷的方式.

提前致谢,

解决方法

你可以这样做:

extension="${file##*.}"                     # get the extension
filename="${file%.*}"                       # get the filename
mv "$file" "${filename}001.${extension}"    # rename file by moving it

您可以在Extract filename and extension in bash的优秀答案中查看有关这些命令的更多信息.

测试

$ls hello.*
hello.doc  hello.txt

让我们重命名这些文件:

$for file in hello*; do ext="${file##*.}"; filename="${file%.*}"; mv "$file" "${filename}001.${ext}"; done

Tachan …

$ls hello*
hello001.doc  hello001.txt

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读