bash - 循环将变量分配给文件名的片段,然后在命令中为目录中的每个文件使用
问题描述
我想将文件上传到日期分区的 s3 对象中。我在目录中有很多文件,并且想将每个文件移动到从文件名中的日期派生的文件夹结构中。
for file in /home/ec2-user/clickparts/t*; do year="${file:9:4}"; month="${file:14:2}"; day="${file:17:2}"; aws s3 cp "$file" s3://bk-py-dev/json/clicks/clickpartition/$year/$month/$day/; done
如果我能以这种单行格式做到这一点,那就太棒了。文件名格式为 the_date=2019-03-05.json
解决方案
你已经有了一个很好的解决方案。另一种选择是
topdir="bk-py-dev/json/clicks/clickpartition"
while IFS=- read -r xxxyear month dayjson; do
aws s3 cp "${xxxyear}-${month}-${dayjson}" s3://${topdir}/${xxxyear#*=}/${month}/${dayjson%.*}
done < <(find * -maxdepth 0 -type f -name '*.json' )
我不知道你为什么要把它放在一行中。你可以用你喜欢
的代码添加一个函数,现在你可以调用 oneliner 。.bashrc
upload2s3() {..}
upload2s3
推荐阅读
- typo3 - Site management and application context TYPO3 9
- angular - angular waiting several subscribe to do something
- coldfusion - 使用 Coldfusion 将表单输入传递到外部 URL
- bash - 在 for 循环中嵌套 case 语句
- shell - jq 内置方法 select():找不到命令
- javascript - Passed argument to the child process is undefined
- vba - 在 VBA 中解析 XML 响应并仅提取最后一个数据
- apache - Redirect only HTTP subdomain to HTTPS subdomain in htaccess
- bash - 将参数传递到heredoc
- selenium - 使用 https 的 Codeceptjs 无头测试不起作用