zipping functionality (tar) not working as expected

Hi all,

I here have an index file ($index) which lists the full paths of some files, and am tying to use "tar" to zip all of them.

I ran a command like below,

cat $index | xargs tar -rcf $archived_file

Strangely I noticed only part of files in that index were zipped in my $archived_file.

Where are the other files?

Thanks,

may you please post the content of the file referenced by $index?

The -c option on your tar command is causing a new file to be created with each invocation. When there are more files in your input file than can be placed on the command line xargs will invoke tar multiple times, and with each invocation tar creates a new file. What you are seeing in the final tar file is the set of files that were placed on the command line to tar with the last invocation.

To solve this problem, just remove the -c option:

xargs <$index  tar -rf $archived_file
 

You also don't need to use cat; xargs can read from stdin so the index file can be redirected in making the process more efficient.

EDIT: One more thought...
Because the -r option always appends if the target file exists, you should always remove the file before executing your tar command:

rm -f $archived_file     # ensure it doesn't exist
xargs <$index  tar -rf $archived_file
2 Likes

Hi Agama

Thanks - your advice made great sense. Just one more question - if I simply run that command, it will tell me something like,

"tar: $archived_file: No such file or directory"

So do I need to used "touch $archived_file" to create an empty archived_file first?

Thanks

Is it saying exactly "$archive_file" or the actual file name? If it's the actual file name, then your version of tar might require you to create an empty/dummy tar file to extend. You can try touching the file, that's certainly the easiest, but if your version of tar won't allow an empty file to be extended, then you'll need to create a real tar file to extend. Probably the easiest way to do this is:

echo "tar created on $(date)" >marker
tar -cf $archive_file marker
 

Hope this helps.