首页 > 解决方案 > 删除在日期范围之间创建的 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

有什么帮助吗?

标签: bashshellhadoop

解决方案


# 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:删除与所有先前选项谓词匹配的文件

推荐阅读