将目录中的所有文件复制到linux中的本地子目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9842633/
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
Copy all files in a directory to a local subdirectory in linux
提问by GSto
I have a directory with the following structure:
我有一个具有以下结构的目录:
file_1
file_2
dir_1
dir_2
# etc.
new_subdir
I'd like to make a copy of all the existing files and directories located in this directory in new_subdir
. How can I accomplish this via the linux terminal?
我想将位于此目录中的所有现有文件和目录复制到 new_subdir
. 如何通过 linux 终端完成此操作?
回答by Shahbaz
You mean like
你的意思是喜欢
cp -R * new_subdir
?
?
cp
take -R
as argument which means recursive (so, copy also directories), *
means all files (and directories).
cp
采取-R
的参数,它的手段递归(所以,也复制目录),*
意味着所有的文件(和目录)。
Although *
includes new_subdir
itself, but cp
detects this case and ignores new_subdir
(so it doesn't copy it into itself!)
虽然*
包含new_subdir
自身,但cp
检测到这种情况并忽略new_subdir
(因此它不会将其复制到自身中!)
回答by Bogdan Emil Mariesan
Try something like:
尝试类似:
cp -R * /path_to_new_dir/
回答by Eihiko
This is an old question, but none of the answers seem to work (they cause the destination folder to be copied recursively into itself), so I figured I'd offer up some working examples:
这是一个老问题,但似乎没有一个答案有效(它们导致目标文件夹被递归复制到自身中),所以我想我会提供一些工作示例:
Copy via find -exec:
通过 find -exec 复制:
find . ! -regex '.*/new_subdir' ! -regex '.' -exec cp -r '{}' new_subdir \;
find . ! -regex '.*/new_subdir' ! -regex '.' -exec cp -r '{}' new_subdir \;
This code uses regex to find all files and directories (in the current directory) which are not new_subdir and copies them into new_subdir. The ! -regex '.'
bit is in there to keep the current directory itself from being included. Using find is the most powerful technique I know, but it's long-winded and a bit confusing at times.
此代码使用正则表达式查找所有不是 new_subdir 的文件和目录(在当前目录中)并将它们复制到 new_subdir 中。该! -regex '.'
位在那里以防止包含当前目录本身。使用 find 是我所知道的最强大的技术,但它冗长且有时令人困惑。
Copy with extglob:
用 extglob 复制:
cp -r !(new_subdir) new_subdir
cp -r !(new_subdir) new_subdir
If you have extglob enabled for your bash terminal (which is probably the case), then you can use ! to copy all things in the current directory which are not new_subdir into new_subdir.
如果您为 bash 终端启用了 extglob(可能是这种情况),那么您可以使用 ! 将当前目录中所有不是 new_subdir 的内容复制到 new_subdir 中。
Copy without extglob:
没有 extglob 的复制:
mv * new_subdir ; cp -r new_subdir/* .
mv * new_subdir ; cp -r new_subdir/* .
If you don't have extglob and find doesn't appeal to you and you really want to do something hacky, you can move all of the files into the subdirectory, then recursively copy them back to the original directory. Unlike cp which copies the destination folder into itself, mv just throws an error when it tries to move the destination folder inside of itself. (But it successfully moves every other file and folder.)
如果您没有 extglob 并且 find 对您没有吸引力,并且您真的想做一些 hacky,则可以将所有文件移动到子目录中,然后递归地将它们复制回原始目录。与将目标文件夹复制到自身中的 cp 不同, mv 在尝试将目标文件夹移动到自身内部时只会引发错误。(但它成功地移动了所有其他文件和文件夹。)