查看子目录时,跳过一个目录

查看子目录时,跳过一个目录

我有一个包含这些子文件夹的目录/u01/oracle/folders

[root@ricusesasctl01vm tax_receipts]# ls -ltr
total 64
drwxr-xr-x 5 OICDev1 oic 4096 Mar  1  2021 Argentina
drwxr-xr-x 5 OICDev1 oic 4096 Mar  1  2021 Brazil
drwxr-xr-x 3 OICDev1 oic 4096 Mar  1  2021 completed
drwxr-xr-x 3 OICDev1 oic 4096 Mar  1  2021 duplicate
drwxr-xr-x 5 OICDev1 oic 4096 Mar  1  2021 EAO
drwxr-xr-x 3 OICDev1 oic 4096 Mar  1  2021 errored
drwxr-xr-x 5 OICDev1 oic 4096 Mar  1  2021 Japan
drwxr-xr-x 5 OICDev1 oic 4096 Mar  1  2021 Korea
drwxr-xr-x 5 OICDev1 oic 4096 Mar  1  2021 SAO
drwxr-xr-x 5 OICDev1 oic 4096 Mar  1  2021 SPPO
drwxr-xr-x 3 OICDev1 oic 4096 Mar  1  2021 temp
drwxr-xr-x 4 OICDev1 oic 4096 Mar  1  2021 template
drwxr-xr-x 3 OICDev1 oic 4096 Mar  1  2021 template2
drwxr-xr-x 5 OICDev1 oic 4096 Mar  1  2021 WHQ
drwxr-xr-x 5 OICDev1 oic 4096 May 10  2021 Canada
drwxr-xr-x 3 OICDev1 oic 4096 Jun  8  2021 canada

在 shell 脚本中,

SourceDirectory="/u01/oracle/folders"
TargetDirectory=/u01/oracle/folders"

对于 的值$SourceDirectory,我想列出除 SAO 之外的所有子文件夹。

在循环中使用此命令

#100 Loop through each directory (e.g. brazil, canada, uk)
#
for EachDir in "$SourceDirectory"*;
do
 strFiles=""
 echo "Current Directory is $EachDir"

我试过 :

SourceDirectory=$(find /u01/oracle/folders -maxdepth 1 -type d \( ! -name SAO \))

它会跳过目录 SAO,但输出是一长串。如何将此字符串拆分到目录中?

例子:

[root@ricusesasctl01vm tax_receipts]# SourceDirectory=$(find /u01/oracle/folders -maxdepth 1 -type d \( ! -name SAO \))
[root@ricusesasctl01vm tax_receipts]# echo $SourceDirectory
/u01/oracle/folders /u01/oracle/folders/duplicate /u01/oracle/folders/Brazil /u01/oracle/folders/completed /u01/oracle/folders/template2 /u01/oracle/folders/Canada /u01/oracle/folders/SPPO /u01/oracle/folders/template /u01/oracle/folders/WHQ /u01/oracle/folders/EAO /u01/oracle/folders/errored /u01/oracle/folders/Korea /u01/oracle/folders/Japan /u01/oracle/folders/Argentina /u01/oracle/folders/temp /u01/oracle/folders/canada

答案1

find /u01/oracle/folders -maxdepth 1 -type d \( ! -name SAO \) | while read SourceDirectory
do
  echo "$SourceDirectory"
  ls "$SourceDirectory"
done

答案2

您可以轻松地SAO在简单的 shell 循环中跳过该目录:

SourceDirectory=/u01/oracle/folders

for dirpath in "$SourceDirectory"/*/; do
    case $dirpath in
        */SAO/) continue ;;
    esac

    # Process "$dirpath" in whatever way is needed.
done

使用bash,您可以将 替换case ... esac

[[ $dirpath == */SAO/ ]] && continue

无论哪种情况,此代码都使用文件名通配模式测试来检查 中的字符串是否$dirpath匹配*/SAO/,如果它以文字字符串 结尾,则会执行此操作/SAO/

另一种方法bash是在循环中使用扩展的通配模式:

shopt -s extglob

SourceDirectory=/u01/oracle/folders

for dirpath in "$SourceDirectory"/!(SAO)/; do
    # Process "$dirpath" in whatever way is needed.
done

用于循环的模式将匹配 中的所有目录$SourceDirectory,除了任何名为 的目录SAO。为了能够在bashshell 中使用扩展的通配模式,需要通过 启用它们shopt -s extglob

我不会find在这里使用,因为我们确切地知道我们要跳过哪个目录。

如果你感觉你真的需要要做到这一点find,我建议这样做:

SourceDirectory=/u01/oracle/folders

find "$SourceDirectory" ! -path "$SourceDirectory" -prune \
    -type d ! -name SOA -exec sh -c '
    for dirpath do
        # Process "$dirpath" in whatever way is needed.
    done' sh {} +

或者,使用 GNU find

SourceDirectory=/u01/oracle/folders

find "$SourceDirectory" -mindepth 1 -maxdepth 1 \
    -type d ! -name SOA -exec sh -c '
    for dirpath do
        # Process "$dirpath" in whatever way is needed.
    done' sh {} +

这用作find要迭代的字符串生成器。这些字符串是 下所有目录的路径名$SourceDirectory,除了该SOA目录。

您想要执行的处理将通过sh -c命令行上提供的内联脚本完成。

相关内容