bash - 删除在日期范围之间创建的 HDFS 中的所有 0 字节文件
问题描述
如何删除 HDFS 中某个日期范围的文件。即删除从昨天到今天的 150 天之间创建的 0 字节文件。这是在 shell 脚本中完成的。
我正在使用以下命令删除所有 0 字节文件,但我需要一个可以提供日期范围的文件
hdfs dfs -ls -R $directory/* |grep -Ev "txt|xml|csv|mrc"| awk '$1 !~ /^d/ && $5 == "0" { print $8 }' | xargs -n100 hdfs dfs -rm
有什么帮助吗?
解决方案
# Create reference file with the date of today 00:00:00.000000 am
# as our upper date limit (excluded bound)
# that's equal to all yesterday up to 11:59:59.999999 pm
touch -d 'today' /tmp/before.tmp # before today is yesterday
# Create reference file with the date of 150 days ago as our lower date limit
# that's equal to 150 days ago 00:00:00.000000 am
touch -d '150 days ago' /tmp/after.tmp
# Find and delete files
find \
"$directory" \
-maxdepth 1 \
-type f \
-size 0 \
-anewer /tmp/after.tmp \
-not -anewer /tmp/before.tmp \
-regex '.*/.*\.\(txt\|xml\|csv\|mrc\)' \
-delete
命令分解find
:
"$directory"
: 从变量中找到从这个路径开始$directory
-maxdepth 1
: 限制搜索到这个目录而不降序子目录-type f
:搜索实际文件(无目录,无链接...)-size 0
: 搜索实际大小为 0 的文件-anewer /tmp/after.tmp
:搜索比此参考文件的日期更近访问的文件/tmp/after.tmp
-not -anewer /tmp/before.tmp
: 以及最多访问或在参考文件日期之前访问的内容/tmp/before.tmp
-regex '.*/.*\.\(txt\|xml\|csv\|mrc\)'
: 搜索全名与路径匹配的文件 POSIX 正则表达式 '. /。.(txt\|xml\|csv\|mrc)'-delete
:删除与所有先前选项谓词匹配的文件
推荐阅读
- android - 升级颤振破坏了依赖关系
- c# - 如何将 RedirectToPage 与需要参数的默认处理程序一起使用?
- language-agnostic - 通过字符串 VS 处理计算器输入。算术
- amazon-dynamodb - 无法解析 JSON 文档:无法识别的令牌“$util”:期待(“true”、“false”或“null”)
- bash - 从标准输入读取的两种不同方式
- python - 如何创建向函数发送参数的装饰器
- javascript - 重启节点服务器后NodeJS TypeError
- ruby-on-rails - 使用 2 列之间的差异加速数据库查询:created_at 和 updated_at
- ssis - 如何在 SSIS 中动态获取带有时间戳的文件名
- swift - Swift 中的 Strava API 请求问题