Tar command fails

I am on Oracle Linux:

tar -cpzf export.tar.gz /dir/dir1/dir2/*

bash: /bin/tar: Argument list too long

I tried the workaround

from the folder /dir/dir1/dir2 :

find . -name '*' -print | tar -cpzf export.tar.gz --files-from �

My issue is that the command inludes the newly created tar file as part of the tar .

How can I ensure if the tar only includes the files in the sub directory dir2 and not the tarfile itself?

Thanks much!

Try creating the archive outside of the directory where find is working on.

find . -print | tar cpzf ../export.tar.gz -T -
1 Like

Thanks This worked!!

You could also just use:

find . ! -name export.tar.gz | tar -cpzf export.tar.gz --files-from -

I spoke too soon

I am not sure if I did something wrong, I see 2 files with the same name in the tar file created in the uppper level of subdirectory.

File names can have the same name, but not the same path (location where they leave)

Example of two files with the same name

$ tree
.
 dir1
    sumang24.file
 dir2
     sumang24.file

Creating a tar.gz file outside the directory sumang24

$ tar cpvzf ../sumang24.tar.gz *
dir1/
dir1/sumang24.file
dir2/
dir2/sumang24.file

Now, checking the content of sumang24.tar.gz

$ tar tf sumang24.tar.gz
dir1/
dir1/sumang24.file
dir2/
dir2/sumang24.file

For further understanding, you might need to post some more information.
Please, post the following:

  • The result of pwd before issuing the command
  • The output entries shown by the command tar tf export.tar.gz for only those two files you say they are the same.
  • The exact command that you used to tar and gzip.
  • And any clear explanation of what you expected but it was not so.
1 Like

Did you remove the export.tar.gz created by your first run before running again using ../export.tar.gz as the output file?

Did you try using the find pipeline I suggested in post #4 in this thread?

Yes I did remove the export.tar.gz created by the first run

---------- Post updated at 12:41 AM ---------- Previous update was at 12:37 AM ----------

I also tried the pipe command to exclude the *.tar.gz file but looks like it is being processed by the command as well.

"tar: ./export.tar.gz: file changed as we read it"

$ find . ! -name export.tar.gz | tar -cpzf export.tar.gz --files-from -

tar: ./export.tar.gz: file changed as we read it

---------- Post updated at 12:44 AM ---------- Previous update was at 12:41 AM ----------

Here is a sample output from

tar tf sumang24.tar.gz

-rw-r--r-- user/user 33935 2015-12-05 13:41 ./File1.xml
-rw-r--r-- user/user 33935 2015-12-05 13:41 ./File1.xml

Actually, this is happening because find is returning the current directory as well. If you do a find . ! -name export.tar.gz the first result is .
This might work.

find . ! -name export.tar.gz ! -name . | tar -cpzf export.tar.gz --files-from -
1 Like

OK: I have kicked off the command suggested by you again

$ find . ! -name export.tar.gz ! -name . | tar -cpzf export.tar.gz --files-from -

Thanks!

I do believe one of these is a link.

I am sorry, but it appears that it would create links as well and you are going to see what it appears to be the same file several times.

A better suggestion would have been
tar cpzf ../export.tar.gz .

Or perhaps even

tar cpzf export.tar.gz /dir/dir1/dir2

without the original trailing * , from a different directory than /dir/dir1/dir2

1 Like

From: /dir/dir1

$ tar cpzf export.tar.gz /dir/dir1/dir2
tar: Removing leading `/' from member names

Is that OK that it is removing leading `/' from member names?

Yes, it is more than OK. If not when you extract the archive it would overwrite the original /dir/dir1/dir2 directory, if exists, instead of extracting it in the current directory as dir/dir1/dir2

Now, if you really, would like to preserver the full path, and overwrite the original path when extracting, then you use the P flag

The above command still gave me single files.
The output looks like:
-rw-r--r-- user/user 33935 2015-12-05 13:41 ./File1.xml
-rw-r--r-- user/user 67306 2015-12-04 20:43 ./File2.xml
-rw-r--r-- user/user 39804 2015-12-05 15:01 ./File3.xml

---------- Post updated at 02:18 AM ---------- Previous update was at 02:16 AM ----------

The output of the following command:

tar -ztvf export.tar.gz

---------- Post updated at 02:22 AM ---------- Previous update was at 02:18 AM ----------

Looks like this command
$ tar cpzf export.tar.gz /dir/dir1/dir2

Is creating duplicates. I see the tarfile size has outgrown (by 1G ) the tarfile created via
find . ! -name export.tar.gz ! -name . | tar -cpzf export.tar.gz --files-from -

1 Like

You can always use the v flag to let you know what's going on, like:

$ tar cvpzf export.tar.gz /dir/dir1/dir2

or log it

$ tar cvpzf export.tar.gz /dir/dir1/dir2 > /tmp/export.tar.log

That will show up.

I used this command to see the contents:

tar -ztvf export.tar.gz > /tmp/list_tar_ztvf.out

find is recursive like tar - together they might process files multiple times.
The following makes the find non-recursive

find /dir/dir1/dir2 -mindepth 1 -maxdepth 1 | tar -cpzf export.tar.gz --files-from -