我想知道 - 我如何移动目录中的所有文件,除了特定目录中的那些文件(因为'mv'没有'--exclude'选项)?
问问题
93561 次
9 回答
94
让我们假设 dir 结构是这样的,
|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
目录中。
于 2013-08-09T09:18:57.267 回答
5
由于 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 更好)。
于 2011-01-06T06:08:00.043 回答
2
这不完全是您所要求的,但它可能会完成这项工作:
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
帮助。
于 2011-01-06T05:56:14.693 回答
2
首先获取文件和文件夹的名称并排除您想要的任何一个:
ls --ignore=file1 --ignore==folder1 --ignore==regular-expression1 ...
然后将过滤后的名称mv
作为第一个参数传递给,第二个参数将是目标:
mv $(ls --ignore=file1 --ignore==folder1 --ignore==regular-expression1 ...) destination/
于 2020-09-05T05:25:58.687 回答
1
这会将当前目录或其下方不在 ./exclude/ 目录中的所有文件移动到 /wherever...
find -E . -not -type d -and -not -regex '\./exclude/.*' -exec echo mv {} /wherever \;
于 2011-01-06T06:10:31.063 回答
0
#!/bin/bash
touch apple banana carrot dog cherry
mkdir fruit
F="apple banana carrot dog cherry"
mv ${F/dog/} fruit
# 这会从列表 F 中删除 'dog',所以它保留在当前目录中,而不是移动到 'fruit'
于 2019-04-10T23:41:08.797 回答
0
ls | grep -v exclude-dir | xargs -t -I '{}' mv {} exclude-dir
于 2019-05-20T06:10:51.170 回答
0
重命名您的目录以使其隐藏,以便通配符看不到它:
mv specific_dir .specific_dir
mv * ../other_dir
于 2021-05-01T00:10:14.597 回答
0
mv * exclude-dir
对我来说是完美的解决方案
于 2020-01-25T14:40:28.400 回答