在bash中,我需要这样做:

取一个目录中的所有文件 将它们复制到现有目录中

我怎么做呢?我尝试了cp -r t1 t2 (t1和t2都是现有目录,t1中有文件),但它在t2中创建了一个名为t1的目录,我不想这样,我需要t1中的文件直接进入t2。我怎么做呢?


当前回答

cp -R t1/ t2

源目录上的尾随斜杠稍微改变了语义,因此它复制了内容,但不复制目录本身。它还避免了通配符和不可见文件的问题,Bertrand的答案(复制t1/*会遗漏不可见文件,复制' t1/* t1/。*'复制t1/。和t1 / . .这是你不想要的)。

其他回答

cp -R t1/ t2

源目录上的尾随斜杠稍微改变了语义,因此它复制了内容,但不复制目录本身。它还避免了通配符和不可见文件的问题,Bertrand的答案(复制t1/*会遗漏不可见文件,复制' t1/* t1/。*'复制t1/。和t1 / . .这是你不想要的)。

2021年11月更新:

下面的代码带标志“-R”,将“folder1”的所有内容完美地复制到现有的“folder2”:

cp -R folder1/. folder2

Flag "-R"也复制符号链接,但Flag "-R"跳过符号链接,因此Flag "-R"优于Flag "-R"。

最新的GNU Grep 3.7:

-R, --dereference-recursive

For each directory operand, read and process all files in that directory, 
recursively, following all symbolic links.
-r, --recursive

For each directory operand, read and process all files in that directory, 
recursively. Follow symbolic links on the command line, but skip symlinks 
that are encountered recursively. Note that if no file operand is given, 
grep searches the working directory. This is the same as the 
‘--directories=recurse’ option.

如果你想从一个目录复制一些东西到当前目录,这样做:

cp dir1/* .

这假设您没有试图复制隐藏文件。

对于在某些目录内,这将使用完整,因为它复制所有内容从“folder1”到新目录“folder2”在某些目录内。

$(pwd)将获取当前目录的路径。

注意folder1后面的点(.)表示folder1内的所有内容

cp -r $(pwd)/folder1/. $(pwd)/folder2

你想要的是:

cp -R t1/. t2/

末尾的圆点告诉它复制当前目录的内容,而不是目录本身。此方法还包括隐藏文件和文件夹。