Pergunta

I use the following command to create a tar.gz

tar -pczf domain.com.tar.gz  /var/www/domain.com/public_html/www/ 

The tar.gz file contain the following folders

var-->www-->domain.com-->public_html-->www-->then the content of www

I want the tar.gz file to contain only the content of www without any folder.I tried to use -C but i get

Cowardly refusing to create an empty archive error

tar -pczf domain.com.tar.gz -C  /var/www/domain.com/public_html/www/ 

Edit 1 I added the . but then the exclude no longer work and all files have a ./ in their filename

Filename become like .\file.txt in archive

tar -pczf domain.com.tar.gz -C  /var/www/domain.com/public_html/www/ . --exclude='/var/www/domain.com/public_html/www/tmp/*'

Real example

tar --exclude='tmp/cookies/*' --exclude='tmp/logs/*' -pczf filterbypass.me.tar.gz -C /var/www/filterbypass.me/public_html/www .

I want tar,gz to contain only my files in www , not folders + not ./ in filename

Foi útil?

Solução 2

You can't put the complete path into -C, if you want to tar the content of www. Do this instead:

tar -pczf domain.com.tar.gz -C /var/www/domain.com/public_html/www .

That way 'www' is the directory to be tarred but omited itself by including it into the -C path. You would than later extract all files of the 'www' directory.

In addtion to your edit (exclude) it must look like this:

tar --exclude=tmp -pczf domain.com.tar.gz -C /var/www/domain.com/public_html/www .

EDIT

According to your wishes, I found a funny but working solution. You exclude the dirs you want with exclude (see the man page of your tar, there are some with --no-recurse option, too) and you will have no ./ syntax at all:

ls /var/www/domain.com/public_html/www | xargs tar --exclude=tmp -C /var/www/domain.com/public_html/www -pczf domain.com.tar.gz

The way you give the filenames to the input, is the way tar is storing it. So it is even possible with -C to store the files without ./ but you need to pipe the list of ls with | xargs to tar.....

Outras dicas

After reading all this good answers for different versions and having solved the problem for myself, I think there are very small details that are very important, and rare to GNU/Linux general use, that aren't stressed enough and deserves more than comments.

So I'm not going to try to answer the question for every case, but instead, try to register where to look when things doesn't work.

IT IS VERY IMPORTANT TO NOTICE:

  1. THE ORDER OF THE OPTIONS MATTER: it is not the same put the --exclude before than after the file option and directories to backup. This is unexpected at least to me, because in my experience, in GNU/Linux commands, usually the order of the options doesn't matter.
  2. Different tar versions expects this options in different order: for instance, @Andrew's answer indicates that in GNU tar v 1.26 and 1.28 the excludes comes last, whereas in my case, with GNU tar 1.29, it's the other way.
  3. THE TRAILING SLASHES MATTER: at least in GNU tar 1.29, it shouldn't be any.

In my case, for GNU tar 1.29 on Debian stretch, the command that worked was

tar --exclude="/home/user/.config/chromium" --exclude="/home/user/.cache" -cf file.tar  /dir1/ /home/ /dir3/

The quotes didn't matter, it worked with or without them.

I hope this will be useful to someone.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top