在Linux中,可以使用if else
语句结合[ -d FILE ]
、[ -f FILE ]
、[ -h FILE ]
等条件来判断文件的类型。以下是几种常见的文件类型判断方法:
if [ -f FILE ]; then
echo "FILE is a regular file"
else
echo "FILE is not a regular file"
fi
if [ -d FILE ]; then
echo "FILE is a directory"
else
echo "FILE is not a directory"
fi
if [ -h FILE ]; then
echo "FILE is a symbolic link"
else
echo "FILE is not a symbolic link"
fi
if [ -r FILE ]; then
echo "FILE exists and is readable"
else
echo "FILE does not exist or is not readable"
fi
以上是几种常见的文件类型判断方法,根据实际情况可以结合不同的条件来判断文件的类型。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:linux的test如何判断文件类型