需要将/opt/module/test/路径下的txt文件后缀修改为cpp,并且以年份结尾
代码如下:
#!/bin/bashyear=2020
directory="/opt/module/test/"cd "$directory" || exit 1for name in *.txt; donew_name="${name%.txt}_${year}.cpp"mv "$name" "$new_name"((year++))
done
执行效果:
(2)实现递归修改:把/opt/module/test/所有文件都进行修改
#!/bin/bash
year=2020
directory="/opt/module/test/"find "$directory" -type f -name "*.txt" -print0 | while IFS= read -r -d $'\0' name; donew_name="${name%.txt}_${year}.cpp"mv "$name" "$new_name"((year++))
done