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

猜你喜欢

转载自blog.csdn.net/qq_24330181/article/details/134750493
今日推荐