删除跨目录的文件夹

huangapple go评论54阅读模式
英文:

Deleting folders across directories

问题

我有这样的目录结构:

... / firstlevel / output / sub-(101-162) / ...

在每个子文件夹 sub-XXX 中,我有 run_1、run_1.feat、run_1+.feat、run_1++.feat、run_2、run_2.feat、run_2+.feat 和 run_2++.feat 这些文件夹。

有没有一条命令可以让我保留所有子文件夹 sub-XXX 中的 run_1++.feat 和 run_2++.feat 文件夹,并删除 run_1、run_1.feat、run_1+.feat、run_2、run_2.feat、run_2+.feat?

谢谢!

英文:

I have a directory structure like this:

... / firstlevel / output / sub-(101-162) / ...

In each sub-XXX folder, I have the folders run_1, run_1.feat, run_1+.feat, run_1++.feat, run_2, run_2.feat, run_2+.feat run_2++.feat.

Is there a one-line command I can run to keep only the run_1++.feat and run_2++.feat folders for all sub-XXX folders?

In other words, for each sub-XXX folder, I want to keep the folders run_1++.feat and run_2++.feat and delete run_1, run_1.feat, run_1+.feat, run_2, run_2.feat, run_2+.feat.

Thanks!

答案1

得分: 1

正面解释

如果您想删除所有名为 run_1run_1.featrun_1.+featrun_2run_2.featrun_2.+feat 的子目录(但保留 firstlevel/output/sub-101/foo/),您可以尝试以下命令:

ls firstlevel/output/sub-{101..162}/run_{1,2}{,{,+}.feat}/

如果列表看起来正确,请将 ls 替换为 rm -rf。注意:末尾的 / 防止删除常规文件,只会匹配目录。可能会出现目录删除列表过长导致 Argument list too long 错误的情况。在这种情况下,您可以尝试使用 find

find firstlevel/output/sub-{101..162} -maxdepth 1 -type d \
\( -name run_[12] -o -name run_[12].feat -o -name run_[12]+.feat \) -print

确保确定后,将 -print 替换为 -delete。如果参数列表仍然过长,可以尝试将 101..162 范围拆分为多个较小的范围:

find firstlevel/output/sub-{101..131} -maxdepth 1 -type d \
\( -name run_[12] -o -name run_[12].feat -o -name run_[12]+.feat \) -print
find firstlevel/output/sub-{132..162} -maxdepth 1 -type d \
\( -name run_[12] -o -name run_[12].feat -o -name run_[12]+.feat \) -print
负面解释

如果您想删除所有子目录,不包括 名为 run_1++.featrun_2++.feat 的目录(包括 firstlevel/output/sub-101/foo/),您可以尝试以下命令:

( shopt -s extglob; ls firstlevel/output/sub-{101..162}/!(run_[12]++.feat)/ )

extglob 选项启用了扩展的模式匹配运算符,如 !(pattern),它匹配除了 pattern 之外的任何内容。在子shell(周围的括号)中运行命令列表可以限制 extglob 的作用范围。同样,如果列表看起来正确,请将 ls 替换为 rm -rf。同样,如果列表太长,您可以尝试使用 find

find firstlevel/output/sub-{101..162} -maxdepth 1 -type d \
! -name run_[12]++.feat -print

确信后,将 -print 替换为 -delete

英文:

There are 2 possible interpretations of your question depending whether you want to delete a sub-directory that would be named firstlevel/output/sub-101/foo/, or not.

Positive interpretation

If you want to delete all sub-directories named run_1, run_1.feat, run_1.+feat, run_2, run_2.feat or run_2.+feat (but keep firstlevel/output/sub-101/foo/), you can try:

ls firstlevel/output/sub-{101..162}/run_{1,2}{,{,+}.feat}/

If the list looks correct replace ls with rm -rf. Note: the trailing / prevents deletion of regular files, only directories will match. It could be that the list of directories to delete is too long and that you get an Argument list too long error. In this case you can try find:

find firstlevel/output/sub-{101..162} -maxdepth 1 -type d \
\( -name run_[12] -o -name run_[12].feat -o -name run_[12]+.feat \) -print

When convinced replace -print with -delete. If the argument list is still too long try to split the 101..162 range in several smaller ones:

find firstlevel/output/sub-{101..131} -maxdepth 1 -type d \
\( -name run_[12] -o -name run_[12].feat -o -name run_[12]+.feat \) -print
find firstlevel/output/sub-{132..162} -maxdepth 1 -type d \
\( -name run_[12] -o -name run_[12].feat -o -name run_[12]+.feat \) -print
Negative interpretation

If you want to delete all sub-directories not named run_1++.feat or run_2++.feat (including firstlevel/output/sub-101/foo/), you can try:

( shopt -s extglob; ls firstlevel/output/sub-{101..162}/!(run_[12]++.feat)/ )

The extglob option enables extended pattern matching operators, like !(pattern) that match anything except pattern. Running the list of commands in a subshell (the surrounding parentheses) limits the scope of extglob. Again, if the list looks correct replace ls with rm -rf. And again, if the list is too long you can try find:

find firstlevel/output/sub-{101..162} -maxdepth 1 -type d \
! -name run_[12]++.feat -print

When convinced replace -print with -delete.

huangapple
  • 本文由 发表于 2023年6月1日 13:34:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76378912.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定