根据部分原始名称重命名文件

时间:2018-06-28 15:30:39

标签: bash awk

每天我们都会得到一个新的数据库备份转储,例如:

thisfile.0.db2v22.DODE0000.CATN00000.20180627132924.001
thisfile.0.db2v22.DODE0000.CATN00000.20180628132924.001
thisfile.0.db2v22.DODE0000.CATN00000.20180629132924.001

,从该转储开始的日期是文件名的第六个位置,例如:20180627132924

我需要编写一个脚本来删除日期和时间,例如:20180627132924从文件夹中的该文件中插入到恢复脚本中。

如何获取此日期,以便可以将其添加为还原脚本中的变量?

也许是这样的:

OUTPUT="$(ls -l *.001 | awk -F '[_.]' '{print $6}')"
echo " restore $(OUTPUT) to this" >>restore 
chmod 700 restore
./restore

1 个答案:

答案 0 :(得分:2)

如果您建议使用文件名,我建议使用纯Bash内置函数:

$ file="thisfile.0.db2v22.DODE0000.CATN00000.20180627132924.001"
$ fname=${file%.*}
$ fname=${fname##*.}
$ echo $fname
20180627132924
$ echo "This is the file name ${fname} and it has been backup." > outputfile
相关问题