Shell 文件名后添加日期做备份

Shell 文件名后添加日期做备份,第1张

${filename//./"$BKUP_DATE."}

改为:

${filename/.dat/$BKUP_DATE.dat}

连带着后缀名一起替换就不会有问题了。前提是,你的后缀名一定是dat

1、假设你有一堆文件要改名列表

2、例如

#!/bin/sh

cd ./sh

ymd=`date +%Y%m%d`

max=`find . -type f -name '*.sh'|wc|awk '{print $1}'`

x=(`seq -f %02g 1 ${max}`)

i=0

for file in `ls *.sh`do

file1=$(echo $file|awk -F. '{print $1}')

newfile=$file1-$ymd-${x[$i]}.sh

echo $file $newfile

/bin/mv $file $newfile

((i++))

done

3、执行

199.sh 199-20180206-01.sh

204.sh 204-20180206-02.sh

27.sh 27-20180206-03.sh

28.sh 28-20180206-04.sh

76.sh 76-20180206-05.sh

#!/bin/bash

read -p "please enter the date: " date

if [[ $date =~ "[0-9]{2}/[0-9]{2}/[0-9]{4}" ]]

then

echo "today is:${date:0:2} month is:${date:3:2} year is:${date:6:4}"

else

echo "wrong date"

fi

我没有对日期范围做校验,你可以自己加一下。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11521413.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-16
下一篇 2023-05-16

发表评论

登录后才能评论

评论列表(0条)

保存