shell 脚本批量处理文件后缀名
flumes收集日常完成后会对收集的文件添加
.COMPLETED
后缀名。
我仍然使用原文件名,于是萌生了编写 shell 脚本批量删除文件后缀名的想法。
效果很好,但整个脚本功能太多单一,如果想按需修改文件后缀名呢?
于是编写了 shell 脚本批量修改文件后缀名。
shell 脚本批量删除文件后缀名
批量删除文件后缀名·
.COMPLETED
#!/bin/bash input_dir=$1 if [ -z "$input_dir" ]; then echo "Usage: ./rename.sh input_directory" exit 1 fi cd "$input_dir" || exit for file in *.csv.COMPLETED; do newname=$(echo $file | sed 's/\.COMPLETED//') mv "$file" "$newname" done
执行脚本
shell 脚本批量修改文件后缀名
#!/bin/bash # 检查参数数量是否正确 if [ "$#" -ne 3 ]; then echo "Usage: $0 <path> <old_extension> <new_extension>" exit 1 fi # 提取参数 path=$1 old_extension=$2 new_extension=$3 # 查找并修改文件后缀名 find "$path" -type f -name "*.$old_extension" -exec sh -c 'mv "$0" "${0%.$1}.$2"' {
} $old_extension $new_extension \;
执行脚本
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/bian-cheng-ji-chu/97811.html