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.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
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.
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
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