Zip folders recursively linux

How can I create a zip archive of a whole directory via terminal without hidden files?

I have a project with lots of hidden folders / files in it. I want to create a zip-archive of it, but in the archive shouldn’t be any hidden folders / files. If files in a hidden folder are not hidden, they should also not be included. I know that I can create a zip archive of a directory like this:

zip -r zipfile.zip directory 
zip -r zipfile.zip directory -x .* 

8 Answers 8

First of all if you don’t have installed zip install it first as follows:

Then for simply creating a zip file:

zip -r compressed_filename.zip foldername 

If you want to exclude hidden files:

find /folder_path -path '*/.*' -prune -o -type f -print | zip ~/compressed_filename.zip -@ 

The basics of file exclusion when creating a zip archive are centered around the -x flag, which is used to exclude files from the archive that match a specific name or pattern. At it’s most basic, it will look like this:

zip archive.zip files -x "ExcludeMe" 

Meaning you could exclude a single file, say it’s named “Nothanks.jpg”

zip archive.zip images/ -x "Nothanks.jpg" 

Let’s cover a few specific examples where this is useful.

Exclude .DS_Store Files from Zip Archives

This will prevent the typically invisible Mac metadata .DS_Store files from being included in a zip archive, which are bundled in by default:

zip -r archivename.zip archivedirectory -x "*.DS_Store" 

If the directory includes subdirectories however, you’ll want to use another variation of that command to exclude the the ds_store files from subdirectories as well:

zip -r archive.zip directory -x "*/\.DS_Store" 

Note: not all shells require the quotations for this command to work properly, but in the bash shell (the default for OS X) you will need to use the quotes for excluding with wildcards and patterns.

Exclude Specific File Types from a Zip Archive

With wildcards, you can also exclude all files of a certain type by focusing on the extension. For example, this command will zip an entire directory, minus any .jpg files:

zip -r archive.zip directory -x "*.jpg" 

That could be modified for any specific file extension or pattern matched in a file name.

Читайте также:  Arch linux aur установка

Exclude the .git or .svn Directory from a Zip Archive

Zip a directory, minus .git and it’s contents:

zip -r zipdir.zip directorytozip -x "*.git*" 

Zip a folder, without including the .svn directory:

zip -r zipped.zip directory -x "*.svn*" 

Exclude All Hidden Files from a Zip Archive

Since patterns and wildcards can be used, you could also exclude any or all invisible files and folders that are made so by being prefixed with a period, whether it’s a directory like .svn or an individual file like .bash_profile or .htaccess .

zip -r archivename.zip directorytozip -x "*.*" 

Or to exclude all invisible files from all subdirectories:

zip -r archive.zip directory -x "*/\.*" 

Источник

zip all files and folders recursively in bash

I am working on a project, where compilation of the project involves, zipping up various files and folders and subfolders (html/css/js) selectively. Working on the windows platform, and I could continue to just use the CTRL+A and then SHIFT-click to unselect, but it does get a little tedious. I am working with cygwin, so I was wondering if it is possible to issue a command to zip selected files/folders recursively whilst excluding others, in one command? I already have zip command installed, but I seem to be zipping up the current zip file too and the .svn file too. I would like this to be incorporated into a shell script if possible, so the simpler the better.

2 Answers 2

After reading the man pages, I think the solution that I was looking for is as follws:

  • needs to recurse directories (-r),
  • needs to exclude certail files/directories (-x)
  • It works in the current directory, but the . can be replaced with the path of any directory zip -x directories_to_exclude -r codebase_latest.zip .

I have incorporated this into a short shell script that deletes files, tidy up some code, and then zips up all of the files as needed.

You should read man page of zip command:

-R --recurse-patterns Travel the directory structure recursively starting at the current directory; for example: zip -R foo "*.c" In this case, all the files matching *.c in the tree starting at the current directory are stored into a zip archive named foo.zip. Note that *.c will match file.c, a/file.c and a/b/.c. More than one pattern can be listed as separate arguments. Note for PKZIP users: the equivalent command is pkzip -rP foo *.c Patterns are relative file paths as they appear in the archive, or will after zipping, and can have optional wildcards in them. For example, given the cur‐ rent directory is foo and under it are directories foo1 and foo2 and in foo1 is the file bar.c, zip -R foo/* will zip up foo, foo/foo1, foo/foo1/bar.c, and foo/foo2. zip -R */bar.c will zip up foo/foo1/bar.c. See the note for -r on escaping wildcards. 

Источник

Читайте также:  Epson perfection v10 драйвер linux

Zip all files in directory?

Is there a way to zip all files in a given directory with the zip command? I’ve heard of using *.* , but I want it to work for extensionless files, too.

Have you tried navigating one-level up from your desired directory and doing zip myarch.zip mydir/* ?

*.* means any file with a dot. In cp/m and dos all files had a dot, and it made you type it (could not do * ). Therefore people came to see *.* as all files. Eventually Microsoft added long-filenames that could have zero, or more dots. To find a file that has a dot on windows you have to type *.*.* .

5 Answers 5

You can just use * ; there is no need for *.* . File extensions are not special on Unix. * matches zero or more characters—including a dot. So it matches foo.png , because that’s zero or more characters (seven, to be exact).

Note that * by default doesn’t match files beginning with a dot (neither does *.* ). This is often what you want. If not, in bash, if you shopt -s dotglob it will (but will still exclude . and .. ). Other shells have different ways (or none at all) of including dotfiles.

Alternatively, zip also has a -r (recursive) option to do entire directory trees at once (and not have to worry about the dotfile problem):

where mydir is the directory containing your files. Note that the produced zip will contain the directory structure as well as the files. As peterph points out in his comment, this is usually seen as a good thing: extracting the zip will neatly store all the extracted files in one subdirectory.

You can also tell zip to not store the paths with the -j / —junk-paths option.

The zip command comes with documentation telling you about all of its (many) options; type man zip to see that documentation. This isn’t unique to zip; you can get documentation for most commands this way.

Источник

How Do I Zip All Files In A Directory In Linux?

Zip archives refer to container archives that contain one or more compressed files and directories. Zip files are cross-platform, allowing you to create zip archives in Windows, Linux, and macOS using various utilities. In addition, zip archive files take less space, making them easier to transfer and store.

Читайте также:  How to cluster in linux

In Linux, we use the zip archive utility to create zip archives. Throughout this tutorial, we will focus on how to go about creating zip archives in Linux using the zip utility.

Install Zip

Depending on your Linux distribution, you will need to install the zip utility. Since all Linux distributions support it, you can use the package manager to install it.

For Debian/Ubuntu

On Debian, use the command:

For REHL/CentOS

On CentOS and REHL family, use the command:

The Zip Command

The zip command is simple to use. The general syntax for the command is:

To create a zip archive of more than one file, pass them in a list (separated by space) after the zip filename. It is also good to ensure you have to write permissions in the directory you are creating the zip file.

How to Zip Files In Linux

We can zip files in a directory as:

The command above displays the name of the file added to the archive and the compression method.

Zip utility automatically adds a .zip extension to the archive filename—if not explicitly specified.

How to Compress Zip Directories In Linux

You can compress directories and the corresponding sub-directories by using the -r flag. The -r flag will tell zip to traverse the entire directory recursively.

For example, consider the /var/log directory. To create an archive of all the files and directories, we use the command:

To suppress the output from the compression process, use the -q for quiet mode. The command creates a zip archive of the specified files with no output.

How to Zip all Files in a Directory In Linux

What if you want to zip all files in a directory? In that case, we use wildcard expressions to do this.

The above command adds all the files and directories in the specified path and adds them to the zip archive.

How to Zip All Files, Including Hidden Files

To add even hidden files to a zip archive, use the wildcard (.* *). The command for that is:

Conclusion

As shown in this tutorial, Linux allows you to create zip archives. You can use any archive utility such as WinRar, 7zip, unzip; to unarchive the files.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list

Источник

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