【问题标题】:Ignore all the folders that have sub folders in it using shell script使用 shell 脚本忽略所有包含子文件夹的文件夹
【发布时间】:2021-03-15 23:49:39
【问题描述】:

我有一个由许多文件夹组成的文件夹,其中一些只有文件,而另一些则有子文件夹,其中还有一些文件。 我想编写一个 shell 脚本来忽略所有包含子文件夹的文件夹以及其中包含的任何内容

示例:此目录:/tmp/orders-temp 有以下文件:

drwxr-xr-x 2 root root 4096 Dec 3 18:38 201201040230_abcd.1of3

它带有 zip 和 xml 文件

drwxr-xr-x 2 root root 4096 Dec 3 18:38 201201040230_abcd.2of3

它带有 zip 和 xml 文件

drwxr-xr-x 2 root root 4096 Dec 3 18:38 201201040230_abcd.3of3

它带有 zip 和 xml 文件

drwxr-xr-x 5 root root 4096 Dec 3 18:53 201201040230_abcd_90975

它有一些子目录

我们只希望 201201040230_abcd_90975 被忽略,因为它包含更多的子文件夹,即:

cd 201201040230_abcd_90975

ls -rlt

drwxr-xr-x 2 root root 4096 Dec 3 18:42 201201040230_abcd-01

drwxr-xr-x 2 root root 4096 Dec 3 18:42 201201040230_abcd-02

drwxr-xr-x 2 root root 4096 Dec 3 18:42 201201040230_abcd-03

我们需要忽略所有这些。

即输出应该是:

drwxr-xr-x 2 root root 4096 Dec 3 18:38 201201040230_abcd.1of3

drwxr-xr-x 2 root root 4096 Dec 3 18:38 201201040230_abcd.2of3

drwxr-xr-x 2 root root 4096 Dec 3 18:38 201201040230_abcd.3of3

此外,我们下次可以为文件夹取一个不同的名称,因此自动化应该是动态的,无需硬编码。

感谢任何帮助!

【问题讨论】:

  • 打印不带参数输入find的数据结构。
  • edit您的问题并解释您想要实现的目标。 “忽略所有包含子文件夹的文件夹以及其中包含的任何内容”是什么意思?你想对所有其他文件夹做些什么,即那些既不包含子文件夹也不包含文件的文件夹?

标签: linux bash shell


【解决方案1】:

假设你有一个目录树:

/tmp
└── orders-temp
    ├── 201201040230_abcd.1of3
    │   ├── bar1.xml
    │   └── foo1.zip
    ├── 201201040230_abcd.2of3
    │   ├── bar2.xml
    │   └── foo2.zip
    ├── 201201040230_abcd.3of3
    │   ├── bar3.xml
    │   └── foo3.zip
    └── 201201040230_abcd_90975
        ├── 201201040230_abcd-01
        │   └── foo4.zip
        ├── 201201040230_abcd-02
        │   └── foo5.zip
        └── 201201040230_abcd-03
            └── foo6.zip

如您所见,201201040230_abcd_90975 目录中包含子目录。
那么怎么样:

#!/bin/bash

dir=$1                  # assign to the target directory

while IFS= read -d "" dir; do
    if [[ -z $(find "$dir" -type d -mindepth 1) ]]; then
                        # if the directory has no subdirectories in it ...
        echo "$dir"     # or do something here
    fi
done < <(find "$dir" -type d -maxdepth 1 -mindepth 1 -print0)
                        # find directories in "$dir" of level one depth

结果:

$ ./this_script /tmp/orders-temp/

/tmp/orders-temp/201201040230_abcd.1of3
/tmp/orders-temp/201201040230_abcd.2of3
/tmp/orders-temp/201201040230_abcd.3of3

【讨论】:

    【解决方案2】:
    tree ./work_dir                          
    work_dir
    ├── 1.txt
    ├── 2.txt
    ├── 3.txt
    └── next_folder
        ├── 1.txt
        ├── 2.txt
        └── 3.txt
    
    1 directory, 6 files
    

    vim find_files.sh

    #!/usr/bin/env bash
    
    WORK_DIR=~/work_dir/
    
    find $WORK_DIR -maxdepth 1 -type f
    

    保存文件并:

    chmod +x find_files.sh && ./find_files.sh
    
    /home/user/work_dir/1.txt
    /home/user/work_dir/3.txt
    /home/user/work_dir/2.txt
    

    -maxdepth 2 也会找到 next_folder 中的所有文件。

    如果你想消除根目录,但打印所有其他(嵌套的):

    #!/usr/bin/env bash
    
    WORK_DIR=~/work_dir/
    FOLDERS_TO_SEARCH=$(find $WORK_DIR -maxdepth 1 -type d)
    
    count=0
    for i in $FOLDERS_TO_SEARCH
    do
            count=$(($count+1))
            if [[ $count -eq 1 ]];then
                    echo "Skipping root directory: $i"
            else
                    find $i -maxdepth 1 -type f
            fi
    done
    

    【讨论】:

    • maxdepth 1 给出了我不想要的文件夹,我试过了,但在我的情况下似乎不起作用,即它给出 3.txt └── next_folder ├── 1.txt ├── 2.txt └── 3.txt 我不想要。终端输出: find 。 -maxdepth 1 -类型 d 。 ./abcd_BB-Clien_hisense_P.2of3 ./abcd_BB-Clien_hisense_P.1of3 ./abcd_BB-Clien_hisense_P.3of3 ./abcd_BB-Clien_hisense_P_90975我只是想:./201201040230_BB-Clien_hisense_P.2of3 ./201201040230_BB-Clien_hisense_P.1of3 ./201201040230_BB-Clien_hisense_P。 3of3 和这个我不想要的文件夹:./abcd_BB-Clien_hisense_P_90975
    • 添加了另一个如何消除根目录的示例。
    猜你喜欢
    • 2015-12-25
    • 2019-07-14
    • 2014-01-14
    • 2021-12-22
    • 2014-05-06
    • 2015-11-02
    • 1970-01-01
    • 2017-10-27
    • 1970-01-01
    相关资源
    最近更新 更多