Zip command for excluding directories and files
Asked Answered
L

4

11

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.

Levine answered 17/1, 2012 at 16:59 Comment(3)
yes it is a programming question for an ssh script.Levine
Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See What topics can I ask about here in the Help Center. Perhaps Super User or Unix & Linux Stack Exchange would be a better place to ask.Seashore
Unix zip directory but excluded specific subdirectories, how to exclude directories and file zipping a directory?, How to exclude a directory when zipping files, etc.Seashore
F
10

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 \)

Fred answered 17/1, 2012 at 17:5 Comment(9)
ok, weird. I got the backslashes from about.com they did some sort of -x \*.0 for excluding all files that end in .0. no idea. anyway i tried your command and it still didn't work. Here's my actual command. zip core-latest.zip -r * -x templates -x stats -x cp/includes/configure.php @Levine
Should only need the one -x at the head and the one @ at the end… and, are you really running zip from inside the directory that you're zipping?Fred
here my script: echo -n "enter new version (v1-1)" read version cd /home/core/www/ rm -rf core-latest-*.zip zip core-latest-$version.zip -r * -x templates stats cp/includes/configure.php @ chown -R core.core core-latest-$version.zipLevine
It's just more usual to be up a level, so that your ZIP contains a top-level folder. Not "wrong," just unusual. Looks good, and seems to work for me.Fred
you mean zip up the www dir? It's for a php program we want to install on other servers for download where they would drop it in their www or public_html dir and unzip it and be done.Levine
would it matter if there were contents inside the templates and stats directories? I want it all excluded, the folder and its contents.Levine
here a directory listing inside the www or public_html directory. cache cp images includes mobile stats temp templatesLevine
ok, the last one actually sort of worked...but the problem now is that I have about 100 files in the root directory not getting zipped. it just zips the directories with that command.Levine
Ah. You didn't mention random files there — they weren't in the listing you gave; You could enumerate them, exclude using -x dir/* @ to strip out directories you don't want to zip, or … well, if your app is littering the root directory with 100 files, maybe it's time for a clean-up of the codebase to make less of a mess? :-) One gimmick: If your filenames have dots and directories don't, you could perhaps add *.* to the included list. Unlike DOS/Windows, this will only include files with a literal . in their name, so it will skip files/dirs without it (like templates)Fred
K
28

For my particular system in order to exclude a directory I had to put quotes around my excluded directories and it worked like a charm:

zip -r myarchive.zip target -x "target/exclude1/*" "target/exclude2/*"

Notes:

-- this excluded both the directory to exclude and all files inside it.

-- You must use the full path to the directories you want to include and exclude!

-- As long as the excludes are at the end of the line you do not need the @ symbol

Krantz answered 31/10, 2012 at 12:2 Comment(1)
This is the only answer that worked for me!Hotpress
F
10

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 \)

Fred answered 17/1, 2012 at 17:5 Comment(9)
ok, weird. I got the backslashes from about.com they did some sort of -x \*.0 for excluding all files that end in .0. no idea. anyway i tried your command and it still didn't work. Here's my actual command. zip core-latest.zip -r * -x templates -x stats -x cp/includes/configure.php @Levine
Should only need the one -x at the head and the one @ at the end… and, are you really running zip from inside the directory that you're zipping?Fred
here my script: echo -n "enter new version (v1-1)" read version cd /home/core/www/ rm -rf core-latest-*.zip zip core-latest-$version.zip -r * -x templates stats cp/includes/configure.php @ chown -R core.core core-latest-$version.zipLevine
It's just more usual to be up a level, so that your ZIP contains a top-level folder. Not "wrong," just unusual. Looks good, and seems to work for me.Fred
you mean zip up the www dir? It's for a php program we want to install on other servers for download where they would drop it in their www or public_html dir and unzip it and be done.Levine
would it matter if there were contents inside the templates and stats directories? I want it all excluded, the folder and its contents.Levine
here a directory listing inside the www or public_html directory. cache cp images includes mobile stats temp templatesLevine
ok, the last one actually sort of worked...but the problem now is that I have about 100 files in the root directory not getting zipped. it just zips the directories with that command.Levine
Ah. You didn't mention random files there — they weren't in the listing you gave; You could enumerate them, exclude using -x dir/* @ to strip out directories you don't want to zip, or … well, if your app is littering the root directory with 100 files, maybe it's time for a clean-up of the codebase to make less of a mess? :-) One gimmick: If your filenames have dots and directories don't, you could perhaps add *.* to the included list. Unlike DOS/Windows, this will only include files with a literal . in their name, so it will skip files/dirs without it (like templates)Fred
P
2

TLDR;

Use -x "**/folder/*"

LR;

You can use the same syntax as gitignore or vscode exclude files vscode files exclude syntax

Perilymph answered 16/10, 2023 at 5:37 Comment(0)
B
0

If you are using putty, please follow following steps:

  1. Enter your directory: $ cd directory_name and press enter
  2. Write command:

    $ zip -r file_name.zip . -x \*excludingfolderName\*
    

After zip Use . for current directory, * is a wildcard.

Balboa answered 1/5, 2015 at 8:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.