0

I have hundred of folders, some with with tar.gz files and some with bam files. I just wanted to extract tar.gz files into a different directory. Folders with tar.gz files looks like mentioned below:

d1017f74-3a39-4427-af57-273e34247b49
           ├──--- UNCID_2207021.7b9569bc-f513-4b64-9a7c-7bb53b9be79b.110801_UNC12-SN629_0115_BD0DVEABXX_3_ACAGTG.tar.gz
5e2d5c52-596f-49bc-967c-42129abbacbf
           ├──--- UNCID_2208720.71b58051-3bf8-4dfb-a431-c8aceab7c799.110608_UNC13-SN749_0073_BD0CV8ABXX_2.tar.gz
2ef74f93-5da2-454c-aca2-d86c289eacb8
           ├──--- UNCID_2206802.25be50e7-7705-492d-a44a-0e40180d10c8.110901_UNC12-SN629_0127_BC025UABXX_1_CTTGTA.tar.gz
e01ca3e0-beb0-46b7-bb7c-f5b16f966918
           ├──--- UNCID_2521679.d817dcee-1322-4949-a6e9-138447e6fc56.140417_UNC13-SN749_0343_BC41HBACXX_5_CTTGTA.tar.gz
992a7083-28ce-4857-898e-9d4b4fbf2fa1
           ├──--- UNCID_2319278.bf92b8cc-9a5c-4e96-917c-c264fe588f8d.131118_UNC12-SN629_0336_AC31D0ACXX_5_ACTTGA.tar.gz

For extracting tar.gz files into a target directory I have used following command:

find /path/to/directory -name '*.tar.gz' -execdir tar -C /path/to/targetdirectory -xzvf '{}' \;

This command gives output fastq files for some tar.gz files with names like below:

110801_UNC12-SN629_0115_BD0DVEABXX.3_1.fastq
110801_UNC12-SN629_0115_BD0DVEABXX.3_2.fastq

110608_UNC13-SN749_0073_BD0CV8ABXX.2_1.fastq
110608_UNC13-SN749_0073_BD0CV8ABXX.2_2.fastq

110901_UNC12-SN629_0127_BC025UABXX.1_1.fastq
110901_UNC12-SN629_0127_BC025UABXX.1_2.fastq

This is not what I want. There is a text file with Foldernames, tar.gz file names, sample names like following:

FolderNames                                         Tar.gz files                                                                                 SampleNames
d1017f74-3a39-4427-af57-273e34247b49    UNCID_2207021.7b9569bc-f513-4b64-9a7c-7bb53b9be79b.110801_UNC12-SN629_0115_BD0DVEABXX_3_ACAGTG.tar.gz   S-E9-A1NA-11A
5e2d5c52-596f-49bc-967c-42129abbacbf    UNCID_2208720.71b58051-3bf8-4dfb-a431-c8aceab7c799.110608_UNC13-SN749_0073_BD0CV8ABXX_2.tar.gz    S-AO-A12H-01A
2ef74f93-5da2-454c-aca2-d86c289eacb8    UNCID_2206802.25be50e7-7705-492d-a44a-0e40180d10c8.110901_UNC12-SN629_0127_BC025UABXX_1_CTTGTA.tar.gz   S-AC-A23E-01A
e01ca3e0-beb0-46b7-bb7c-f5b16f966918    UNCID_2521679.d817dcee-1322-4949-a6e9-138447e6fc56.140417_UNC13-SN749_0343_BC41HBACXX_5_CTTGTA.tar.gz   S-C8-A8HR-01A
992a7083-28ce-4857-898e-9d4b4fbf2fa1    UNCID_2319278.bf92b8cc-9a5c-4e96-917c-c264fe588f8d.131118_UNC12-SN629_0336_AC31D0ACXX_5_ACTTGA.tar.gz   S-A8-A08A-01A

Output should get sample name from text file and should look like below:

S-E9-A1NA-11A.3_1.fastq
S-E9-A1NA-11A.3_2.fastq

S-AO-A12H-01A.2_1.fastq
S-AO-A12H-01A.2_2.fastq

S-AC-A23E-01A.1_1.fastq
S-AC-A23E-01A.1_2.fastq

S-C8-A8HR-01A.5_1.fastq
S-C8-A8HR-01A.5_2.fastq

S-A8-A08A-01A.5_1.fastq
S-A8-A08A-01A.5_2.fastq
0

Something like this then?

while read -r folder tarfile sample; do
  find /path/to/directory/"$folder" -name "$tarfile" -execdir sh -c '
        tar -C /path/to/targetdirectory -xzvf "$0";
        for f in *.fastq; do
            mv "$f" "$1.${f#*.}";
        done' {} "$sample" \;
done <Foldernames

I'm not altogether sure you need find here at all, but your exposition doesn't reveal what exactly /path/to and its subdirectories refers to, so I assume you know what you are doing there (or how to stop doing that if you don't).

8
  • From tar.gz file I directly need fastq's with sample names as output. And in your code where did u mention about text file? – beginner Apr 14 '18 at 12:44
  • I assumed you have a file named Foldernames. This renames the extracted fastq files like you requested. – tripleee Apr 14 '18 at 12:57
  • Ok. Could you please tell what does "$1.${f#*.}" mean? – beginner Apr 14 '18 at 13:10
  • "$sample" is passed in as $1. The parameter substitution ${f#*.} obtains the value of $f with the beginning of the string up through the first dot is trimmed off. (If there are no dots, no trimming will occur.) – tripleee Apr 14 '18 at 13:21
  • This is the error I got. find: ‘0027045b-9ed6-45af-a68e-f55037b5184c,UNCID_2210188.c71ca9f7-248f-460c-b5d3-afb2c648fef2.110412_UNC13-SN749_0051_AB0168ABXX_4.tar.gz,S-B6-A0I8-01A\r’: No such file or directory – beginner Apr 14 '18 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.