简体   繁体   English

Bash:将目录路径不包含特定字符串的所有目录移动

[英]Bash: Move all directories where the directory path does not contain a particular string

Let's say I have a directory with two subdirectories A and G. Directory A contains two subdirectories (B and C). 假设我有一个包含两个子目录A和G的目录。目录A包含两个子目录(B和C)。 Directory B also contains two subdirectories (D and E). 目录B还包含两个子目录(D和E)。 Finally, directory D contains directory F. 最后,目录D包含目录F。

.
├── A
│   ├── B
│   │   ├── D
│   │   │   └── F
│   │   └── E
│   └── C
└── G

Now, I want to move all directories where their path does not contain B/D to directory G. Assuming I'm in the top directory where A and G exist, I'm trying this command: 现在,我想将其路径不包含B / D的所有目录移动到目录G。假设我位于A和G所在的顶层目录中,我正在尝试以下命令:

    $shopt -s extglob
    $mv !(*B/D*) ./G

But I'm getting this error: mv: rename !(*B/D*) to ./G/*: No such file or directory . 但是我收到此错误: mv: rename !(*B/D*) to ./G/*: No such file or directory Any ideas? 有任何想法吗? I'm also open to other ways of solving the problem. 我也欢迎其他解决问题的方法。

Using find : 使用find

find A/ -depth -type d -not \( -name *A* -o -name *B* -o -name *D* \) -exec mv -t G/ {} +

Example: 例:

$ tree
.
├── A
│   ├── B
│   │   ├── D
│   │   │   └── F
│   │   └── E
│   └── C
└── G

$ find A/ -depth -type d -not \( -name *A* -o -name *B* -o -name *D* \) -exec mv -t G/ {} +

$ tree
.
├── A
│   └── B
│       └── D
└── G
    ├── C
    ├── E
    └── F

If your mv does not have -t option: 如果您的mv没有-t选项:

find A/ -depth -type d -not \( -name *A* -o -name *B* -o -name *D* \) -exec mv {} G/ \;

这是正确的答案:

find A/ -depth -type d -not \( -wholename "*B" \) -not \( -wholename "*B/D*" \) -exec sh -c 'mkdir -p G/`dirname "{}"`; mv "{}" G/`dirname "{}"`' \;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM