Linux 如何使用“mv”命令移动特定目录中的文件以外的文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4612157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use 'mv' command to move files except those in a specific directory?
提问by David Liu
I am wondering - how can I move all the files in a directory except those files in a specific directory (as 'mv' does not have a '--exclude' option)?
我想知道 - 如何移动目录中除特定目录中的那些文件之外的所有文件(因为“mv”没有“--exclude”选项)?
采纳答案by Chathura Kulasinghe
Lets's assume the dir structure is like,
让我们假设 dir 结构是这样的,
|parent
|--child1
|--child2
|--grandChild1
|--grandChild2
|--grandChild3
|--grandChild4
|--grandChild5
|--grandChild6
And we need to move files so that it would appear like,
我们需要移动文件,使其看起来像,
|parent
|--child1
| |--grandChild1
| |--grandChild2
| |--grandChild3
| |--grandChild4
| |--grandChild5
| |--grandChild6
|--child2
In this case, you need to exclude two directories child1
and child2
, and move rest of the directories in to child1
directory.
在这种情况下,您需要排除两个目录child1
and child2
,并将其余目录移动到child1
目录中。
use,
用,
mv !(child1|child2) child1
This will move all of rest of the directories into child1
directory.
这会将所有其余目录移动到child1
目录中。
回答by Thanatos
This isn't exactly what you asked for, but it might do the job:
这不完全是您所要求的,但它可能会完成这项工作:
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
(Essentially, move the excluded folder out of the larger tree to be moved.)
(本质上,将排除的文件夹从要移动的较大树中移出。)
So, if I have a/
and I want to exclude a/b/c/*
:
所以,如果我有a/
并且我想排除a/b/c/*
:
mv a/b/c ../c
mv a final_destination
mkdir -p a/b
mv ../c a/b/c
Or something like that. Otherwise, you might be able to get find
to help you.
或类似的东西。否则,您可能会得到find
帮助。
回答by Slartibartfast
Since find does have an exclude option, use find + xargs + mv:
由于 find 确实有一个排除选项,请使用 find + xargs + mv:
find /source/directory -name ignore-directory-name -prune -print0 | xargs -0 mv --target-directory=/target/directory
Note that this is almost copied from the find man page (I think using mv --target-directory is better than cpio).
请注意,这几乎是从 find 手册页复制的(我认为使用 mv --target-directory 比 cpio 更好)。
回答by sjr
This will move all files at or below the current directory not in the ./exclude/ directory to /wherever...
这会将不在 ./exclude/ 目录中的当前目录或低于当前目录的所有文件移动到 /wherever...
find -E . -not -type d -and -not -regex '\./exclude/.*' -exec echo mv {} /wherever \;
回答by Chris Reid
#!/bin/bash
touch apple banana carrot dog cherry
mkdir fruit
F="apple banana carrot dog cherry"
mv ${F/dog/} fruit
# this removes 'dog' from the list F, so it remains in the current directory and not moved to 'fruit'
# 这将从列表 F 中删除 'dog',因此它保留在当前目录中而不移动到 'fruit'
回答by xyz
ls | grep -v exclude-dir | xargs -t -I '{}' mv {} exclude-dir
回答by Felix K?rner
mv * exclude-dir
was the perfect solution for me
对我来说是完美的解决方案