I have many sub directories, each one of them contains .txt files and a .fastq file. I want to do something on each txt file and their corresponding file in each sub directory
#!/bin/bash
dir=$1
subdirectories=$(find $dir -type d) # find only subdirectories in dir
for subdir in $subdirectories;do
cd $subdir
p_files=$(find . -name '*v1.txt') # find all the txt files in that subdirectory
fq_file=$(find . -name '*v1.fastq') # find all the fastq files in that subdirectory
for f in $p_files;do
for q in $fq_file;
do
grep -A3 -f "$f" "$q" > "${f%.txt}_ex.fastq"
done
done
done
I want to grep on each single txt file in each directory with its fastq counterpart and output a new file.
Suppose you have these files (
treeis an installed program...):Rather than using
finda bunch of times you can do something like this with a recursive glob:In this micro example, prints:
As you go through the loop now,
$fqand$ftare set to the path name (relative to where the loop started) and you should be able to do what you want.For Bash, make sure that
shopt globstaris set to on withshopt -s globstar.