0

I have compressed text files in the following folder structure:

~/A/1/1.faa.tgz #each tgz file has dozens of faa text files
~/A/2/2.faa.tgz
~/A/3/3.faa.tgz

I would like to extract the faa files (text) from each tgz file and then join them using the subfoldername (1,2 and 3) to create a single text file for each subfolder.

My attempt was the following, but the files were extracted in the folder where I ran the script:

#!/bin/bash

for FILE in ~/A/*/*.faa.tgz; do
tar -vzxf "$FILE"
done

After extracting the faa files I would use "cat" to join them (for example, using cat *.faa > .txt.

Thanks in advance.

Fernando
  • 93
  • 1
  • 9

1 Answers1

0

To extract: #!/bin/bash

for FILE in ~/A/*/*.faa.tgz; do
    echo "$FILE"
    mkdir "`basename "$FILE"`"
    tar -vzxf "$FILE" -C "`basename "$FILE"`"
done

To join:

#!/bin/bash
for dir in ~/A/*/; do
(
    cd "$dir"
    file=( *.faa )
    cat "${file[@]}" > "${PWD##*/}.txt"
    )
done
Fernando
  • 93
  • 1
  • 9