Thursday, 18 February 2016

Batch extract tarballs into directories with their respective base names (for ILSVRC 2012 training images)

I encountered this problem when I downloaded the ILSVRC 2012 training images. Thanks to this code, we now have a quick solution.

1. Paste the following code to file tarsubdir.sh:
#!/bin/sh
#usage:
# nameOfScript myArchive.tar.gz
# nameOfScript myArchive.gz
# nameOfScript myArchive.tar
#
# Result:
# myArchive   //folder
fileName="${1%.*}" #extracted filename
echo "file name is: $fileName"
trailingExtension="${fileName##*.}"
if [ "$trailingExtension" == "tar" ]
then
    fileName="${fileName%.*}"
    echo "new file name is: $fileName"
fi
echo "now file name is: $fileName"
mkdir "$fileName"
tar -xf "$1" --strip-components=0 -C "$fileName"
2. Paste the following code to another file untarall.sh:
#!/bin/sh

for file in *.tar
do
  bash ./tarsubdir.sh "$file"
done
3. Run from the bash prompt:
bash ./untarall.sh