bash 将文件从一个目录复制到现有目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3643848/
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 files from one directory into an existing directory
提问by David Chang
In bash I need to do this:
在 bash 中,我需要这样做:
take all files in a directory
copy them into an existing directory
获取目录中的所有文件
将它们复制到现有目录中
How do I do this? I tried cp -r t1 t2
(both t1 and t2 are existing directories, t1 has files in it) but it created a directory called t1 inside t2, I don't want that, I need the files in t1 to go directly inside t2. How do I do this?
我该怎么做呢?我试过cp -r t1 t2
(t1 和 t2 都是现有目录,t1 中有文件)但它在 t2 中创建了一个名为 t1 的目录,我不想要那个,我需要 t1 中的文件直接进入 t2 内部。我该怎么做呢?
回答by Nick
What you want is:
你想要的是:
cp -R t1/. t2/
The dot at the end tells it to copy the contents of the current directory, not the directory itself. This method also includes hidden files and folders.
最后的点告诉它复制当前目录的内容,而不是目录本身。此方法还包括隐藏文件和文件夹。
回答by Karl Giesing
If you want to copy something from one directory into the currentdirectory, do this:
如果要将某个目录中的某些内容复制到当前目录中,请执行以下操作:
cp dir1/* .
This assumes you're not trying to copy hidden files.
这假设您没有尝试复制隐藏文件。
回答by Bertrand Marron
cp dir1/* dir2
Or if you have directories inside dir1 that you'd want to copy as well
或者,如果您在 dir1 中有想要复制的目录
cp -r dir1/* dir2
回答by Konkret
Assuming t1 is the folder with files in it, and t2 is the empty directory. What you want is something like this:
假设 t1 是包含文件的文件夹,而 t2 是空目录。你想要的是这样的:
sudo cp -R t1/* t2/
Bear in mind, for the first example, t1 and t2 have to be the full paths, or relative paths (based on where you are). If you want, you can navigate to the empty folder (t2) and do this:
请记住,对于第一个示例,t1 和 t2 必须是完整路径或相对路径(基于您所在的位置)。如果需要,您可以导航到空文件夹 (t2) 并执行以下操作:
sudo cp -R t1/* ./
Or you can navigate to the folder with files (t1) and do this:
或者您可以导航到包含文件 (t1) 的文件夹并执行以下操作:
sudo cp -R ./* t2/
Note: The * sign (or wildcard) stands for all files and folders. The -R flag means recursively (everything inside everything).
注意:* 符号(或通配符)代表所有文件和文件夹。-R 标志表示递归(所有内容中的所有内容)。
回答by Gordon Davisson
cp -R t1/ t2
The trailing slash on the source directory changes the semantics slightly, so it copies the contents but not the directory itself. It also avoids the problems with globbing and invisible files that Bertrand's answer has (copying t1/*
misses invisible files, copying `t1/* t1/.*' copies t1/. and t1/.., which you don't want).
源目录的尾部斜杠稍微改变了语义,因此它复制内容而不是目录本身。它还避免了 Bertrand 的答案所具有的 globbing 和不可见文件的问题(复制t1/*
错过了不可见文件,复制了 t1/* t1/.* 复制了 t1/. 和 t1/..,这是您不想要的)。
回答by Jim Dennis
Depending on some details you might need to do something like this:
根据某些细节,您可能需要执行以下操作:
r=$(pwd)
case "$TARG" in
/*) p=$r;;
*) p="";;
esac
cd "$SRC" && cp -r . "$p/$TARG"
cd "$r"
... this basically changes to the SRC directory and copies it to the target, then returns back to whence ever you started.
...这基本上会更改到 SRC 目录并将其复制到目标,然后返回到您开始的地方。
The extra fussing is to handle relative or absolute targets.
额外的麻烦是处理相对或绝对目标。
(This doesn't rely on subtle semantics of the cp
command itself ... about how it handles source specifications with or without a trailing / ... since I'm not sure those are stable, portable, and reliable beyond just GNU cp
and I don't know if they'll continue to be so in the future).
(这不依赖于cp
命令本身的微妙语义......关于它如何处理带有或不带有尾随 / 的源规范......因为我不确定除了 GNUcp
和我之外,那些是稳定的、可移植的和可靠的不知道以后会不会一直这样)。
回答by ashish Kumar
For inside some directory, this will be use full as it copy all contents from "folder1" to new directory "folder2" inside some directory.
对于某些目录内部,这将使用 full,因为它将所有内容从“folder1”复制到某个目录中的新目录“folder2”。
$(pwd) will get path for current directory.
$(pwd) 将获取当前目录的路径。
Notice the dot (.) after folder1 to get all contents inside folder1
注意文件夹 1 后面的点 (.) 以获取文件夹 1 中的所有内容
cp -r $(pwd)/folder1/. $(pwd)/folder2