Test650
Test650

Reputation: 13

How to Check Directory exist or not in HDFS

To check directory exist or not ,i have used this below command

hdfs dfs -test -d /HDFS/Sample

Here Sample is a directory in HDFS.

commands says that if path is directory,it will return 0 , when i execute this command in prompt,i am unable to get 0 in command prompt.

Upvotes: 1

Views: 3591

Answers (2)

kevin zhang
kevin zhang

Reputation: 19

[oracle@bigdatalite tmp]$ hdfs dfs -ls 
Found 12 items
drwx------   - oracle oracle          0 2018-01-09 15:06 .Trash
drwxr-xr-x   - oracle oracle          0 2018-01-09 16:06 .sparkStaging
drwx------   - oracle oracle          0 2017-11-20 14:44 .staging
drwxr-xr-x   - oracle oracle          0 2017-01-15 17:19 indexMetadata
drwxr-xr-x   - oracle oracle          0 2017-01-15 17:19 jobRegistry
drwxr-xr-x   - oracle oracle          0 2018-10-02 11:04 kevin_learning
drwxr-xr-x   - oracle oracle          0 2017-01-15 17:19 mediademo
drwxr-xr-x   - oracle oracle          0 2017-01-15 17:19 moviedemo
drwxr-xr-x   - oracle oracle          0 2017-01-15 17:19 moviework
drwxr-xr-x   - oracle oracle          0 2017-01-15 17:19 oggdemo
drwxr-xr-x   - oracle oracle          0 2017-01-15 17:19 oozie-oozi
drwxrwxrwx   - oracle oracle          0 2017-11-17 16:39 tmp
[oracle@bigdatalite tmp]$ hdfs dfs -test -d kevin_learning
[oracle@bigdatalite tmp]$ echo $?
0
[oracle@bigdatalite tmp]$ hdfs dfs -test -d kevin_learning2
[oracle@bigdatalite tmp]$ echo $?
1
[oracle@bigdatalite tmp]$ 

Upvotes: 0

Shubhangi
Shubhangi

Reputation: 2254

You will have to check exit status of command. In bash you may use echo $?.

$hdfs dfs -test -d /tmp/testdir
$echo $?
1

Above example shows /tmp/testdir does not exit.

Upvotes: 1

Related Questions