1

I have the following directory structure and files:

test:
total 16
drwxr-xr-x 2 db2root1 db2iadm1 4096 Jan 18 16:52 archive
-rw-r--r-- 1 db2root1 db2iadm1   34 Jan 18 16:38 file1.txt
-rw-r--r-- 1 db2root1 db2iadm1   34 Jan 18 16:38 file2.txt
drwxr-xr-x 2 db2root1 db2iadm1 4096 Jan 18 17:04 subdir

test/archive:
total 0

test/subdir:
total 4
-rw-r--r-- 1 db2root1 db2iadm1 34 Jan 18 16:38 file3.txt

Now I want to create a tar file in the directory archive without the directory archive and the file file1.txt with the following command:

tar --remove-files --exclude file1.txt --exclude archive -cvzf /test/archive/test.tar.gz /test

I put the --exclude and --remove-files options to different places but always get the following error message:

tar: Removing leading `/' from member names
/test/
/test/subdir/
/test/subdir/file3.txt
/test/file2.txt
tar: /test: Cannot rmdir: Directory not empty
tar: Exiting with failure status due to previous errors

How can I prevent the error regarding the base directory /test?

2 Answers 2

3
( cd /test; tar --remove-files --exclude file1.txt --exclude archive -cvzf archive/test.tar.gz * )

The trick is to cd into the directory and then tar *; the subshell will prevent the cd having any effect in the upper-level shell.

0
0

You're trying to use --remove-files, which will "remove files after adding them to the archive".

You're placing the tarball at /test/archive/test.tar.gz, and tarring up /test/. The --remove-files option is telling tar to delete the directory /test/ at the end, but so long as tar is running it will be accessing the file /test/archive/test.tar.gz, so you wont be able to delete the directory.

You can either throw out the --remove-files option, or place your tar in a different directory.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .