在shell中如何判斷HDFS中的檔案目錄是否存在

2021-09-25 07:29:08 字數 3852 閱讀 2759

在linux檔案系統中,我們可以使用下面的shell指令碼判斷某個檔案是否存在:

# 這裡的-f引數判斷$file是否存在

if[ ! -f"$file"];then

echo"檔案不存在!"

fi

但是我們想判斷hdfs上某個檔案是否存在咋辦呢?別急,hadoop內建提供了判斷某個檔案是否存在的命令:

[[email protected] ~]$ hadoop fs -help

......

-test-[defsz] :answer various questions about , with result viaexitstatus.

-dreturn0ifis a directory.

-ereturn0ifexists.

-freturn0ifis afile.

-sreturn0iffileis greater than zero bytesinsize.

-zreturn0iffileis zero bytesinsize.

else,return1.

......

從上面的輸出可以看出,我們可以使用test命令來判斷某個檔案是否存在。如果檔案存在,這個命令將返回0;反之則返回1。

[[email protected] ~]$ hadoop fs -test-e/path/not/exist

[[email protected] ~]$echo$?

1

[[email protected] ~]$ hadoop fs -test-e/path/exist

[[email protected] ~]$echo$?

0

所以我們可以在shell裡面判斷hdfs上某個檔案是否存在:

hadoop fs -test-e/path/exist

if[ $? -eq0 ] ;then

echo'exist'

else

echo'error! path is not exist'

fi

test命令還可以判斷某個檔案是否是資料夾、是否是檔案、某個檔案大小是否大於0或者等於0。

hadoop fs -test-d/path/exist

if[ $? -eq0 ] ;then

echo'is a directory'

else

echo'is not a directory'

fi

hadoop fs -test-f/path/exist

if[ $? -eq0 ] ;then

echo'is a file'

else

echo'is not a file'

fi

hadoop fs -test-s/path/exist

if[ $? -eq0 ] ;then

echo'is greater than zero bytes in size'

else

echo'is not greater than zero bytes in size'

fi

hadoop fs -test-z/path/exist

if[ $? -eq0 ] ;then

echo'is zero bytes in size.'

else

echo'is not zero bytes in size. '

fi

Shell中判斷HDFS中的檔案或目錄是否存在

在linux檔案系統中,shell指令碼判斷某個檔案是否存在 這裡的 f引數判斷 file是否存在 if f file then echo 檔案不存在 fihadoop提供了test命令判斷hdfs上某個檔案或目錄是否存在 root node00 hdfs dfs help test defsz a...

hdfs 中 Shell 類介紹

shell 類功能 是乙個提供執行作業系統命令的類,主要有兩個抽象方法給基類去繼承,1.abstract string getexecstring 繼承類實現該方法,提供乙個要給shell在執行的命令 2.abstract void parseexecresult bufferedreader li...

HDFS中的shell操作

1.首先命令都是以hadoop fs 開頭 2.hadoop fs ls 檢視hdfs的根目錄下的內容,hadoop fs lsr 遞迴檢視根目錄下的內容 3.hadoop fs mkdir gao,在hdfs上建立資料夾gao 4.hadoop fs put 把資料從linux上傳到hdfs的特定...