在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"。