【发布时间】:2011-06-04 11:20:09
【问题描述】:
我想知道 - 我如何移动目录中的所有文件,除了特定目录中的文件(因为 'mv' 没有 '--exclude' 选项)?
【问题讨论】:
我想知道 - 我如何移动目录中的所有文件,除了特定目录中的文件(因为 'mv' 没有 '--exclude' 选项)?
【问题讨论】:
让我们假设目录结构是这样的,
|parent
|--child1
|--child2
|--grandChild1
|--grandChild2
|--grandChild3
|--grandChild4
|--grandChild5
|--grandChild6
我们需要移动文件,使其看起来像,
|parent
|--child1
| |--grandChild1
| |--grandChild2
| |--grandChild3
| |--grandChild4
| |--grandChild5
| |--grandChild6
|--child2
在这种情况下,您需要排除两个目录child1 和child2,并将其余目录移动到child1 目录中。
使用,
mv !(child1|child2) child1
这会将所有其余目录移动到child1 目录中。
【讨论】:
extglob。您可以使用shopt -s extglob 启用它(如果您希望默认打开extended globs,您可以将shopt -s extglob 添加到.bashrc)
-bash: !: event not found。
mv -f 似乎不起作用。我通过创建一个临时目录解决了这个问题,所以总共有三个目录,然后移动所有目录,然后删除临时目录。丑!
由于 find 确实有排除选项,请使用 find + xargs + mv:
find /source/directory -name ignore-directory-name -prune -print0 | xargs -0 mv --target-directory=/target/directory
请注意,这几乎是从 find 手册页中复制的(我认为使用 mv --target-directory 比 cpio 更好)。
【讨论】:
这并不完全符合您的要求,但它可能会完成这项工作:
mv the-folder-you-want-to-exclude somewhere-outside-of-the-main-tree
mv the-tree where-you-want-it
mv the-excluded-folder original-location
(本质上,将排除的文件夹移出要移动的较大树。)
所以,如果我有 a/ 并且我想排除 a/b/c/*:
mv a/b/c ../c
mv a final_destination
mkdir -p a/b
mv ../c a/b/c
或者类似的东西。否则,您也许可以得到find 的帮助。
【讨论】:
首先获取文件和文件夹的名称并排除您想要的:
ls --ignore=file1 --ignore==folder1 --ignore==regular-expression1 ...
然后将过滤后的名称作为第一个参数传递给mv,第二个参数将是目标:
mv $(ls --ignore=file1 --ignore==folder1 --ignore==regular-expression1 ...) destination/
【讨论】:
这会将当前目录下的所有文件移动到/exclude/目录下的所有文件到/wherever...
find -E . -not -type d -and -not -regex '\./exclude/.*' -exec echo mv {} /wherever \;
【讨论】:
unknown predicate '-E'
#!/bin/bash
touch apple banana carrot dog cherry
mkdir fruit
F="apple banana carrot dog cherry"
mv ${F/dog/} fruit
# 这会从列表 F 中删除 'dog',所以它仍保留在 当前目录而不是移动到'fruit'
【讨论】:
ls | grep -v exclude-dir | xargs -t -I '{}' mv {} exclude-dir
【讨论】:
mv * exclude-dir
对我来说是完美的解决方案
【讨论】:
重命名您的目录以使其隐藏,这样通配符就看不到它:
mv specific_dir .specific_dir
mv * ../other_dir
【讨论】: