How to check if the file or directory exists in HDFS?

Hadoop command to check whether the directory exists or not:

Syntax: hdfs dfs -test -d hdfs_path

Example: hdfs dfs -test -d /revisit/content/files

echo $? —> to validate the previous command return code.

Explanation

If you are using hadoop version 1, please use hadoop fs instead of hdfs dfs and rest of the command is same for it. If the given hdfs directory present in the system,it will return the code as 0.Otherwise it will return the code as 1.We can validate the return code by using echo $? command after the execution of hadoop command.

Hadoop command to check whether the file exists or not

Syntax : hdfs dfs -test -e hdfs_path/filename

Example: hdfs dfs -test -e /revisit/content/files/schema.xml

echo $? —> To validate the previous command return code.

Explanation

If the file schema.xml present in the given hdfs path,it will return the code as 0.Otherwise it will return the code as 1.If you are using the shell script to execute the hadoop command,you can validate the return code using the if condition in the program.

Sample shell script code to validate the hadoop command return codes

Recommended Articles