Linux compress all folders

Can I zip an entire folder using gzip?

Use tar ; it supports options for compression. gzip is designed as a complement to tar , not as a replacement.

To demostrate what @Shadur means. Given files 1 and 2 under folder playground . gzip -r ./playground will give you 1.gz and 2.gz (and no more 1 and 2 files) under folder playground , NOT a zipped file with everything in it.

@Shadur — I had the same issue using the answer at lifewire.com/example-uses-of-the-linux-gzip-command-4078675 coz I blindly went for the most plausible command — they need to specify RECURSIVELY — I mean who would want to do that more often than an entire dir ?

7 Answers 7

Unlike zip , gzip functions as a compression algorithm only.

Because of various reasons some of which hearken back to the era of tape drives, Unix uses a program named tar to archive data, which can then be compressed with a compression program like gzip , bzip2 , 7zip , etc.

In order to «zip» a directory, the correct command would be

tar -zcvf archive.tar.gz directory/ 
  • compress it using the z (gzip) algorithm
  • c (create) an archive from the files in directory ( tar is recursive by default)
  • v (verbosely) list (on /dev/stderr so it doesn’t affect piped commands) all the files it adds to the archive.
  • and store the output as a f (file) named archive.tar.gz

The tar command offers gzip support (via the -z flag) purely for your convenience. The gzip command/lib is completely separate. The command above is effectively the same as

tar -cv directory | gzip > archive.tar.gz 

To decompress and unpack the archive into the current directory you would use

That command is effectively the same as

tar has many, many, MANY other options and uses as well; I heartily recommend reading through its manpage sometime.

Just to make things even more explicit, this is exactly equivilant to tar -cv directory | gzip > archive.tar.gz . The resulting archive can then be extracted with tar -zxvf or even zcat file.tar.gz | tar -xv . Point being that the tar is completely independent from the gzip, the tar command just includes gzip support for convenience.

i have been using tar cvzf for quite a while. one thing to note: if you use Windows ( 7-zip to be specified) to unzip a *.tar.gz file, it takes two rounds. One to unzip *.tar.gz file into a *.tar file, the next one to unzip that tar file into the original content. it increases the total unzipping time, especially for large files (e.g. logs)

Does the naming of the archive affect anything machine-wise? I know it would be nice to let people know the algorithm originally used to zip it (hence .gz), but other than that does is it actually matter how you name the archive?

@hello_there_andy It makes no difference to most unixes, but windows (and smart tab completion in linux) will makes assumptions based on filename extension.

Читайте также:  Archer t2u nano arch linux

The gzip command will not recursively compress a directory into a single zip file, when using the -r switch. Rather it will walk that directory structure and zip each file that it finds into a separate file.

Example

$ tree dir1/ dir1/ |-- dir11 | |-- file11 | |-- file12 | `-- file13 |-- file1 |-- file2 `-- file3 
$ tree dir1/ dir1/ |-- dir11 | |-- file11.gz | |-- file12.gz | `-- file13.gz |-- file1.gz |-- file2.gz `-- file3.gz 

If you’d prefer to zip up the directory structure then you’ll likely want to use the tar command, and then compress the resulting .tar file.

Example

$ tar zcvf dir1.tar.gz dir1/ dir1/ dir1/file1 dir1/file2 dir1/dir11/ dir1/dir11/file11.gz dir1/dir11/file12.gz dir1/dir11/file13.gz dir1/file3 

Which results in the following single file:

$ ls -l | grep tar -rw-rw-r-- 1 saml saml 271 Oct 1 08:07 dir1.tar.gz 

You can confirm its contents:

$ tar ztvf dir1.tar.gz drwxrwxr-x saml/saml 0 2013-10-01 08:05 dir1/ -rw-rw-r-- saml/saml 0 2013-10-01 07:45 dir1/file1 -rw-rw-r-- saml/saml 0 2013-10-01 07:45 dir1/file2 drwxrwxr-x saml/saml 0 2013-10-01 08:04 dir1/dir11/ -rw-rw-r-- saml/saml 27 2013-10-01 07:45 dir1/dir11/file11.gz -rw-rw-r-- saml/saml 27 2013-10-01 07:45 dir1/dir11/file12.gz -rw-rw-r-- saml/saml 27 2013-10-01 07:45 dir1/dir11/file13.gz -rw-rw-r-- saml/saml 0 2013-10-01 07:45 dir1/file3 

The Answer to the question “Can I zip an entire folder using gzip [on linux]?” is that you can use the zip program in place of gzip, with the syntax:

@JeffSchaller: This is an answer. It’s a grievously wrong one (I believe), but it clearly is an answer.

Points for using zip like the question asked, most likely the author would have liked to know it’s possible to DO THE EXACT SAME THING as he used to do on windows. It is sad most people don’t wish to think as far.

I’m upvoting this answer since I think the product (a zip file) is likely more important than the method (using gzip).

I scripted these 2 commands:

#!/bin/bash if [[ -d $1 ]]; then cd "$1" cd .. base=$(basename "$1") tar -zcvf "$base.tgz" "$base" if [[ $? == 0 && -f "$base.tgz" ]]; then rm -rf "$base" fi else echo "Usage: $0 DIRECTORY"; fi 
#!/bin/bash if [[ -f $1 ]]; then base=$ file=$(basename "$1"); dir=$(basename "$base"); if [[ ! -d $base ]]; then mkdir "$base" cd "$base" cd .. tar -xvf "$file" if [[ $? == 0 && -d "$dir" ]]; then rm -f "$file" fi else echo "Directory $base already exists. Nothing done." fi else echo "Usage: $0 file.tgz"; fi 

(. ) Please test before use (as there is a ‘rm -f’ which could potentially remove important data if used in an uncommon way).

cd /home/; gzipdir MyDirectory or gzipdir /home/MyDirectory

Will create /home/MyDirectory.tgz and remove MyDirectory on success (. ).

Will create /home/MyDirectory and remove /home/MyDirectory.tgz on success.

Источник

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

Читайте также:  What is linux machine

*.* 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 compress a directory?

I’m trying to compress a directory and ftp it to a windows ftp. I have tried every tar command I can find to compress a directory. It appears to be ok. Then I transfer it and view it’s contents using Winrar. Winrar keeps telling me the file is corrupted. I have viewed other .gz or .bz2 files using winrar but for some odd reason I can’t get it to work. I would prefer just to have it zip the files so they have a .zip extension but even then when i try to browse it’s contents both windows and winrar claim it’s corrupt. Does anyone else have a suggestion as to something else to try?

3 Answers 3

Well, most probably your files are perfectly fine before FTP transmission.

Unfortunately, probably you are transferring your files using wrong FTP mode.

FTP do have two modes: binary and ASCII. By default most clients use ASCII mode, which breaks your binary files completely. I don’t know which FTP client you are using, but for example in ncftp you can use command «binary» to switch to binary mode.

If you want to create ZIP files using Ubuntu (or almost any other Linux), use zip . You can install it to Ubuntu by running

Then you can create zip file by running

zip -r compressed_filename.zip foldername 

On related note, you should know that FTP is insecure transmission protocol. Consider switching to sftp, for example. There is many free ssh servers for Windows, including minimal OpenSSH port.

Читайте также:  Linux debian на ноутбуке

Источник

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.

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

Источник

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