bash - Convert timestamp string to UTC date in bash
问题描述
I would like to rename my files with UTC timestamp. Currently those files are names based on local time (CET), as following, where first number if timestamp:
DataExport.test_20181029183500_300.csv.gz
For a reason I can not fix that at file source itself, so I have to perform a change later on.
Currently I extracted number with following:
echo "DataExport.test_20181029183500_300.csv.gz" | cut -d"_" -f2- | cut -c1-14
But now I am stuck here, how to continue.
Thank you and best regards
解决方案
将此检查为您的代码的延续
date -d "$( echo "DataExport.test_20181029183500_300.csv.gz" | cut -d'_' -f2 | sed 's/\(....\)\(..\)\(..\)\(..\)\(..\)\(..\)/\1-\2-\3 \4:\5:\6/g' )" +%s
这将输出:
1540809300
然后,您可以使用该行的输出来执行文件的重命名。但是我建议如果仅仅为此创建一个函数或一个实际的脚本会变得复杂。
这是一个快速的功能来做到这一点:
function rename_ts() {
filename="$1"
time_stamp=$(echo "$filename" | cut -d'_' -f2)
date_fmt=$(echo "$time_stamp" | sed 's/\(....\)\(..\)\(..\)\(..\)\(..\)\(..\)/\1-\2-\3 \4:\5:\6/g')
utc=$(date -d "$date_fmt" +%s)
mv $filename $(echo "$filename" | sed "s/$time_stamp/$utc/g")
}
例如rename_ts DataExport.test_20181029183500_300.csv.gz
推荐阅读
- asp.net-core - 我如何从身份服务器获取特定用户 - aspNetUsers 表依赖于他的 id
- oracle - oracle中的条件数据选择
- oracle - 无法使用 oracle.jd 建立到 jdbc:oracle:thin:@localhost:1521:XE 的连接
- c# - WPF 应用程序未显示 JIT 调试
- google-cloud-platform - GCP中Pass-through型负载均衡器和Proxy型负载均衡器有什么区别
- python - 图像(png、jpg 或 jpeg)被 python 的 PIL / Pillow verify() 检测为损坏
- pycharm - 期望类的实例,而不是类本身错误
- python - 如何使用 sublime 3 创建要在 Python 中读取的 XML 文件?
- c# - 从 mongo db c# driver 2.7.3 识别 COLLSCAN
- php - Laravel 如何在多个页面上包含 Schema.org 结构化数据