Thursday, November 18, 2021

[SOLVED] Issues with tar command

Issue

I'm trying to figure out why is the following tar command not working -

I've tried the following 2 versions & both don't work -

Version 1

tar -c --use-compress-program=pigz -f /home/jhonst/data_lake/1m/UX.tar -C '/home/jhonst/data_lake/1m/*.UX.csv'

The error I see is

tar: Cowardly refusing to create an empty archive
Try 'tar --help' or 'tar --usage' for more information.

Version 2

tar -c --use-compress-program=pigz -f /home/jhonst/data_lake/1m/UX.tar -C '/home/jhonst/data_lake/1m/*.UX.csv' .

The error I see is

    tar: 
/home/jhonst/data_lake/1m/*.UX.csv: Cannot open: No such file or directory
    tar: Error is not recoverable: exiting now

Please could someone guide me on what I am doing wrong


Solution

When you do:

tar -c --use-compress-program=pigz -f /home/jhonst/data_lake/1m/UX.tar /home/jhonst/data_lake/1m/*.UX.csv

You'll get:

tar -t -f /home/jhonst/data_lake/1m/UX.tar
home/jhonst/data_lake/1m/file1.UX.csv
home/jhonst/data_lake/1m/file2.UX.csv
...

Which is not the best. There are two possibilities for getting rid of the "path" inside the tar archive:

  • Go inside the directory with cd (in a subshell or with pushd/popd if you want to return to the original directory after the tar command):
cd /home/jhonst/data_lake/1m && tar -c --use-compress-program=pigz -f UX.tar *.csv
# returning to the same place after the tar:
(cd /home/jhonst/data_lake/1m && tar -c --use-compress-program=pigz -f UX.tar *.csv)
# or:
pushd /home/jhonst/data_lake/1m && {
    tar -c --use-compress-program=pigz -f UX.tar *.csv
    popd
}
  • Use the -C option of GNU tar, which is not that easy to handle:
dirpath=/home/jhonst/data_lake/1m
files=("$dirpath"/*.csv)
tar -c --use-compress-program=pigz -f "$dirpath"/UX.tar -C "$dirpath" "${files[@]#$dirpath/}"


Answered By - Fravadona