如何使用 find 测试目录是否为空
我正在尝试在 unix shell 脚本中编写一个 if 语句,如果它为空,则返回 true,如果不是,则返回 false。
这种事情……
if directory foo is empty then
echo empty
else
echo not empty
fi
我该怎么办?有人告诉我 find 是一个很好的起点
I am trying to write an if statement in a unix shell script that returns true if it's empty, and false if it's not.
This type of thing...
if directory foo is empty then
echo empty
else
echo not empty
fi
How do I do this? I'm told find is a good palce to start
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
简单 - 使用 -empty 标志。引用 find 手册页:
所以类似:
将列出所有空目录。
Simple - use the -empty flag. Quoting the find man page:
So something like:
Will list all the empty directories.
三个最佳答案:
find
as OP requests ;1.
[ $(find your/dir -prune -empty) = your/dir ]
test:
find
仅打印了两个空目录(empty1
和empty2
)。这个答案看起来像来自 阿里尔。但这个答案有点短;)
2.
[ $(ls -A your/directory) ]
或
类似于 Michael Berkowski 和 gpojd 答案。但这里我们不需要通过管道连接到
wc
。另请参阅 Bash Shell 检查目录是否为空或不是 nixCraft (2007) 的。3.
(( ${#files} ))
注意:如上例所示,空目录和不存在的目录没有区别。
最后一个答案的灵感来自 Bruno De Fraine 的答案以及 teambob。
Three best answers:
find
as OP requested ;ls
;bash
but it invokes (spawns) a sub-shell.1.
[ $(find your/dir -prune -empty) = your/dir ]
test:
find
has printed the two empty directories only (empty1
andempty2
).This answer looks like the
-maxdepth 0 -empty
from Ariel. But this answer is a bit shorter ;)2.
[ $(ls -A your/directory) ]
or
Similar to Michael Berkowski and gpojd answers. But here we do not require to pipe to
wc
. See also Bash Shell Check Whether a Directory is Empty or Not by nixCraft (2007).3.
(( ${#files} ))
Caution: as written in this above example, there is no difference between an empty directory and a non-existing one.
This last answer has been inspired from the Bruno De Fraine's answer and the excellent comments from teambob.
一定有一种更简单的方法,但是您可以使用通过管道传输到
wc -l
的 ls -1A 来测试空/非空目录There must be an easier way, but you can test for an empty/nonempty directory with
ls -1A
piped towc -l
为什么一定要使用查找呢?在 bash 中,
ls -a
将为空目录返回两个文件(.
和..
),并且应该比非空目录返回更多文件。空的。Why do you have to use find? In bash,
ls -a
will return two files (.
and..
) for an empty directory and should have more than that for non-empty ones.foo
是这里的目录名称。foo
is the directory name here.类似下面的
用法
Something like below
Usage
我不喜欢使用 ls 因为我有一些非常大的目录,而且我讨厌浪费资源来用所有这些东西填充管道。
我也不喜欢用所有这些东西填充 $files 变量。
因此,虽然 @libre 的所有答案都很有趣,但我发现它们都不可读,并且更喜欢制作我最喜欢的“查找”解决方案的功能:
这样我就可以编写一年后可以阅读的代码,而无需询问“什么”我在想”吗?
或者我可以使用额外的代码来梳理负面结果,但是该代码
应该是很明显的。无论如何,我马上就会知道我在想什么。
I don't like using ls because I have some very large directories and I hate wasting the resources to fill a pipe with all that stuff.
I don't like filling a $files variable with all that stuff either.
So while all of @libre's answers are interesting, I find them all unreadable, and prefer to make a function of my favorite, the 'find' solution:
So that I can write code that I can read a year from now without asking "what was I thinking"?
or I can use additional code to tease apart the negative results, but that code
should be pretty obvious. In any case, I'll know right away what I was thinking.
没有脚本,没有 fork/exec(echo 是内置的)...
NO script, no fork/exec (echo is a builtin)...