查找空目录
我需要找到给定目录列表的空目录。 有些目录里面有目录。
如果内部目录也为空,我可以说主目录为空,否则它不为空。
我该如何测试这个?
例如:
A>A1(file1),A2 this is not empty beacuse of file1
B>B1(no file) this is empty
C>C1,C2 this is empty
I need to find empty directories for a given list of directories.
Some directories have directories inside it.
If inside directories are also empty I can say main directory is empty otherwise it's not empty.
How can I test this?
For example:
A>A1(file1),A2 this is not empty beacuse of file1
B>B1(no file) this is empty
C>C1,C2 this is empty
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
以下将删除所有空目录(没有子目录的目录)。由于它是深度优先(自下而上)的,因此它将删除仅包含(空)目录作为子目录的目录。请注意,在
find
中,参数的顺序很重要。删除
-delete
以查看匹配的文件和目录,但请注意,由于-深度
,将不会列出变空的父目录(请注意,在某些find
实现中,-delete
隐含了-深度
)。如果您还想删除空文件,请删除-type d
。例如:
The following will remove all empty directories (those with no children). As it does so depth-first (bottom-up), it will remove directories with only (empty) directories as children. Beware that, in
find
, the order of arguments matters.Drop
-delete
to see the files and directories matched, however note that because of-depth
, parent dirs that would become empty will not be listed (beware that-depth
is implied by-delete
in somefind
implementations). Remove the-type d
if you would to also like to delete empty files.For example:
您可以使用以下命令:
You can use the following command:
检查
是否找到
-type f
输出任何内容。这是一个例子:Check whether
find <dir> -type f
outputs anything. Here's an example:这是我觉得最有趣的版本。如果从目录内部执行,它将删除下面的所有空目录(如果目录仅包含空目录,则该目录被视为空)。
mindepth 选项可防止目录本身在碰巧为空时被删除。
This is the version that I found most interesting. If executed from inside directory, it will delete all empty directories below (a directory is considered empty if it only contains empty directories).
The mindepth option prevents the directory itself from being deleted if it happens to be empty.
<代码>查找 . -type d -empty
查找并列出当前树中的空目录和子目录。
例如,空目录和子目录的结果列表:
不对目录进行任何操作。它们只是简单地列出来。
这对我有用。
find . -type d -empty
finds and lists empty directories and sub-directories in the current tree.
E.g. resulting list of empty dirs and subdirs:
No operation is made on the directories. They are simply listed.
This works for me.
只找到空目录
为了只找到空目录(如问题标题中指定),mosg 的答案是正确的:
但是
-empty
在非常旧的find
版本上可能不可用(HP-UX 例如)。如果您遇到这种情况,请参阅下面目录是否为空?部分中描述的技术。删除空目录
这有点棘手:假设目录
MyDir
包含空目录。删除这些空目录后,MyDir
将成为空目录,也应该删除。因此,我使用带有选项--parents
(或-p
)的命令rmdir
,该选项还会在可能的情况下删除父目录: >find版本尚不支持
+
语句,因此您可以使用;
代替:目录是否为空?
这些答案中的大多数都解释了如何检查目录是否为空。因此,我在这里提供我所知道的三种不同的技术:
[ $(find your/dir -prune -empty) = your/dir ]
变体:
说明:
find -prune
与find -maxdepth 0
类似,使用更少的字符find -type d
仅打印目录find -empty
打印空目录和文件<前><代码>> mkdir -v 空1 空2 not_empty
mkdir:创建目录“empty1”
mkdir:创建目录“empty2”
mkdir:创建目录“not_empty”
>触摸 not_empty/文件
>查找空1 空2 not_empty -prune -empty
空1
空2
(( ${#files} ))
这个技巧是 100%
bash
,但会调用(生成)一个子 shell。这个想法来自 Bruno De Fraine 并由 teambob 的评论。如果您使用 bash 并且您的脚本不必是可移植的。注意:空目录和不存在的目录之间没有区别(即使提供的路径是文件)。
[ $(ls -A your/dir) ]
这个技巧的灵感来自nixCraft 的文章 于 2007 年发布。Andrew Taylor 于 2008 年回答,gr8can8dian 2011 年。
或者单行 bashism 版本:
注意: 当目录不存在时,
ls
返回$?=2
。但文件和空目录没有区别。Just find empty dirs
In order to just find empty directories (as specified in the question title), the mosg's answer is correct:
But
-empty
may not be available on very oldfind
versions (this is the case of HP-UX for example). If this is your case, see the techniques described in below section Is a directory empty?.Delete empty dirs
This is a bit tricky: Suppose a directory
MyDir
contains empty directories. After removing these empty directories,MyDir
will become an empty directory and should also be removed. Therefore I use the commandrmdir
with the option--parents
(or-p
) that also removes parent directories when possible:On older
find
version the statement+
is not yet supported, therefore you may use;
instead:Is a directory empty?
Most of these answers explain how to check if a directory is empty. Therefore I provide here the three different techniques I know:
[ $(find your/dir -prune -empty) = your/dir ]
a variation:
Explanation:
find -prune
is similar thanfind -maxdepth 0
using less charactersfind -type d
prints directories onlyfind -empty
prints the empty directories and files(( ${#files} ))
This trick is 100%
bash
but invokes (spawns) a sub-shell. The idea is from Bruno De Fraine and improved by teambob's comment. I advice this one if you use bash and if your script does not have to be portable.Note: no difference between an empty directory and a non-existing one (and even when the provided path is a file).
[ $(ls -A your/dir) ]
This trick is inspired from nixCraft's article posted in 2007. Andrew Taylor answered in 2008 and gr8can8dian in 2011.
or the one-line bashism version:
Note:
ls
returns$?=2
when the directory does not exist. But no difference between a file and an empty directory.rmdir *
怎么样?该命令将在非空目录上失败。How about
rmdir *
? That command will fail on non-empty directories.这个递归函数似乎可以解决这个问题:
给定这个示例目录结构:
运行该函数的结果将是:
它错过了
/dir4/dirD
。如果将递归调用findemty "$dir"
移到fi
之后,该函数将在其结果中包含该目录。This recursive function would seem to do the trick:
Given this example directory structure:
The result of running that function would be:
which misses
/dir4/dirD
. If you move the recursive callfindempty "$dir"
after thefi
, the function will include that directory in its results.如果目录为空(或不存在),以下命令将返回 1,否则返回 0(因此可以在 shell 脚本中使用
!
反转返回代码):The following command returns 1 if a directory is empty (or does not exists) and 0 otherwise (so it is possible to invert the return code with
!
in a shell script):我创建了一个简单的结构,如下所示:
test/test3/file
包含一些垃圾文本。发出
find test -empty
返回“test/test2/test2.2
”作为唯一的空目录。I created a simple structure as follows:
The
test/test3/file
contains some junk text.Issuing
find test -empty
returns "test/test2/test2.2
" as the only empty directory.一个简单的方法
也是,
a simple approach would be,
also,