Linux zip folder exclude

Unix zip directory but excluded specific subdirectories (and everything within them)

I’m trying to zip a directory (on Unix via SSH) but I need to exclude a couple of subdirectories (and all files and directories within them). So far I have this:

zip -r myarchive.zip dir1 -x dir1/ignoreDir/**/* 
zip -r myarchive.zip dir1 -x dir1/ignoreDir1/* dir1/ignoreDir2/* 

However that will still include subdirectories within ignoreDir1 and ignoreDir2. The subdirectory structure in the directories that I want to exclude is quite substantial so I can’t simply add each directory to the -x argument. Does anyone know how to do this?

9 Answers 9

The actual command I need is:

zip -r myarchive.zip dir1 -x dir1/ignoreDir1/**\* dir1/ignoreDir2/**\* 

On MacOS I was successful with: zip -r theme.zip theme -x theme/node_modules/\* or zip -r theme.zip theme -x «theme/node_modules/*»

For my particular system (Mac OS) in order to exclude a directory I had to put quotes around my excluded directories and it worked like a charm. You need to do this because sometimes directories and files can be named with spaces and special characters that would cause this to fail without the quotes. Its easier than escaping the spaces and special characters.

zip -r myarchive.zip dir1 -x "dir1/ignoreDir1/*" "dir1/ignoreDir2/*" 

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

— You must use the full path to the directories you want to exclude!

As nobody has explained the reason for requiring either quoting the paths or escaping the asterisk, the reason is this: If the path is not quoted or the wildcard escaped, the shell will perform wildcard expansion before passing them to zip , which will receive a list of paths to entries in the specified directories, but not paths to files within subdirectories of those, thus causing zip to not to ignore everything under the given directories.

I think originally one of the reasons this worked was because on Mac OS there were spaces in my directories and this avoided needing a \

Notice that when using a file with the exception list (zip -r myarchive.zip dir1 -x@exceptions.lst), it’s not necessary to use quotes neither escape with * in the file. Use just like: dir1/ignoreDir1/* dir1/ignoreDir2/*

zip -r myarchive.zip dir1 -x dir1/ignoreDir1/**\* dir1/ignoreDir2/**\* 

will still include dir1/ignoreDir1/ empty folder in the zip archive, using:

zip -r myarchive.zip dir1 -x dir1/ignoreDir1** dir1/ignoreDir2** 

will do the trick, you can also use a leading ** to search in subfolders instead of only dir1

zip -r myarchive.zip dir1 -x dir1/ignoreDir1\* dir1/ignoreDir2\*

What did you need the ** for, @sulman?

It works like a charm for me as follows:

[root@ip-00-000-000-000 dir1]# ls -lrt dir1/ total 16 drwxr-xr-x 2 root root 4096 Oct 31 07:38 ignoredir1 drwxr-xr-x 2 root root 4096 Oct 31 07:38 ignoredir2 drwxr-xr-x 2 root root 4096 Oct 31 07:39 dir3 -rw-r--r-- 1 root root 8 Oct 31 07:39 test.txt [root@ip-00-000-000-000 temp]# zip -r dir1.zip dir1 -x dir1/ignoredir1\* dir1/ignoredir2\* adding: dir1/ (stored 0%) adding: dir1/dir3/ (stored 0%) adding: dir1/dir3/test3.txt (deflated 13%) adding: dir1/test.txt (stored 0%) 

Sorry, @sulman, I typed wrongly. What I meant is as per my latest edit. Works a like a charm for me 🙂

Читайте также:  Установка linux mint файловая система

Just like other answers, but excluding directories entirely, instead of excluding all contents of directories:

zip -r myarchive.zip dir1 -x dir1/ignoreDir1/\* dir1/ignoreDir2/\* 

In Ubuntu Server this commands works for zip a file excluding some folders, but with a little differences:

If you wanna zip without keep empty folders:

zip -r myarchive.zip dir1 -x dir1/ignoreDir1/**\* dir1/ignoreDir2/**\* 

If you wanna zip keep empty folders:

zip -r myarchive.zip dir1 -x dir1/ignoreDir1\* dir1/ignoreDir2\* 
zip -r testtt.zip uploads/2013/ -x uploads/2013/03/**\* uploads/2013/04/**\* zip -r testtt.zip uploads/2013/ -x uploads/2013/03\* uploads/2013/04\* 

I haven’t looked at the code, but what appears to be happening is that -R creates a list of paths, and -x does a regex on each element of that list to include or exclude it. Thus, the argument to -x is basically a text regex — or that’s how you can think of it.

To exclude all instances of a folder name (and, actually, any files that have the same name), the command would be (I have shortened the prefix that will include ignoreDir1 and ignoreDir2 ):

zip -r myarchive.zip dir1 -x "*ignore*" 

To avoid excluding files, you can add the path separators (but you need to add the ? to the regex pattern to include both numbered folders):

zip -r myarchive.zip dir1 -x "*/ignoreDir?/*" 

I think it’s more readable to quote the argument instead of escaping the shell special characters.

Источник

How to exclude files from a zip archive in Linux

Collecting multiple files into a single file is called archiving, and “zip” is one of the widely used archive formats that come with lossless data compression support. Archiving files is an efficient way to save your machine’s storage and make it easier to transfer files through email or FTP.
Linux has a built-in zip utility to archive the files with a set of options. This utility provides several methods to archive the files. When you archive a directory using zip utility, it includes all the files in that directory and zip it. So, is it possible to archive a file and excluding the particular files using the “zip” utility? The answer is yes! The “zip” is a versatile Linux utility that lets you archive files, excluding specified files or folders. How does it happen? Let’s begin:

How to exclude files and directories when using “zip” in Linux:

You can archive any directory excluding a particular file using the “-x” flag with the “zip” utility. The general syntax is mentioned below:

Let’s understand it through an example; I have created a directory by the name of “images” that contains 3 image files, one zipped file, and one sub-directory (my folder) as shown in the following image :

Now launch terminal and type:

In the above command, I am compressing the “images” directory and excluding an image file “img2.jpg” and saving it as “myfile.zip.”

As it can be seen that “myfile.zip” did not include the “img2.jpg” file. You can also exclude file by mentioning the extension of the file; for instance, I want to exclude all “.jpg” files in the “images” directory and archive it:

Читайте также:  What is memory leak in linux

All the “jpg” files have been excluded in the zip file. You can exclude multiple files and directories by mentioning them in the following way:

$zip -r myfile.zip images -x / images / img2.jpg -x

The “img2.jpg” and “all_images.zip” files will not be archived:

Conclusion:

Keeping the files in an archive saves a lot of your machine’s storage and helps manage tons of files and folders. In Linux “zip” utility is used to archive files/directories. In this write-up, we learned how to use the “zip” utility to archive the directory, excluding a specific file or folder. The “zip” utility comes with quite useful options for executing “zip –help” in the terminal.

About the author

Sam U

I am a professional graphics designer with over 6 years of experience. Currently doing research in virtual reality, augmented reality and mixed reality.
I hardly watch movies but love to read tech related books and articles.

Источник

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 
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.

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.

3 Answers 3

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/*" 

— 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

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.

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

Читайте также:  Format disk linux mint

Источник

How to Exclude Files from a Zip Archive

Do you want to send files but you are facing a problem due to their large size? It has the solution. For this purpose, we have a tool known as zip, which is used to compress all the files and store them in a single folder. Now you can send the compressed zip folder containing all the files easily to anyone. But there is a folder having 100 files and you have to forward this folder excluding some specific files. Now instead of going to the folder and searching the specific files, we can exclude them by using some commands.

In this article, we will discuss how to make a zip folder by excluding specific files and how it works.

Syntax

Generally, all the Linux distributions have default zip packages installed. The syntax of the command excluding files and directories is as follows:

In the above syntax, the “-r” flag is used to append files, and the “-x” flag is used to exclude files. Now to understand it more clearly let us consider an example.

Examples to Excludes Files in Zip Archive

I have created a sample directory structure including some files. Where a directory named “docs” contains one directory and some files as shown below screenshot:

List Directory Tree in Linux

Let’s consider the above structure, here are a few examples to learn about excluding files from the zip archive.

01. Exclude a directory

Create an archive file excluding the cache directory. Use the following command to create a new archive named “docs.zip” excluding the “cache” directory. So in command, it will be written as:

zip -r docs.zip docs -x "docs/cache/*" 

Zip Command to Exclude a Directory

02. Exclude a single file

Define the full file path to exclude a single file from the archive file with the zip command. For example, to exclude index.html from the docs directory, type:

zip -r docs.zip docs -x "docs/index.html" 

Zip Command to Exclude Single File

03. Exclude files with wildcard

You can also use wild card characters to exclude multiple files from the zip archive. For example, to exclude all files with the “.log” extension, run:

Zip Command to Exclude files With Wildcard Names

04. Exclude multiple files

You can define -x multiple times in a single zip command to exclude multiple files and directories from the zip archive.

zip -r docs.zip docs -x "/docs/README.md" -x "docs/cache/*" 

Zip Command to Exclude Multiple Files

05. Exclude “.git” directory

The source code managed with Git contains a “.git” directory under the root directory. Sometimes you may not be required to archive this directory. Use the following command to exclude .git directory and its contents:

zip -r filename.zip my_dir -x "*.git*" 

06. Exclude “.svn” directory

Applications source code managed through SVN contains the “.svn” directory. Use the following command to exclude the .svn directory from the zip archive.

zip -r filename.zip my_dir -x "*.svn*" 

07. Excluding “node_modules” Directory from Zip Archive

All the Node.js modules are installed under the node_modules directory. While archiving the source code, you can ignore the “node_modules” directory with the below-mentioned command:

zip -r mydir.zip myDir -x "node_modules*" 

Conclusion

With the advancement of technology, it’s not a big issue to resolve anything. Now we can easily make a single zip file of many files and can email it to anyone easily. In this article, we learned how we can make zip files excluding some files that we don’t want to be a part of the zip file

Источник

Оцените статью
Adblock
detector