查找空目录

发布于 2024-08-31 22:38:56 字数 235 浏览 2 评论 0原文

我需要找到给定目录列表的空目录。 有些目录里面有目录。

如果内部目录也为空,我可以说主目录为空,否则它不为空。

我该如何测试这个?

例如:

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 技术交流群。

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

发布评论

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

评论(12

扛刀软妹 2024-09-07 22:38:56

以下将删除所有空目录(没有子目录的目录)。由于它是深度优先(自下而上)的,因此它将删除仅包含(空)目录作为子目录的目录。请注意,在 find 中,参数的顺序很重要。

find testdir -depth -type d -empty -delete

删除 -delete 以查看匹配的文件和目录,但请注意,由于 -深度,将不会列出变空的父目录(请注意,在某些 find 实现中,-delete 隐含了 -深度)。如果您还想删除空文件,请删除-type d

例如:

$ mkdir -p testdir/foo/bar/baz; touch testdir/foo/afile.txt

$ find testdir -printf '%y %p\n' | tee before.txt
d testdir
d testdir/foo
d testdir/foo/bar
d testdir/foo/bar/baz
f testdir/foo/afile.txt

$ find testdir -depth -type d -empty
testdir/foo/bar/baz

$ find testdir -depth -type d -empty -delete

$ find testdir -printf '%y %p\n' | tee after.txt
d testdir
d testdir/foo
f testdir/foo/afile.txt

$ diff -u {before,after}.txt | tail +4
 d testdir
 d testdir/foo
-d testdir/foo/bar
-d testdir/foo/bar/baz
 f testdir/foo/afile.txt

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.

find testdir -depth -type d -empty -delete

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 some find implementations). Remove the -type d if you would to also like to delete empty files.

For example:

$ mkdir -p testdir/foo/bar/baz; touch testdir/foo/afile.txt

$ find testdir -printf '%y %p\n' | tee before.txt
d testdir
d testdir/foo
d testdir/foo/bar
d testdir/foo/bar/baz
f testdir/foo/afile.txt

$ find testdir -depth -type d -empty
testdir/foo/bar/baz

$ find testdir -depth -type d -empty -delete

$ find testdir -printf '%y %p\n' | tee after.txt
d testdir
d testdir/foo
f testdir/foo/afile.txt

$ diff -u {before,after}.txt | tail +4
 d testdir
 d testdir/foo
-d testdir/foo/bar
-d testdir/foo/bar/baz
 f testdir/foo/afile.txt
寄离 2024-09-07 22:38:56

您可以使用以下命令:

find . -type d -empty

You can use the following command:

find . -type d -empty
残龙傲雪 2024-09-07 22:38:56

检查是否找到

-type f 输出任何内容。这是一个例子:

for dir in A B C; do
    [ -z "`find $dir -type f`" ] && echo "$dir is empty"
done

Check whether find <dir> -type f outputs anything. Here's an example:

for dir in A B C; do
    [ -z "`find $dir -type f`" ] && echo "$dir is empty"
done
讽刺将军 2024-09-07 22:38:56
find directory -mindepth 1 -type d -empty -delete

这是我觉得最有趣的版本。如果从目录内部执行,它将删除下面的所有空目录(如果目录仅包含空目录,则该目录被视为空)。

mindepth 选项可防止目录本身在碰巧为空时被删除。

find directory -mindepth 1 -type d -empty -delete

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.

暮凉 2024-09-07 22:38:56

<代码>查找 . -type d -empty

查找并列出当前树中的空目录和子目录。
例如,空目录和子目录的结果列表:

./2047
./2032
./2049
./2063
./NRCP26LUCcct1/2039
./NRCP26LUCcct1/2054
./NRCP26LUCcct1/2075
./NRCP26LUCcct1/2070

不对目录进行任何操作。它们只是简单地列出来。
这对我有用。

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:

./2047
./2032
./2049
./2063
./NRCP26LUCcct1/2039
./NRCP26LUCcct1/2054
./NRCP26LUCcct1/2075
./NRCP26LUCcct1/2070

No operation is made on the directories. They are simply listed.
This works for me.

森林散布 2024-09-07 22:38:56

只找到空目录

为了只找到空目录(如问题标题中指定),mosg 的答案是正确的:

find -type d -empty

但是 -empty 在非常旧的 find 版本上可能不可用(HP-UX 例如)。如果您遇到这种情况,请参阅下面目录是否为空?部分中描述的技术。

删除空目录

这有点棘手:假设目录 MyDir 包含空目录。删除这些空目录后,MyDir 将成为空目录,也应该删除。因此,我使用带有选项 --parents(或 -p)的命令 rmdir,该选项还会在可能的情况下删除父目录

find -type d -empty -exec rmdir -vp --ignore-fail-on-non-empty {} +

: >find版本尚不支持+语句,因此您可以使用;代替:

find -type d -empty -exec rmdir -vp --ignore-fail-on-non-empty {} `;`

目录是否为空?

这些答案中的大多数都解释了如何检查目录是否为空。因此,我在这里提供我所知道的三种不同的技术:

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

    d=您的/目录
    if [ x$(find "$d" -prune -empty) = x"$d" ]
    然后
      echo“空(目录或文件)”
    别的
      echo“包含文件(或不存在)”
    菲
    

    变体:

    d=您的/目录
    if [ x$(find "$d" -prune -empty -type d) = x"$d" ]
    然后
      echo "空目录"
    别的
      echo“包含文件(或不存在或不是目录)”
    菲
    

    说明:

    • find -prunefind -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

  2. (( ${#files} ))

    这个技巧是 100% bash,但会调用(生成)一个子 shell。这个想法来自 Bruno De Fraine 并由 teambob 的评论。如果您使用 并且您的脚本不必是可移植的。

    files=$(shopt -s nullglob dotglob; echo your/dir/*)
    如果 (( ${#files} ))
    然后 
      echo“包含文件”
    别的 
      echo“空(或不存在或是一个文件)”
    菲
    

    注意:空目录和不存在的目录之间没有区别(即使提供的路径是文件)。

  3. [ $(ls -A your/dir) ]

    这个技巧的灵感来自nixCraft 的文章 于 2007 年发布。Andrew Taylor 于 2008 年回答,gr8can8dian 2011 年。

    if [ "$(ls -A your/dir)" ]
    然后
      echo“包含文件”
    别的
      echo“空(或不存在或是一个文件)”
    菲
    

    或者单行 bashism 版本:

    [[ "$(ls -A your/dir)" ]] && echo“包含文件”|| echo“空或...”
    

    注意: 当目录不存在时,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:

find -type d -empty

But -empty may not be available on very old find 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 command rmdir with the option --parents (or -p) that also removes parent directories when possible:

find -type d -empty -exec rmdir -vp --ignore-fail-on-non-empty {} +

On older find version the statement + is not yet supported, therefore you may use ; instead:

find -type d -empty -exec rmdir -vp --ignore-fail-on-non-empty {} `;`

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:

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

    d=your/dir
    if [ x$(find "$d" -prune -empty) = x"$d" ]
    then
      echo "empty (directory or file)"
    else
      echo "contains files (or does not exist)"
    fi
    

    a variation:

    d=your/dir
    if [ x$(find "$d" -prune -empty -type d) = x"$d" ]
    then
      echo "empty directory"
    else
      echo "contains files (or does not exist or is not a directory)"
    fi
    

    Explanation:

    • find -prune is similar than find -maxdepth 0 using less characters
    • find -type d prints directories only
    • find -empty prints the empty directories and files

      > 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
      
  2. (( ${#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 and if your script does not have to be portable.

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

    Note: no difference between an empty directory and a non-existing one (and even when the provided path is a file).

  3. [ $(ls -A your/dir) ]

    This trick is inspired from nixCraft's article posted in 2007. Andrew Taylor answered in 2008 and gr8can8dian in 2011.

    if [ "$(ls -A your/dir)" ]
    then
      echo "contains files"
    else
      echo "empty (or does not exist or is a file)"
    fi
    

    or the one-line bashism version:

    [[ "$(ls -A your/dir)" ]] && echo "contains files" || echo "empty or ..."
    

    Note: ls returns $?=2 when the directory does not exist. But no difference between a file and an empty directory.

呆° 2024-09-07 22:38:56

rmdir * 怎么样?该命令将在非空目录上失败。

How about rmdir *? That command will fail on non-empty directories.

故笙诉离歌 2024-09-07 22:38:56

这个递归函数似乎可以解决这个问题:

# Bash
findempty() {
    find ${1:-.} -mindepth 1 -maxdepth 1 -type d | while read -r dir
    do
        if [[ -z "$(find "$dir" -mindepth 1 -type f)" ]] >/dev/null
        then
            findempty "$dir"
            echo "$dir"
        fi
    done
}

给定这个示例目录结构:

    .
    |-- dir1/
    |-- dir2/
    |   `-- dirB/
    |-- dir3/
    |   `-- dirC/
    |       `-- file5
    |-- dir4/
    |   |-- dirD/
    |   `-- file4
    `-- dir5/
        `-- dirE/
            `-- dir_V/

运行该函数的结果将是:

    ./dir1
    ./dir5/dirE/dir_V
    ./dir5/dirE
    ./dir5
    ./dir2/dirB
    ./dir2

它错过了/dir4/dirD。如果将递归调用 findemty "$dir" 移到 fi 之后,该函数将在其结果中包含该目录。

This recursive function would seem to do the trick:

# Bash
findempty() {
    find ${1:-.} -mindepth 1 -maxdepth 1 -type d | while read -r dir
    do
        if [[ -z "$(find "$dir" -mindepth 1 -type f)" ]] >/dev/null
        then
            findempty "$dir"
            echo "$dir"
        fi
    done
}

Given this example directory structure:

    .
    |-- dir1/
    |-- dir2/
    |   `-- dirB/
    |-- dir3/
    |   `-- dirC/
    |       `-- file5
    |-- dir4/
    |   |-- dirD/
    |   `-- file4
    `-- dir5/
        `-- dirE/
            `-- dir_V/

The result of running that function would be:

    ./dir1
    ./dir5/dirE/dir_V
    ./dir5/dirE
    ./dir5
    ./dir2/dirB
    ./dir2

which misses /dir4/dirD. If you move the recursive call findempty "$dir" after the fi, the function will include that directory in its results.

逆光下的微笑 2024-09-07 22:38:56

如果目录为空(或不存在),以下命令将返回 1,否则返回 0(因此可以在 shell 脚本中使用 ! 反转返回代码):

find $dir -type d -prune -empty -exec false {} +

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

find $dir -type d -prune -empty -exec false {} +
零崎曲识 2024-09-07 22:38:56

我创建了一个简单的结构,如下所示:

test/
test/test2/
test/test2/test2.2/
test/test3/
test/test3/file

test/test3/file 包含一些垃圾文本。

发出 find test -empty 返回“test/test2/test2.2”作为唯一的空目录。

I created a simple structure as follows:

test/
test/test2/
test/test2/test2.2/
test/test3/
test/test3/file

The test/test3/file contains some junk text.

Issuing find test -empty returns "test/test2/test2.2" as the only empty directory.

遮了一弯 2024-09-07 22:38:56

一个简单的方法

$ [ "$(ls -A /path/to/direcory)" ] && echo "not empty" || echo "its empty"

也是,

if [ "$(ls -A /path/to/direcory)" ]; then
   echo "its not empty"
else 
   echo "empty directory"

a simple approach would be,

$ [ "$(ls -A /path/to/direcory)" ] && echo "not empty" || echo "its empty"

also,

if [ "$(ls -A /path/to/direcory)" ]; then
   echo "its not empty"
else 
   echo "empty directory"
老街孤人 2024-09-07 22:38:56
find . -name -type d -ls |awk '($2==0){print $11}'
find . -name -type d -ls |awk '($2==0){print $11}'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文