Zip all files and folders 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.

Читайте также:  Hamachi установка на kali 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

Источник

How to Zip and Unzip a directory and its files in Linux [duplicate]

I am a newbie in Linux.Whats is the complete process to zip and unzip a directory and its files.Please mention if any installation has to be done.

Читайте также:  Изменить цвет терминала linux

3 Answers 3

You say you are a newbie. Maybe you got Windows-experiences. If you do not have the packages installed yet, you need to

sudo apt-get install zip gzip tar 

first (or by a graphical pkg-manager).

Then, for an entry it would be the easiest way to use zip/unzip:

zip -r my_arch.zip my_folder 

Zip stores relative path names by default. There are several parameter-options available for zip. For that read: the manual (man zip). For a starting this will do.

Most often you will see .tar.gz endings in linux-world. That’s the product of two tools: TAR (the tape archiver) and GZIP (the GNU-Zip). Tar has got the call option to automatically gzip/gunzip files after «taring».

tar -cvzf may_arch.tar.gz my_folder 
  • -c means «create»
  • -v means «verbose» (sometimes bothersome and slowing down. )
  • -z means «use (GNU)zip»
  • -f XYZ declares the name of the output file. (You should chose a helping name like XYZ.tar.gz)

There may also be .tar.bz2 endings. This is the product of the -j parameter instead of the -z parameter: you will choose compression with BZIP2 (-> man bzip2).

To extract you simply use -x (eXtract) instead of -c (Create):

You can use the zip and unzip command line utilities. These can be installed by running

sudo apt-get install zip unzip 

I know several ways, but since you’re new on linux. So I’ll tell you how to zip a file using GUI method (the easiest way).

enter image description here

  1. Create a new folder and fill it with anything you want, for example many of file (In my case, I’ll fill it with theme folder) :

enter image description here

  1. Right click the folder you want to zip and select «Compress. » option : enter image description here
  2. You can choose which file format you want by clicking combobox next to «Filename» textbox. Also you can set the folder you want to zip location. enter image description here

enter image description here

Clicking «Other options» will lead you to password section, in other word you can set password for your preferred file so someone (include you) have to enter password before unzip the file.

Источник

zip all files and subfolder in directory without parent directory

I want to zip the content (files and sub folders) of folder without including the root folder in the zip. I have tried command zip -r package.zip folder But this includes the root folder. Also tried the following form zip -j -r package.zip folder But this will flatten all the directory structures and just include the files. How do I preserve the internal directory structure but ignore the parent folder?

3 Answers 3

zip stores paths relative to the current directory (when it is invoked), so you need to change that:

(cd folder && zip -r "$OLDPWD/package.zip" .) 

&& ensures that zip only runs if the directory was correctly changed, and the parentheses run everything in a subshell, so the current directory is restored at the end. Using OLDPWD avoids having to calculate the relative path to package.zip .

Читайте также:  Копирование символической ссылки linux

The above command will zip all files and sub folders under folder directory (It will ignore the parent directory for folder)

This will store the folder/ path in the archive, which the OP doesn’t want. It will also ignore any hidden files inside folder .

To add to other answers, for scripts, you could use

pushd folder; zip -r ../package.zip .; popd 

pushd changes the directory much like cd , but remembers the previous one, and popd restores the last remembered directory.

In this case it’s equivalent to removing the parentheses (which spawn a new shell) and adding ; cd .. at the end of the command suggested by Stephen.

Note: I would add this as a comment, but I can’t because I do not have enough reputation.

Stephen’s answer is fine as it is; it doesn’t need anything added to it. Do you believe that your approach is better than his? Why? … … … … … … … … … … … … … … … … … … Please do not respond in comments; edit your answer to make it clearer and more complete.

It’s a nice trick that is useful especially when writing scripts as I mentioned in the first sentence.

It’s not necessarily better, but can make your life easier. For example, if you go a few directories down but need to back later in the script, it’s easier to just popd than to cd ../../../../ . It’s easy to get the number of levels you have to go up wrong. Can’t make that mistake with popd though. Thanks for coming to my speech inside a comment!

Well, my point is that you don’t seem to fully understand Stephen’s answer. He is using parentheses, not as English punctuation, but as shell syntax, causing the commands within the parentheses to run in a subshell. So the cd changes the working directory of the subshell but not the main shell. To borrow your language, the main shell process remembers what directory it is in, by staying there; it’s still there when the subshell terminates, so nothing needs to be restored. … (Cont’d)

(Cont’d) … If Stephen’s answer is a line in a script, the command on the next line (or even after a ; ) is executed in the directory above “ folder ”. You can try this yourself with (pwd; cd /; pwd); pwd (from the command line or in a script). That’s why I said “Stephen’s answer is fine as it is; it doesn’t need anything added to it.” — it doesn’t need to cd .. .

Источник

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