'Extract tar achives into separate folders
I have a folder containing many tar archives
- archive1.tar
- archive2.tar
- ...
I'd like to write a bash script which extracts their innard files to separate folders:
- archive1
-
- file1-1
-
- file1-2
-
- ...
- archive2
-
- file2-1
-
- file2-2
-
- ...
- ...
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.
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |