简体   繁体   English

For循环:处理UNIX中不同文件夹中的文件

[英]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. 因此,我在当前目录中有数百个文件夹,名称从folder1000到folder1500。 In each folder, I have one .fastq file with a different name (Lib1.fastq, Lib2.fastq, etc). 在每个文件夹中,我都有一个.fastq文件,其名称不同(Lib1.fastq,Lib2.fastq等)。 I want to proceed each of these files in one loop command by running a shell script. 我想通过运行Shell脚本在一个循环命令中继续处理这些文件。

Here is my shell script (script.sh) for one file (it creates outputs which further proceeded) which I run in my Terminal: 这是我在终端中运行的一个文件的shell脚本(script.sh)(它将创建输出,并进一步处理):

#!/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: 这是我尝试在Shell脚本中执行的循环(在这里,我仅从一个简单的check“ head”命令开始,并且仅从5个第一个文件夹开始),该循环从所有文件夹所在的当前目录运行:

#!/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). 然后我得到正确的结果(显示文件的前10行)。

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. 好吧,您正在使用变量$file遍历文件夹,但没有在循环体中使用此变量。 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 :-) 让我们一一解决问题:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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