1

i have a directory in which there are multiple tar files like

tweets10_1.tar
tweets10_2.tar
tweets10_8.tar

now i want to untar these files and keep them in a directory structure like

10_1
10_2
10_8

I have made a shell script

#!/bin/bash
for string in `ls` ; do
  if [ $string == tweets10_*.tar*]; then
    length=${#string}
    folder=${string:6:$length-10}
    mkdir /mnt/filer01/round2/twitter/$folder
    tar -xvf $string -C /mnt/filer01/round2/twitter/$folder
  fi
done

this is giving me error :3: [: missing ] Please tell what is the problem

2 Answers 2

2

The [ command should be closed using a ] with a leading space:

if [ $string == tweets10_*.tar* ]; then

Also, please don't do

for x in `ls`

Instead, use:

for x in *

Or, better:

for x in tweets10_*.tar

and skip the check altogether.

You can also extract out the 10_x part more easily:

$ a=tweets10_8.tar; echo ${a//[a-z.]/}
10_8

Here, I'm removing the alphabets and the ..

0
#!/bin/bash

DIR="/mnt/filer01/round2/twitter"

for file in tweets*.tar
do
    NEWDIR=`echo $file | tr -d [a-zA-Z.]`
    mkdir $DIR/$NEWDIR
    tar -xvf $file -C $DIR/$NEWDIR
done 

You must log in to answer this question.

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