1

I have bunch *.tgz files and when I untar them some how the directory name is different than the name of the tgz file. Example: I have the ab.tgz file and it get change to something like 2012_test_ab, I have feeling this happens because during compression file must have gotten rename of this compress file was within a parent directory etc.

But what I want to do is rename the directory to original name of the file.

I have following one line script to run on terminal, but how can I rename?

for i in *.tgz; do tar xvf $i -C ../stats/; done

How can I modify this to rename the directory thats getting unzip.

Example: When I untar ab.tgz it gets extracted to directory name is 2013_03_12_18_26_ab and the files inside that directory but what I want to do is keep the directory name as ab instead of 2013_03_12_18_26_ab

I want ab.tgz to get extract to ../stats/ab

add-semi-colons
  • 18,094
  • 55
  • 145
  • 232

2 Answers2

1

Something like this might work. It's early though, so my syntax could be a bit off.

for i in *.tgz
do 
NEWDIR=`tar -tf $i | head -1 | cut -d/ -f2 | cut -d_ -f6` 
mkdir ../stats/$NEWDIR 
tar xvf $i -C ../stats/$NEWDIR/ 
done

or

for i in *.tgz; do NEWDIR=`tar -tf $i | head -1 | cut -d/ -f2 | cut -d_ -f6`; mkdir ../stats/$NEWDIR; tar xvf $i -C ../stats/$NEWDIR/; done
Mike Gardner
  • 6,611
  • 5
  • 24
  • 34
  • `for i in *.tgz; do NEWDIR = `tar -tf $i | head -1 | cut -d/ -f1` mkdir $NEWDIR tar xvf $i -C ../stats/$NEWDIR; done` tried it like this get error NEWDIR command not found – add-semi-colons Mar 21 '13 at 13:32
  • hmm still giving me same error `#for i in *.tgz; do NEWDIR = ``tar -tf $i | head -1 | cut -d/ -f2`` mkdir $NEWDIR tar xvf $i -C ../stats/$NEWDIR; done` – add-semi-colons Mar 21 '13 at 15:28
  • Error says bash: NEWDIR: command not found. I am bit confuse. – add-semi-colons Mar 21 '13 at 15:44
  • yes I am using single quote and getting the same error yes I use single back quote the key shares with tilda? – add-semi-colons Mar 21 '13 at 16:11
  • 1
    Oh, I see the issue. The variable assignment can't have spaces around the =. It must be VAR=value, not VAR = value. – Mike Gardner Mar 21 '13 at 16:21
  • so my tgz file name is ab.gz but it get expand to 2013_03_12_18_26_ab how can I just keep ab only as a directory that get expand too – add-semi-colons Mar 21 '13 at 16:24
  • just updated the question – add-semi-colons Mar 21 '13 at 16:29
  • I will try that.. yes it will always in that format can I try this on one line..? if not one line should be running as a shell script..? – add-semi-colons Mar 21 '13 at 16:36
  • strange thing is when I try oneline I get the dir name as I mentioned in the question: `2013_03_12_18_26_ab 2013_03_12_18_26_ckb 2013_03_12_18_26_hif 2013_03_12_18_26_lmo 2013_03_12_18_26_pap 2013_03_12_18_26_szl 2013_03_12_18_26_ace` – add-semi-colons Mar 21 '13 at 16:39
  • Since the solution didn't work I am going to manually do rename dir, `mv 2013_03_12_18_26_ace/ ace` – add-semi-colons Mar 21 '13 at 16:45
  • Yes I did, same issue still there I posted a new question http://stackoverflow.com/questions/15553425/rename-more-than-one-directory-at-once – add-semi-colons Mar 21 '13 at 16:52
  • It works fine on my end, so maybe I'm just not following what you are asking. What happens when you run tar -tf ab.tgz | head -1 | cut -d/ -f2 | cut -d_ -f6 – Mike Gardner Mar 21 '13 at 16:57
1

Assuming that all you tar archives end with .tgz extension and there could be multiple dots in the original file name, what about:

for i in *.tgz; do tname=$(echo $i | sed -e 's/.tgz//') ; tar xvf $i -C ../stats/$tname; done
Ashish Kumar
  • 811
  • 4
  • 8