<分区>
我有一个问题
我尝试了一些类似的功能
DIR=/path/tmp/
if [ -d "$DIR" ]; then
和
if [ -f "$DIR" ]; then
但只检查/path/tmp这个路径
我该怎么办?
<分区>
我有一个问题
我尝试了一些类似的功能
DIR=/path/tmp/
if [ -d "$DIR" ]; then
和
if [ -f "$DIR" ]; then
但只检查/path/tmp这个路径
我该怎么办?
最佳答案
shopt -s nullglob dotglob
files=(*)
(( ${#files[*]} )) || echo directory is empty
shopt -u nullglob dotglob
这个小脚本用路径扩展 *
中找到的每个文件填充数组 files
。然后检查数组的大小,如果为 0,则打印“目录为空”。由于使用了 dotglob
,这将适用于隐藏文件。
要求解析 ls
的答案通常是一个坏主意和糟糕的形式。要了解原因,请阅读 Why you shouldn't parse the output of ls
关于linux - 如何检查文件夹是否为空或文件夹文件是否使用 shell 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4253698/