17

I have to create an archive with the command gzip (not tar - it's necessary) and the archive should contain files from another directory - for example, /etc. I tried to use command

gzip myetc.gz /etc

But it didn't work.

6
  • 3
    gzip is not zip. They are different tools that work differently. Commented May 23, 2016 at 12:11
  • 3
    gzip is just stream compressor. It does not know about files or directories.
    – user140866
    Commented May 23, 2016 at 12:34
  • 3
    Why can't you use tar?
    – marcelm
    Commented May 23, 2016 at 15:37
  • If you don't have tar, do you have cpio?
    – Rup
    Commented May 23, 2016 at 17:29
  • 2
    You say you want to create an archive, but you can't use tar. You know that part of the word "tar" comes from "ARchive". I mean, that's its raison d'etre. Who or what would want to exclude "tar" from an archive creation process? Basically you're asking how to drive a screw with a hammer (not a screwdriver - it's necessary).
    – Mike S
    Commented May 23, 2016 at 19:31

3 Answers 3

17

gzip works with files, not directories. Therefore to create an archive of a directory you need to use tar (which will create a single tarball out of multiple files):

tar cvf myetc.tar /etc

or, for a gzipped archive:

tar cvzf myetc.tar.gz /etc
1
  • 2
    if one has an "ancient" system with no "-z" option, they can do: cd / ; tar cf - ./etc | gzip -c - > myetc.tar.gz (if you are wondering why I tar "./etc" instead of "/etc", it's because on those older systems, when using tar xvf, it will output to /etc and overwrite it ... [or you have to use "pax" in that case]). Inverse command, ie to extract that archive underneath "/somewhere" : cd /somewhere ; gzip -dc < /the/location/of/myetc.tar.gz | tar xvf - Commented May 23, 2016 at 15:45
7

Gzip works only with a single file, or a stream - data piped to gzip. So you first need to generate one file, like with tar, and then you could gzip that. The other option is to gzip all individual files, and then tar that into one file.

Both these solutions are stupid and should not be used. You should use tar with the built in compression option and do it all in one command.

5
  • 1
    Why should first method be stupid? This is the usual way to produce .tar.gz... Commented May 23, 2016 at 15:13
  • Well stupid is probably not the right term, but instead of using gzip, if you can use tar with the c option. It's simpler, and I don't believe piping tar to gzip has an advantage.
    – SPRBRN
    Commented May 23, 2016 at 15:42
  • Gzip can work for multiple files with -r option, but will create a new .gz for each file in the folder. Commented May 23, 2016 at 17:39
  • 1
    @rexkogitans - it's stupid in the same way that cat filename | grep blah is stupid. It's also much less than optimal because it would use a large interim space. I don't know if I'd call it stupid as much as unwarranted, suboptimal, and generally just sort of ugly. Well, stupid works too.
    – Mike S
    Commented May 23, 2016 at 19:28
  • Oh, now i see what's so stupid. No, ` tar -czf` is the way to go as it does not involve a useless fork(). Commented Jul 22, 2016 at 18:42
2

If you really dont have tar available, you can use gzip to create a backup of a directory using the option (-r - recursively), but maybe it is not exactly what you're expecting for...

First you will need to copy your directory to another name, if you want to keep the original directory untouchable.

#cp -a directory my_gzip_alone_backup

Second, you will compress all the files on the directory recursively(-r).

#gzip -r my_gzip_alone_backup

Now lets take a look inside the my-gzip-alone-backup. You will not have a single file myfile.gz as you are expecting for, but a lot gziped files recursively. But it is not good to do that with etc because of symlinks...

To uncompress the directory the same logic can be applied with gunzip.

Another way is to convert the directory into a single file, if you don't have tar to do that, but have the dd util, like demonstrated in this post : https://askubuntu.com/questions/626634/converting-a-file-to-directory

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .