如何使用“cp -R”将所有文件复制到子文件夹而不复制子文件夹?

如何使用“cp -R”将所有文件复制到子文件夹而不复制子文件夹?

我有一个图像文件夹,我试图将其复制到其自身的子文件夹中,以便我可以调整其中的图像大小。

它看起来像这样:

$ ls
100x100                                 image1.png
image2.png                              image3.png
image4.png                              pic1.jpg

我遇到的问题是,当我运行命令时cp -R ./. 100x100,出现错误,基本上表示我无法继续向自身添加 100x100 文件夹:

cp: 100x100/./100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/100x100/.DS_Store: name too long (not copied)

如何cp -R将文件复制到子文件夹而不同时复制子文件夹?

答案1

如果您的 shell 是zsh(如果您使用的是 macos):

set -o extendedglob # best in ~/.zshrc
cp -- {^,}100x100

将扩展到:

cp -- ^100x100 100x100

也就是说,将所有文件复制100x100100x100.

同时复制隐藏文件:

cp -- ^100x100(D) 100x100

仅复制常规文件(也包括目录、符号链接等):

cp -- ^100x100(D.) 100x100

答案2

使用cp ./*.* ./100x100 没有-R选项会将目录中的所有文件复制到100x100子目录中,而不将目录复制到其自身中。

-R选项用于递归地将所有子目录复制到目标中,如果您不希望发生这种情况,则不应使用该选项。

man cp有关该命令的选项及其用途的更多信息,请参阅 参考资料。

相关内容