bash - 删除 HDFS 中在某个日期范围内创建的所有 0 字节文件

标签 bash shell hadoop

如何删除 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 RegularExpression '. /..(txt\|xml\|csv\|mrc)'
  • -delete: 删除找到的与前面所有选项谓词匹配的文件

关于bash - 删除 HDFS 中在某个日期范围内创建的所有 0 字节文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57530142/

相关文章:

bash - 通过ssh获取nohup进程的PID

linux - 在 Bash 脚本中比较字符串时找不到命令错误 (stderr)

Shell脚本来检查文件是否存在

linux - 从 EC2 同步到 s3 后如何删除文件

hadoop - MapR 客户端不执行 hadoop - Windows

bash - 在 bash 脚本中运行 vi 并执行 vi 命令来编辑另一个文件

Bash: "command not found"简单变量赋值

linux - 如果在 Linux 中找到匹配的字符串,如何跳过期望工作并继续处理

hadoop - java.lang.reflect.InvocationTargetException java.lang.NoClassDefFoundError : com/google/common/io/LimitInputStream 错误

mysql - 如何在配置单元中将时间戳转换为 gmt 格式