GooseIt
GooseIt

Reputation: 69

Extract tar achives into separate folders

I have a folder containing many tar archives

I'd like to write a bash script which extracts their innard files to separate folders:

My best shot so far is this script:

for file in ./*tar; do tar -xf $file -C .; done

The problem with it is that it just dumps all the files into the . folder.

I'm really puzzled about how can I specify the destination folder after -C, or there is another way to make this work.

Upvotes: 0

Views: 395

Answers (1)

Barmar
Barmar

Reputation: 782693

Create a subdirectory named after the tarfile and use that instead of .

for file in *.tar; do
    dir=$(basename "$file" .tar)
    mkdir "$dir"
    tar -xf "$file" -C "$dir"
done

Upvotes: 1

Related Questions