Judgment of HDFS files and directories in the shell

In the shell, you can judge whether a file exists or not, and you can -f $fileNamejudge whether a folder exists or not -d $directory, such as the following script:
Shell judgment judgment of files and folders

#!/bin/sh

file="/opt/cdh-5.7.6/hadoop-2.6.0-cdh5.7.6/stop_all.sh"
if [ ! -f "$file" ]; then
  echo "文件不存在!"
else 
  echo "文件存在!"
fi

directory="/opt/cdh-5.7.6/hadoop-2.6.0-cdh5.7.6/"
if [ ! -d "$directory" ]; then
  echo "文件夹不存在!"
else 
  echo "文件夹存在!"
fi

So, how to judge whether hdfs files and folders exist?
First, let’s take a look at the built-in commands and parameters on hdfs

[fuyun@bigdata-training shell]$ hdfs dfs -help
Usage: hadoop fs [generic options]
	[-appendToFile <localsrc> ... <dst>]
	[-cat [-ignoreCrc] <src> ...]
	[-checksum <src> ...]
	[-chgrp [-R] GROUP PATH...]
	[-chmod [-R] <MODE[,MODE]... | OCTALMODE> PATH...]
	[-chown [-R] [OWNER][:[GROUP]] PATH...]
	[-copyFromLocal [-f] [-p] [-l] <localsrc> ... <dst>]
	[-copyToLocal [-p] [-ignoreCrc] [-crc] <src> ... <localdst>]
	[-count [-q] [-h] [-v] <path> ...]
	[-cp [-f] [-p | -p[topax]] <src> ... <dst>]
	[-createSnapshot <snapshotDir> [<snapshotName>]]
	[-deleteSnapshot <snapshotDir> <snapshotName>]
	[-df [-h] [<path> ...]]
	[-du [-s] [-h] <path> ...]
	[-expunge]
	[-find <path> ... <expression> ...]
	[-get [-p] [-ignoreCrc] [-crc] <src> ... <localdst>]
	[-getfacl [-R] <path>]
	[-getfattr [-R] {
    
    -n name | -d} [-e en] <path>]
	[-getmerge [-nl] <src> <localdst>]
	[-help [cmd ...]]
	[-ls [-C] [-d] [-h] [-q] [-R] [-t] [-S] [-r] [-u] [<path> ...]]
	[-mkdir [-p] <path> ...]
	[-moveFromLocal <localsrc> ... <dst>]
	[-moveToLocal <src> <localdst>]
	[-mv <src> ... <dst>]
	[-put [-f] [-p] [-l] <localsrc> ... <dst>]
	[-renameSnapshot <snapshotDir> <oldName> <newName>]
	[-rm [-f] [-r|-R] [-skipTrash] <src> ...]
	[-rmdir [--ignore-fail-on-non-empty] <dir> ...]
	[-setfacl [-R] [{
    
    -b|-k} {
    
    -m|-x <acl_spec>} <path>]|[--set <acl_spec> <path>]]
	[-setfattr {
    
    -n name [-v value] | -x name} <path>]
	[-setrep [-R] [-w] <rep> <path> ...]
	[-stat [format] <path> ...]
	[-tail [-f] <file>]
	[-test -[defsz] <path>]
	[-text [-ignoreCrc] <src> ...]
	[-touchz <path> ...]
	[-usage [cmd ...]]

...........
...........
-test -[defsz] <path> :
  Answer various questions about <path>, with result via exit status.
    -d  return 0 if <path> is a directory.
    -e  return 0 if <path> exists.
    -f  return 0 if <path> is a file.
    -s  return 0 if file <path> is greater than zero bytes in size.
    -z  return 0 if file <path> is zero bytes in size, else return 1.
..........
..........

Parameter explanation:

  • -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 returns 0 if the size of the file is greater than 0 bytes
  • -z If the size of the file is 0, return 0, otherwise return 1

Determine whether the path exists

#!/bin/sh

hdfs dfs -test -e "/fuyun/"
if [ $? -eq 0 ]; then
  echo "parth is exists!"
else 
  echo "Error! parth is not exists!"
fi

The test command can also determine whether a file is a folder, whether it is a file, whether the size of a file is greater than 0 or equal to 0

#!/bin/sh

hdfs dfs -test -e "/fuyun/"
# 判断路径是否存在
if [ $? -eq 0 ]; then
  echo "parth is exists!"
else 
  echo "Error! parth is not exists!"
fi

# 判断是否为文件夹
hdfs dfs -test -d "/fuyun/"
if [ $? -eq 0 ]; then
  echo "parth is directory!"
else 
  echo "parth is not directory!"
fi

# 判断是否为文件
hdfs dfs -test -f "/fuyun/"
if [ $? -eq 0 ]; then
  echo "parth is file!"
else 
  echo "parth is not file!"
fi

# 判断路径的大小是否大于0 bytes
hdfs dfs -test -s "/fuyun/"
if [ $? -eq 0 ]; then
  echo "Is greater than zero bytes in size!"
else 
  echo "Is not greater than zero bytes in size!"
fi

# 判断路径的大小是否等于0 bytes
hdfs dfs -test -s "/fuyun/"
if [ $? -eq 0 ]; then
  echo "Is zero bytes in size!"
else 
  echo "Is not zero bytes in size!"
fi

Guess you like

Origin blog.csdn.net/lz6363/article/details/115034645