如何删除 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/