0

I'm not very familiar with the terminal but I'm trying to join several splitted .tar.gz files like

  • file1.tar.gz_aa
  • file1.tar.gz_ab
  • file2.tar.gz_aa
  • file2.tar.gz_ab

within the same folder but I can only manage to get them all in the same file using:

for i in *.gz_; do cat ${i%/}* > "${i%/}".joined; done

I've tried "nesting" loops like so:

for i in *.gz_;do for y in ${i}.gz_* do cat ${i}.gz_${y} > ${i}.joined; done; done

but I get a syntax error near unexpected token `>'

Edit: I'm using Xubuntu for this (not sure if it matters) and my objective is to joind files in their original state (file1.tar.gz and file2.tar.gz). I'm also trying to get just the filenames into an array using ${name%.tar.gz*} but I'm so new to this I don't know yet how to remove duplicates and then "cat" them (sort -u hasn't worked)

2 Answers2

0

Try this ..

zcat file1.tar.gz_aa file1.tar.gz_ab file2.tar.gz_aa file2.tar.gz_ab | gzip -c > joined.gz
bingi
  • 78
  • 7
  • Thank you, I'm getting an "unexpected end of file" error using that command + I need to select those files without specifying their names like adding them to a variable inside the loop or something but I can't manage to find out how. – Adrian Skar Apr 16 '18 at 12:58
  • find /root -maxdepth 1 -type f -name '\*.gz_\*' | gzip -c > joined1.gz – bingi Apr 17 '18 at 00:58
  • replace /root with your path. Please let me know if you are still having issue – bingi Apr 17 '18 at 00:59
  • That and this `find -type f -name '*.gz_*' | gzip -c > joined1.gz` inside that folder creates a 52bytes file with that name but doesn't join them. In any case I'd expect that to join them all in one archive but my target is file1.tar.gz and file2.tar.gz. I'll edit the original to clarify this. – Adrian Skar Apr 17 '18 at 06:50
  • Try this `find /root -maxdepth 1 -type f -name 'file1*.gz_*' | cat > file1.tar.gz` – bingi Apr 17 '18 at 07:15
  • Thank you but that makes a file containing just the list of files matched and I need to be able to select those files without specifying their names, that's why I'm trying to do this using loops. Otherwise I'd just go ahead and do `cat *.gz_* > joined.gz` or use the first loop I posted – Adrian Skar Apr 17 '18 at 09:11
0

Yayy, I finally got it!! I'm sure and hope someone with more knowledge can simplify this but it currently does what I wanted,

1st: Assign filenames to an array without their extension

for name in *.gz_*; do arrname+=(${name%.tar.gz*}); done 

2nd: Get rid of duplicates (from this other question)

uniq=($(echo "${arrname[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' '))

3rd: Join each in their own original state

for i in ${uniq[@]}; do cat $i.tar.gz_** > $i.joined.tar.gz; done

All at once: for name in *.gz_*; do arrname+=(${name%.tar.gz*}); done && uniq=($(echo "${arrname[@]}" | tr ' ' '\n' | sort -u | tr '\n' ' ')) && for i in ${uniq[@]}; do cat $i.tar.gz_** > $i.joined.tar.gz; done

Community
  • 1
  • 1