I need to copy all files from one UNIX path to another within the same server. However, there are some .zip files I want to exclude while copying. How can I achieve this using the cp command options?

  • are you copying recursively (-r)? – Jeff Schaller Jun 20 at 16:30
  • What shell are you using? I have a feeling zsh would be able to do this easier than some others. – Jesse_b Jun 20 at 16:30
  • 2
    Please clarify: 1) Is this on a UNIX system, or a Linux system? 2) Which OS are you using? 3) Which shell are you using? 4) Do you wish to copy files which are in subdirectories of the source, or just the source directory? 5) Since the cp command itself cannot do this alone, is it satisfactory to you other common shell commands? Please do NOT click Add Comment to respond; instead, click edit and clarify your question to provide this very useful information. Why? Comments accumulate. When new comments arrive, old comments can get pushed off screen. – K7AAY Jun 20 at 16:30

Not sure if you can do it with just cp/globbing but using find/exec:

find /first/path/ -type f -not -name '*.zip' -exec cp {} /second/path/ \;

In order to avoid recursion you can add the maxdepth option:

find /first/path/ -maxdepth 1 -type f -not -name '*.zip' -exec cp {} /second/path/ \;

Alternatively you could copy everything from the source to the destination and then go through and remove all the .zip files afterward:

cp -R /first/path/ /second/path/ && find /second/path -type f -name '*.zip' -exec rm {} \;
  • 1
    Thank You Jesse, This is helpful ! – AMK Jun 20 at 17:19

A simple solution is to move the .zip files out of the way with mkdir and mv, copy the remainder with cp, then restore the .zip files to their original location with mv and rmdir.

cd SOURCE && mkdir ~/dELETEmE && mv *.zip ~/dELETEmE && cp * DESTINATION  
cd ~/dELETEmE && mv * SOURCE && cd .. && rmdir dELETEmE  
  • Thanks for the suggestion! – AMK Jun 20 at 17:20

Don't know if its necessary using only cp, but if using bash and being sure your files doesn't have spaces in their names, maybe like this?

Inside of your source path

cp $(ls | grep -v *.zip) /destination/path

Not the answer you're looking for? Browse other questions tagged or ask your own question.