首页 > 解决方案 > Difference between *.* and * when used with the cp command and the -R parameter

问题描述

I just want to find out what is the difference between

cp -R $rootpath/vgcore/core/src/geom/*.* $destpath/geom_src

,

cp -R $rootpath/vgcore/core/src/geom/* $destpath/geom_src

,

cp -R $rootpath/vgcore/core/src/geom $destpath/geom_src

and

cp -R $rootpath/vgcore/core/src/geom/ $destpath/geom_src

Let's say we have one subdir in geom, say alg with files in it

  1. In the case of one, would only all the files be copied from geom and alg and put into geom_src? So the dir structure of the source will be ignored?

  2. all the files from geom and all the files from alg + alg itself will be copied over retaining the subdir structure?

  3. sames as 2?

  4. sames as 2 & 3?

Sorry, don't have a test Linux machine handy to test this myself.

Thanks.

标签: linuxshellcommand-line

解决方案


cp -R $rootpath/vgcore/core/src/geom/ $destpath/geom_src

在 .../src/geom 中找到的每个名称中带有点的目录条目(文件和子目录)都将被复制到 .../geom_src

请注意,隐藏的条目(名称以点开头的条目)不会被复制,因为它们不可见。

cp -R $rootpath/vgcore/core/src/geom/* $destpath/geom_src

此命令与上述相同,但范围更广 - 将复制任何文件(或“目录条目”),而不仅仅是名称中包含点的文件。

cp -R $rootpath/vgcore/core/src/geom $destpath/geom_src

cp -R $rootpath/vgcore/core/src/geom/ $destpath/geom_src

是等价的,和以前不同。他们将单个对象(目录条目)复制到目标;单,因为没有指定通配符。以您的具体示例为例,最后两个命令将复制 .../geom/ 中的所有内容,与以前大致相同,但它们将再复制一次,即目录“geom”本身!并且 /geom/ 中的所有文件仍将位于目标中新创建的“geom”下(假设它尚不存在)。

如果你问这个,那么也许你来自dos或windows。在 dos/winslow 下,在执行命令之前没有扩展 - 解释通配符的是命令本身。相反,在 unix 下,参数是预先展开的:如果您碰巧在 .../src/geom/ 中有 200 个项目,则调用的程序(例如 cp)将接收 200 个参数。

另一个dos / unix的区别是您提到的点(* vs .)。DOS 使用由两个部分组成的文件名,这些部分由一个(有时是不可见的)点分隔。Unix 没有,点在文件名中没有特殊含义,除了以点开头的名称通常不会被 shell“看到”(因此被考虑)这一事实。


推荐阅读