如何使用 find 测试目录是否为空

发布于 2024-11-26 17:38:10 字数 209 浏览 1 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

银河中√捞星星 2024-12-03 17:38:10

简单 - 使用 -empty 标志。引用 find 手册页:

 -empty 如果当前文件或目录为空,则为 true。

所以类似:

find . -type d -empty

将列出所有空目录。

Simple - use the -empty flag. Quoting the find man page:

 -empty  True if the current file or directory is empty.

So something like:

find . -type d -empty

Will list all the empty directories.

想你只要分分秒秒 2024-12-03 17:38:10

三个最佳答案:

  1. 第一个是基于 find as OP requests ;
  2. 第二个基于 ls ;
  3. 第三个是 100% bash,但它调用(生成)一个子 shell。

1. [ $(find your/dir -prune -empty) = your/dir ]

dn=your/dir
if [ x$(find "$dn" -prune -empty) = x"$dn" ]; then
  echo empty
else
  echo not empty
fi

test:

> mkdir -v empty1 empty2 not_empty
mkdir: created directory 'empty1'
mkdir: created directory 'empty2'
mkdir: created directory 'not_empty'
> touch not_empty/file
> find empty1 empty2 not_empty -prune -empty
empty1
empty2

find 仅打印了两个空目录(empty1empty2)。

这个答案看起来像来自 阿里尔。但这个答案有点短;)

2. [ $(ls -A your/directory) ]

if [ "$(ls -A your/dir)" ]; then
  echo not empty
else
  echo empty
fi

[ "$(ls -A your/dir)" ] && echo not empty || echo empty

类似于 Michael Berkowskigpojd 答案。但这里我们不需要通过管道连接到wc。另请参阅 Bash Shell 检查目录是否为空或不是 nixCraft (2007) 的。

3. (( ${#files} ))

files=$(shopt -s nullglob dotglob; echo your/dir/*)
if (( ${#files} )); then 
  echo not empty
else 
  echo empty or does not exist
fi

注意:如上例所示,空目录和不存在的目录没有区别。

最后一个答案的灵感来自 Bruno De Fraine 的答案以及 teambob

Three best answers:

  1. The first one is based on find as OP requested ;
  2. The second is based on ls ;
  3. The third one is 100% bash but it invokes (spawns) a sub-shell.

1. [ $(find your/dir -prune -empty) = your/dir ]

dn=your/dir
if [ x$(find "$dn" -prune -empty) = x"$dn" ]; then
  echo empty
else
  echo not empty
fi

test:

> mkdir -v empty1 empty2 not_empty
mkdir: created directory 'empty1'
mkdir: created directory 'empty2'
mkdir: created directory 'not_empty'
> touch not_empty/file
> find empty1 empty2 not_empty -prune -empty
empty1
empty2

find has printed the two empty directories only (empty1 and empty2).

This answer looks like the -maxdepth 0 -empty from Ariel. But this answer is a bit shorter ;)

2. [ $(ls -A your/directory) ]

if [ "$(ls -A your/dir)" ]; then
  echo not empty
else
  echo empty
fi

or

[ "$(ls -A your/dir)" ] && echo not empty || echo empty

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} ))

files=$(shopt -s nullglob dotglob; echo your/dir/*)
if (( ${#files} )); then 
  echo not empty
else 
  echo empty or does not exist
fi

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.

慕烟庭风 2024-12-03 17:38:10

一定有一种更简单的方法,但是您可以使用通过管道传输到 wc -l 的 ls -1A 来测试空/非空目录

DIRCOUNT=$(ls -1A /path/to/dir |wc -l)
if [ $DIRCOUNT -eq 0 ]; then
  # it's empty
fi

There must be an easier way, but you can test for an empty/nonempty directory with ls -1A piped to wc -l

DIRCOUNT=$(ls -1A /path/to/dir |wc -l)
if [ $DIRCOUNT -eq 0 ]; then
  # it's empty
fi
心的憧憬 2024-12-03 17:38:10
find directoryname -maxdepth 0 -empty
find directoryname -maxdepth 0 -empty
时光倒影 2024-12-03 17:38:10

为什么一定要使用查找呢?在 bash 中,ls -a 将为空目录返回两个文件(...),并且应该比非空目录返回更多文件。空的。

if [ $(ls -a | wc -l) -eq 2 ]; then echo "empty"; else echo "not empty"; fi

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.

if [ $(ls -a | wc -l) -eq 2 ]; then echo "empty"; else echo "not empty"; fi
猫七 2024-12-03 17:38:10
if [ `find foo | wc -l` -eq 1 ]
then
    echo Empty
else
    echo Not empty
fi

foo 是这里的目录名称。

if [ `find foo | wc -l` -eq 1 ]
then
    echo Empty
else
    echo Not empty
fi

foo is the directory name here.

抱猫软卧 2024-12-03 17:38:10

类似下面的

dircnt.sh:
-----------
#!/bin/sh

if [ `ls $1 2> /dev/null | wc -l` -gt 0 ]; then echo true; else echo false; fi

用法

andreas@earl ~
$ mkdir asal

andreas@earl ~
$ sh dircnt.sh asal
false

andreas@earl ~
$ touch asal/1

andreas@earl ~
$ sh dircnt.sh asal
true

Something like below

dircnt.sh:
-----------
#!/bin/sh

if [ `ls $1 2> /dev/null | wc -l` -gt 0 ]; then echo true; else echo false; fi

Usage

andreas@earl ~
$ mkdir asal

andreas@earl ~
$ sh dircnt.sh asal
false

andreas@earl ~
$ touch asal/1

andreas@earl ~
$ sh dircnt.sh asal
true
吻安 2024-12-03 17:38:10

我不喜欢使用 ls 因为我有一些非常大的目录,而且我讨厌浪费资源来用所有这些东西填充管道。

我也不喜欢用所有这些东西填充 $files 变量。

因此,虽然 @libre 的所有答案都很有趣,但我发现它们都不可读,并且更喜欢制作我最喜欢的“查找”解决方案的功能:

function isEmptyDir {
  [ -d $1 -a -n "$( find $1 -prune -empty 2>/dev/null )" ]
}

这样我就可以编写一年后可以阅读的代码,而无需询问“什么”我在想”吗?

if isEmptyDir some/directory
then
  echo "some/directory is empty"
else
  echo "some/directory does not exist, is not a directory, or is empty
fi

或者我可以使用额外的代码来梳理负面结果,但是该代码
应该是很明显的。无论如何,我马上就会知道我在想什么。

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:

function isEmptyDir {
  [ -d $1 -a -n "$( find $1 -prune -empty 2>/dev/null )" ]
}

So that I can write code that I can read a year from now without asking "what was I thinking"?

if isEmptyDir some/directory
then
  echo "some/directory is empty"
else
  echo "some/directory does not exist, is not a directory, or is empty
fi

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.

小伙你站住 2024-12-03 17:38:10

没有脚本,没有 fork/exec(echo 是内置的)...

[ "$(cd $dir;echo *)" = "*" ] && echo empty || echo non-empty

NO script, no fork/exec (echo is a builtin)...

[ "$(cd $dir;echo *)" = "*" ] && echo empty || echo non-empty
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文