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.
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
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 -
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.
.tar.gz
...
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.
fork()
.
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
gzip
is notzip
. They are different tools that work differently.gzip
is just stream compressor. It does not know about files or directories.