2

I am trying to extract multiple tar files by creating a directory of the same name as tar file. I am using centos with tar version 1.26 and unfortunately can't update the tar version to 1.29 or above due to user restrictions. So am unable to use the below snippet.

-exec tar xvf {} --one-top-level \;

Trying to replicate the working code I have for.ZIP format but no success.

Working Code for zip file -

for file in `ls *.zip`; do unzip $file -d `echo $file | cut -d . -f 1`; done

Replicated code for tar files -

for f in 'ls *.tar'; do tar xvf $f -C 'echo $f|cut -d . -f 1'; done

The code works until I pass "tar xvf $f -C". This correctly prints the name of files -

for f in 'ls *.tar'; do echo $f|cut -d . -f 1; done

It gives the below error -

$ for f in 'ls *.tar'; do tar xvf $f -C 'echo $f|cut -d . -f 1'; done

tar: ls: Cannot open: No such file or directory

tar: Error is not recoverable: exiting now

Thanks,

Pankaj

4
  • 1
    Make sure you're using back quotes as in the example and not single quotes. Jul 27, 2021 at 11:52
  • I double-checked, am using back quotes
    – Pankaj
    Jul 27, 2021 at 12:43
  • 1
    for f in *.tar; do tar xvf "$f" -C "${f%.tar}"; done. See Bash Pitfall #1 Jul 27, 2021 at 14:03
  • Thanks. I'll try this and update.
    – Pankaj
    Jul 28, 2021 at 14:14

0

You must log in to answer this question.

Browse other questions tagged .