shell 脚本批量处理文件后缀名

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 脚本批量删除文件后缀名

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 \;

执行脚本

shell 脚本批量修改文件后缀名

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李昊哲小课

桃李不言下自成蹊

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值