Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 827
sven30 Avatar asked Jan 17 '12 16:01

sven30


People also ask

How exclude files from zip Linux?

In the above syntax, the “r” flag is used to append files and “x” flag is used to exclude files.

Can zip files contain files and directories?

ZIP is an archive file format that supports lossless data compression. A ZIP file may contain one or more files or directories that may have been compressed.

How do I exclude files from a folder?

Go to Start > Settings > Update & Security > Windows Security > Virus & threat protection. Under Virus & threat protection settings, select Manage settings, and then under Exclusions, select Add or remove exclusions. Select Add an exclusion, and then select from files, folders, file types, or process.

Can I zip a folder and subfolders?

Creating a zip folder allows files to be organized and compressed to a smaller file size for distribution or saving space. Zip folder can have subfolders within this main folder.


2 Answers

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

like image 153
pathfinder Avatar answered Jan 01 '23 21:01

pathfinder


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

like image 33
BRPocock Avatar answered Jan 01 '23 22:01

BRPocock