HDFS判断文件是否存在

可以用hadoop fs -test来判断文件是否存在。

官方文档解释:Apache Hadoop 3.4.0 – Overview(搜索 -test 就能找到)

Usage: hadoop fs -test -[defswrz] URI

Options:

  • -d: if the path is a directory, return 0.
  • -e: if the path exists, return 0.
  • -f: if the path is a file, return 0.
  • -s: if the path is not empty, return 0.
  • -w: if the path exists and write permission is granted, return 0.
  • -r: if the path exists and read permission is granted, return 0.
  • -z: if the file is zero length, return 0.

Example:

  • hadoop fs -test -e filename

参数说明:

        -d 判断是否是目录,是则返回0;

        -e 判断文件/目录是否存在,存在则返回0;

        -f 判断是否是文件,是则返回0;

        -s 判断path是否非空,非空则返回0;

        -w 判断path存在并且有写的权限,返回0;

        -r 判断path存在并且有读的权限,返回0;

        -z 判断文件大小如果为0字节,则返回0.

判断文件是否存在的脚本:

#!/bin/sh

path='/test/a.txt'  # 要判断的路径

hdfs dfs -test -e ${path}
if [ $? -eq 0 ]; then
   echo "Path is exist!"
else
   echo "Path is not exist!"
fi

拓展:

在Linux命令中,以下运算符用于整数之间的比较‌

-eq 表示两个数值相等。

-ne:不等于

-gt:大于

-ge:大于或等于

-lt:小于

-le:小于或等于

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值