在shell中,可以使用以下命令来判断一个目录下是否有文件:
if [ "$(ls -A /path/to/directory)" ]; then
echo "Directory is not empty"
else
echo "Directory is empty"
fi
这里的/path/to/directory
是要检查的目录的路径。上面的命令使用ls -A
命令来列出目录下的文件和子目录,然后通过[ "$(ls -A /path/to/directory)" ]
来判断是否有文件。如果目录不为空,则输出"Directory is not empty",否则输出"Directory is empty"。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: shell怎么传带空格的参数