231

Possible Duplicate:
gzipping up a set of directories and creating a tar compressed file

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.

Community
  • 1
  • 1
Doug
  • 5,116
  • 10
  • 33
  • 42
  • 1
    `gzip` by its very nature compresses only a single file. To put multiple files into one file for subsequent gzipping, use `tar`. – Thomas Sep 08 '12 at 14:41
  • Possible duplicate of [gzipping up a set of directories and creating a tar compressed file](https://stackoverflow.com/questions/3341131/gzipping-up-a-set-of-directories-and-creating-a-tar-compressed-file) – Cœur Jul 14 '18 at 14:19

3 Answers3

440
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

kevinmicke
  • 5,038
  • 2
  • 20
  • 22
amitchhajer
  • 12,492
  • 6
  • 40
  • 53
  • 2
    Thanks. Will this compress everything under "folderToCompress" (i.e., recursively)? – Doug Sep 08 '12 at 14:48
  • 2
    everything in folderToCompress will go to compressFileName – amitchhajer Sep 08 '12 at 14:56
  • 85
    You'll probably want to choose `compressFileName` so it includes the `.tar.gz` extension or you'll drive everyone else crazy. – drewish Oct 19 '13 at 00:48
  • 9
    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. – Zoltán Jan 31 '14 at 12:59
  • 6
    Also works with `*` to compress everything in current dir, ie.: `tar -zcvf all.tar.gz *` –  Feb 28 '14 at 10:18
  • 2
    But I learned if you use . instead of * it will fail, is tar tries to compress the new zipfile itself. – Marc Maxmeister Jul 29 '14 at 17:19
  • 1
    @PeterM, `*` ignores hidden files. use `tar -zcvf all.tar.gz * .*` to glob them all together. Or use `tar -zcvf all.tar.gz .` (See below.) – kdbanman Dec 07 '14 at 00:10
  • 1
    @MarcMaxson, yes, `tar -zcvf all.tar.gz .` will try to grab its own archive and shove it inside itself (bad). But if you put the archive somewhere else, then you're fine. EX. `tar -zcvf /tmp/all.tar.gz .` works great. _This is because `*` is expanded to all pwd contents by shell (except hidden files!!) before the `all.tar.gz` archive is created. When `.` is used instead, `tar` actually traverses the directory, so it will see the directory it's creating if that's where you put it._ :-) – kdbanman Dec 07 '14 at 00:16
  • 1
    Maybe it's a zsh quick but `tar cf file.tar.gz -C dir/` makes me a package with `dir` as first directory. I want's everything *inside* `dir` not itself... – tutuca Sep 06 '15 at 03:34
68

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

/home/myusername

tar -xvzf passwd.tar.gz

this will create /home/myusername/etc/passwd

unsure if all versions of tar do this:

 Removing leading `/' from member names
V H
  • 8,382
  • 2
  • 28
  • 48
19

@amitchhajer 's post works for GNU tar. If someone finds this post and needs it to work on a NON GNU system, they can do this:

tar cvf - folderToCompress | gzip > compressFileName

To expand the archive:

zcat compressFileName | tar xvf -
Jeremy J Starcher
  • 23,369
  • 6
  • 54
  • 74
  • 2
    In addition to being portable, this solution is useful if you want to pass arguments to gzip (e.g., --test or --best), since afaik even GNU tar doesn't provide a way to send arguments to gzip. – Timothy Woods Oct 04 '13 at 18:46
  • This is versatile, since you can also use other compressors. For example `pigz` which is faster as it can utilize the cpu better(a multi-threaded implementation of zip). – Ortwin Angermeier Dec 04 '14 at 17:54