Search code examples
sshdirectorycentoszip

Zip command for excluding directories and files


i'm trying to use this ssh command in centos 5 to zip up a directory full up folders and files yet exclude the examples below. it's not working.

zip -r file.zip * -x dir1 -x dir2 -x file1 -x file2

or this one

zip -r file.zip * -x "dir1" -x "dir2" -x "file1" -x "file2"

It still just zips up the whole directory and everything it in. I don't want dir1 dir2 file1 file2.

I need to know the right -x syntax for the exclude functions to work.


Solution

  • The -x option is a bit strange; you list the files, starting with -x, and ending with an @ sign:

    zip file.zip -r * -x dir1 dir2 file1 file2 @
    

    I also seriously doubt you want \ in your filenames on a Unix system … make sure your paths are OK. / is the usual path separator.

    Example:

    mkdir tmp5
    cd tmp5
    touch a b c d e f g
    zip foo.zip -r * -x e f g @
      adding: a (stored 0%)
      adding: b (stored 0%)
      adding: c (stored 0%)
      adding: d (stored 0%)
    

    With subdirectories, you can easily omit their contents using a wildcard, but it seems that the * causes the directory itself to be included (empty):

    mkdir x y z
    touch {a,b,c} {x,y,z}/{a,b,c}
    zip foo.zip -r * -x c y y/* z z/* @
      adding: a (stored 0%)
      adding: b (stored 0%)
      adding: x/ (stored 0%)
      adding: x/b (stored 0%)
      adding: x/a (stored 0%)
      adding: x/c (stored 0%)
      adding: y/ (stored 0%)
      adding: z/ (stored 0%)
    

    You might to better to omit the * and explicitly list those things you do want included…

    zip core-latest-$version.zip -r cp images include mobile stats \
        -x cp/includes/configure.php @
    

    (the \ at the end just continues to the next line; you can put it all on one line without the trailing \)