Tar directory to file in the directory being tar'ed

6,216

Solution 1

You can use --exclude=pattern if you need to, though I'm not sure how you're doing where you need to do this.

Running the following:

tar -cf test.tar .

I get the message:

tar: ./test.tar: file is the archive; not dumped

but perhaps that's just a GNU extension, though my BSD version gives a similar

tar: ./test.tar: Can't add archive to itself

Still, you could tell it explicitly to ignore it with

tar -cf test.tar --exclude=./test.tar .

Solution 2

I meet a similar problem too.

It may works like this, in the example

tar -cf test.tar .

You are asking tar to archive ., then tar create an "empty" file test.tar to store the content. But the point is that test.tar is also part of the directory .. It looks a little like infinite recursive. Anyhow, tar seems pretty smart and prevent that, since in most case that is not what you want.

Besides using --exclude, you can also try

tar -cf test.tar ./*

In this way, you are asking tar to archive (almost) all files in . before the command runs, note that test.tar is not included.

Share:
6,216

Related videos on Youtube

Janus Troelsen
Author by

Janus Troelsen

Updated on September 18, 2022

Comments

  • Janus Troelsen
    Janus Troelsen over 1 year

    I have a mount-point, /media/xvdf1 which I use bindfs to mount at /var/lib/jenkins/jobs with user/group jenkins. I wish to back it up, but I do not have enough space on the other partition. After tarring, the file should be uploaded to S3 using aws s3 cp.

    My problem is that I if I use tar to create the archive, it tries to tar the file that it is writing to.

    Isn't there a way have tar avoid tarring its own output?

  • Janus Troelsen
    Janus Troelsen over 7 years
    The reason it does not detect the problem is probably because of the bindfs mount.