Gzip all files in directory linux

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?

Читайте также:  Удалить рекурсивно директорию linux

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

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.

Источник

How to gzip a Directory in Linux

Trying to gzip a directory and seeing an error in Linux? Here’s the command (with explanation) to gzip compress a folder in Linux terminal.

Читайте также:  Linux show users list

How do you gzip a directory in Linux? It’s definitely not using the gzip command because if you try to compress a folder using gzip command, you’ll see this error:

gzip: target is a directory — ignored

Interesting, isn’t it? gzip command cannot compress a directory because essentially, gzip works on individual files, not the entire folder.

What can you do now? How to gzip compress a file in Linux? Here’s what you can do.

gzip a directory using tar command

Instead of trying to compress the folder directly, you should use tar on it first. The tar command will collate all the files into one archive file. It doesn’t compress the file itself.

If you combine tar with gzip, the tar command will create one single archive file from the folder and then gzip will compress this archive file.

The good thing is that you can do both of these steps in one single command by using the z option. The command looks something like this:

tar -zcvf output_file_name directory_to_compress

Let me explain the options used in the above command to you:

  • z – tells tar that it is dealing with gzip file
  • c – tells tar to create the archive file
  • v – verbose mode showing what files are being processed
  • f – output is a file

Example of using tar and gzip to compress a folder

Let’s say I have a bunch of files in a directory. If I use the du command to get the size of the directory, it’s 204 KB.

du -sh sample_text_files 204K sample_text_files

Now if I gzip compress the folder:

tar -cvzf sample_text_archive sample_text_files sample_text_files/ sample_text_files/sample_rar.rar sample_text_files/test/ sample_text_files/dir2/ sample_text_files/dir2/services sample_text_files/dir2/agatha.txt sample_text_files/abhi-3.txt

Now if I check the size of the compressed folder using ls command, it’s hardly 10 KB.

ls -lh sample_text_archive -rw-r--r-- 1 abhishek abhishek 9.6K Apr 11 11:41 sample_text_archive

Keep in mind while using tar and gzip command

You should note a couple of things while using the tar command:

It’s important to provide the filename in the command otherwise you’ll see this error:

tar: Cowardly refusing to create an empty archive Try 'tar --help' or 'tar --usage' for more information.

If you don’t use the option -f at all, you will still see an error, a different one this time.

tar: Refusing to write archive contents to terminal (missing -f option?) tar: Error is not recoverable: exiting now

I hope this command helped you to gzip a directory in Linux and you also learned a few related things around tar and compression. Now that you know how to compress a folder, maybe you would like to read on how to extract tar xz file in Linux.

Any questions, suggestions or a simple word of thanks are always welcomed.

Источник

How to gzip all files in all sub-directories into one compressed file in bash

This post describes how to gzip each file individually within a directory structure. However, I need to do something slightly different. I need to produce one big gzip file for all files under a certain directory. I also need to be able to specify the output filename for the compressed file (e.g., files.gz) and overwrite the old compressed file file if one already exists.

Читайте также:  Функции файловой системы linux

gzip by its very nature compresses only a single file. To put multiple files into one file for subsequent gzipping, use tar .

3 Answers 3

tar -zcvf compressFileName.tar.gz folderToCompress 

everything in folderToCompress will go to compressFileName

Edit: After review and comments I realized that people may get confused with compressFileName without an extension. If you want you can use .tar.gz extension(as suggested) with the compressFileName

You’ll probably want to choose compressFileName so it includes the .tar.gz extension or you’ll drive everyone else crazy.

This also works for an arbitrary number of directories, e.g. tar -zcvf two-dirs.tar.gz dir-one dir-two will create an archive containing two directories.

there are lots of compression methods that work recursively command line and its good to know who the end audience is.

i.e. if it is to be sent to someone running windows then zip would probably be best:

zip -r file.zip folder_to_zip unzip filenname.zip 

for other linux users or your self tar is great

tar -cvzf filename.tar.gz folder tar -cvjf filename.tar.bz2 folder # even more compression #change the -c to -x to above to extract 

One must be careful with tar and how things are tarred up/extracted, for example if I run

cd ~ tar -cvzf passwd.tar.gz /etc/passwd tar: Removing leading `/' from member names /etc/passwd pwd 

this will create /home/myusername/etc/passwd

unsure if all versions of tar do this:

 Removing leading `/' from member names 

Источник

How to gzip all or specific files in Linux

The gzip (GNU zip) utility is older and less efficient than bzip2. Its flags and operation are very similar to those of bzip2. A file compressed by gzip is marked with a .gz filename extension. Linux stores manual pages in gzip format to save disk space; likewise, files you download from the Internet are frequently in gzip format.

To gzip all the files in current directory, we can use for command. The below example will gzip all the files from /var/log/audit directory.

gzip all the files

1. Change the directory to audit logs as follows:

2. Execute the following command in the audit directory:

# ls audit.log audit.log.1 audit.log.2 audit.log.3 audit.log.4
# for LOG in audit* do gzip $LOG done

3. This will zip all the files in audit directory. Verify the gzipped log file in the /var/log/audit directory:

# ls audit.log.1.gz audit.log.2.gz audit.log.3.gz audit.log.4.gz audit.log.gz

Unzip all files

1. To unzip all the files in one go, execute the following command:

# for LOG in audit* do gunzip $LOG done

Verify the unzipped files in the directory:

# ls audit.log audit.log.1 audit.log.2 audit.log.3 audit.log.4

gzip specific files only

To gzip some specific files only, use the below gzip command:

# gzip -c file file1 test > gzip.gz

This will create the gzip.gz file in the current directory which includes the files specified with -c option.

Источник

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