2

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

user2650277
  • 6,289
  • 17
  • 63
  • 132

2 Answers2

3

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.

user2553863
  • 682
  • 1
  • 8
  • 17
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.....

Jimmy Koerting
  • 1,231
  • 1
  • 14
  • 27
  • Two important points about `tar` and `--exclude`: Don't use *trailing slashes* in your paths and any `--exclude` needs to be the first options in the `tar` command. – Jimmy Koerting Dec 30 '13 at 15:11
  • Its nearly there dude.....now i have `www` folder-->file contents.I don't want the file content to be in any folder.Just correct it so that the content are not inside the `www`. – user2650277 Dec 30 '13 at 15:26
  • ok, than extend the -C with www and put a . at the end. I will edit my Answer in a second. – Jimmy Koerting Dec 30 '13 at 15:27
  • I use it but now my file contain `./` in filename + i got a file with no name and just a .Check my real example edit – user2650277 Dec 30 '13 at 15:45