0

I have several scripts that run and collect data which is then fed into other scripts. So this entire chain is automated. At one point in the automation, all data files are tarred up to be sent for processing. The command I am using is this:

tar -zchvf "$target_dir" -C "$data_dir" .

Where target_dir is where my tarball is going and data_dir is where my data is located.

Now this is working fine; however, the files in my tarball look like this:

./file1
./file2
 <etc>

The post processing script is messing up because of the leading ./ on these files. It is expecting them to look like this:

file1
file2
<etc>

I have looked at the man page and nothing really stands out to me for removing the leading ./ in the tar process. One thing I was thinking about doing was changing the script to the following...

cd $data_dir; tar -zchvf "$target" *; cd -

but this just feels very wrong to me and I believe there is something else stupid I am missing.

I am using bash v3.2.5 just as reference.

15
  • I'd recommend instead of changing how the tarball is made to change how your post processing script checks for things. One option would be something like tar -tf ${TARGET_FILE} | sed "s@^[./]*@@g"
    – Centimane
    Commented Jul 24, 2018 at 16:10
  • Or tar ... | cut -c3- Commented Jul 24, 2018 at 16:15
  • 1
    I was thinking about this. Thought it would be easier to fix the tar than the untar though. Will look to see how difficult this is.
    – Sharki
    Commented Jul 24, 2018 at 16:15
  • 1
    Use star, it strips off leading ./ since 35 years.
    – schily
    Commented Jul 24, 2018 at 16:16
  • @roaima that only changes the way its print to the term not how its put into the actual tar
    – Sharki
    Commented Jul 24, 2018 at 16:17

2 Answers 2

1

If you use gnu tar you can edit the filenames on the way into the tar:

$ mkdir -p a/b
$ cd a
$ tar cf - --transform='s:^./::' . | tar tvf - 
drwxr-xr-x        0 2018-07-24 18:18 ./
drwxr-xr-x        0 2018-07-24 18:18 b/
8
  • I would love to use the transform option since I know this works. But as I mentioned I am using bash 3.2.5 and tar 1.15.1 which does not have this available.
    – Sharki
    Commented Jul 24, 2018 at 16:23
  • It makes no difference what version of bash you're using if you're looking at tar invocation.
    – DopeGhoti
    Commented Jul 24, 2018 at 16:24
  • I thought it would matter since different tar versions come with different shell versions.
    – Sharki
    Commented Jul 24, 2018 at 16:31
  • @Sharki tar doesn't come with your shell, tar (and your shell) comes with your distro.
    – Centimane
    Commented Jul 24, 2018 at 16:35
  • Fair enough - for the distro I am working with my tar version does not have that option.
    – Sharki
    Commented Jul 24, 2018 at 16:38
0

Reading tar 1.15.1's documentation,released 2004-12-21, from https://www.gnu.org/software/tar/ 's download link and its included tar.info to be sure:

6.3 Reading Names from a File
[...]

--files-from=FILE NAME' `-T FILE NAME' Get names to extract or create from file FILE NAME.

If you give a single dash as a file name for '--files-from', (i.e., you specify either '--files-from=-' or '-T -'), then the file names are read from standard input.

Now for ls, part of coreutils, reading coreutil.info from coreutils-5.0 from 2003-04-02 (https://ftp.gnu.org/gnu/coreutils/), so predating tar 1.15.1:

'-A'
'--almost-all'
List all files in directories except for '.' and '..'.

So this should work as wished (pair of parenthesis are there to forget the cd, you could probably do cd - instead or just not care):

( cd "$data_dir" && ls -A | tar -zchvf "$target_file" -T - ) 

You must log in to answer this question.

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