简体   繁体   中英

For loop: process files in different folders UNIX

I have tried to find an answer to my question looking at similar topics but didn't succeed. Maybe I have overlooked. Any help is appreciated!

So, I have hundreds of folders in my current directory named from folder1000 to folder1500. In each folder, I have one .fastq file with a different name (Lib1.fastq, Lib2.fastq, etc). I want to proceed each of these files in one loop command by running a shell script.

Here is my shell script (script.sh) for one file (it creates outputs which further proceeded) which I run in my Terminal:

#!/bin/sh
bowtie --threads 4 -v 2 -m 10 -a genome Lib1.fastq --sam > Lib1.sam
samtools view -h -o Lib1.sam Lib1.bam
sort -k 3,3 -k 4,4n Lib1.sam > Lib1.sam.sorted
# ...etc

Here is the loop I am trying to make as well in a shell script (here I have started only with a simple checking "head" command, and only with 5 first folders) which I run from my current directory where all folders are located:

#!/bin/sh

for file in ./folder{1000..1005}
do
head -10 *.fastq
done

But as a result I get:

head: *.fastq: No such file or directory
head: *.fastq: No such file or directory
head: *.fastq: No such file or directory
head: *.fastq: No such file or directory
head: *.fastq: No such file or directory

So, even a simple checking command does not work for me in a loop. Somehow I can not see the file. But if I run the command directly in one of the folders:

MacBook-Air-Maxim:folder1000 maxim$ head -10 *.fastq

then I get the correct result (the first 10 lines of the file displayed).

Could anyone suggest the way to process all files in the most convenient way?

Thanks a lot and very sorry, I am just learning.

Well, you are traversing through the folders using the variable $file , but you are not using this variable in the loop body. Just use it:

#!/bin/sh

for file in ./folder{1000..1005}
do
    head -10 $file/*.fastq
done

There are other issues in the overall problem, but this is the answer to the point that is stopping you. Let's tackle the problems one by one :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM