0

I have an image folder which I am trying to copy into a subfolder of itself, so I can resize the images there.

It looks something like this:

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

The issue I have is when I run the command cp -R ./. 100x100, I get an error, which basically says I can't keep adding 100x100 folders to themselves:

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)

How do I use cp -R to copy files to a subfolder without also copying the subfolder?

αғsнιη
  • 40,939
  • 15
  • 71
  • 114
Seph Reed
  • 183
  • 1
  • 11

2 Answers2

2

If your shell is zsh (likely if you're on macos):

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

Would expand to:

cp -- ^100x100 100x100

That is, copy all files but 100x100 into 100x100.

To also copy hidden files:

cp -- ^100x100(D) 100x100

To only copy regular files (nor directories, symlinks, etc):

cp -- ^100x100(D.) 100x100
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
1

Using cp ./*.* ./100x100 without the -R option will copy all files in the directory into the 100x100 subdirectory without copying the directory into itself.

The -R option is for recursively copying all subdirectories into the target and should not be used if you do not want that to happen.

See man cp for more information on the options for the command and what they do.

Mio Rin
  • 2,940
  • 14
  • 17
  • `cp ./*.* ./100x100` without the `-R` option will copy all files _with a dot in the filename_... If you meant what you wrote you would use `cp ./* 100x100` – roaima Dec 31 '17 at 09:05
  • All image files with a type extension have a dot in the filename, while directories (and other, possibly broken, files) do not. That's why it works. ;) – Mio Rin Dec 31 '17 at 10:07
  • My point is that `*.*` does not match _all files_, as you have written. – roaima Dec 31 '17 at 15:19
  • This wouldn't copy other directories either. – jdwolf Dec 31 '17 at 17:32