在shell中判断文件是否存在可以通过-f $fileName
进行判断,判断文件夹是否存在可以通过-d $directory
进行判断,如以下脚本:
shell判断对文件和文件夹的判断
#!/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
那么,怎么判断hdfs文件和文件夹是否存在呢?
首先,我们来看看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.
..........
..........
参数解释说明:
- -d 如果路径是目录,返回0
- -e 如果路径存在,返回0
- -f 如果路径是文件,返回0
- -s 如果文件的大小大于0字节则返回0
- -z 如果文件的大小为0,则返回0,否则返回1
判断路径是否存在
#!/bin/sh
hdfs dfs -test -e "/fuyun/"
if [ $? -eq 0 ]; then
echo "parth is exists!"
else
echo "Error! parth is not exists!"
fi
test命令还可以判断某个文件是否是文件夹、是否是文件、某个文件大小是否大于0或者等于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