Linux中判斷hdfs檔案是否存在

2022-07-20 11:42:11 字數 1734 閱讀 5188

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

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

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

echo "檔案不存在!"

fi

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

在linux檔案系統中,我們可以使用下面shell指令碼判斷:

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

echo "檔案不存在"

fi同樣hadoop內建了提供了判斷某個檔案是否存在的命令

hadoop fs -test 

-d 判斷是否是目錄

-e 判斷是否存在

-f 判斷是否是個檔案

-s 判斷內容是否大於0bytes ,大於0為真

-z 判斷內容是否等於0bytes,為0真

從上面的輸出可以看出,我們可以使用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

所以,上**:

hadoop fs -test -e /path/exist

if [ $? -eq 0 ] ;then

echo 'exist'

else

echo 'error! path is not exist'

fi

除此之外,還可以判斷某個檔案是否是資料夾、是否是檔案、某個檔案的大小是否大於0或者等於0。

hadoop fs -test -d /path/exist

if [ $? -eq 0 ] ;then

echo 'is a directory'

else

echo 'is not a directory'

fi

hadoop fs -test -f /path/exist

if [ $? -eq 0 ] ;then

echo 'is a file'

else

echo 'is not a file' fi

hadoop fs -test -s /path/exist

if [ $? -eq 0 ] ;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 [ $? -eq 0 ] ;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...

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

在linux檔案系統中,我們可以使用下面的shell指令碼判斷某個檔案是否存在 這裡的 f引數判斷 file是否存在 if f file then echo 檔案不存在 fi 但是我們想判斷hdfs上某個檔案是否存在咋辦呢?別急,hadoop內建提供了判斷某個檔案是否存在的命令 iteblog ww...

HDFS中的檔案許可

hdfs對於檔案及目錄有與posix非常相似的許可模式。共有三種形式的許可 讀取許可 r 寫入許可 w 和執行許可 x 讀取檔案或列出目錄內容時需要讀取許可。寫入乙個檔案,或是在乙個目錄上建立或刪除檔案或目錄,需要寫入許可。對於檔案而言執行許可可以忽略因為hdfs中不能執行檔案 與posix不同 但...