I want to check if each .bam file is accompanied with a .bai file. So if clean_xyz_1.sorted.bam is present clean_xyx_1.sorted.bam.bai should also be present. Each file has a variable string in the middle (xyz). I want to check multiple folders to make sure both files are present. If both files are not present, I want to run a command. However, I have not been able to check for both files in multiple directories. Here is what I have tried:
dirs=(*/)
clean="clean_"
sorted="_1.sorted.bam"
for i in "$dirs"/"$clean"*"$sorted"*; do
if [[ ! -e "$i".bai ]]; then
samtools index "$i"
fi
done
The command works fine and creates a '.bai' file. However, it only opens the first directory. Is there a way to expand all directories?
1.bam
is present1.bam.bai
should also be present" - your example code appears only to consider.bam
files matching the pattern*/clean_*_1.bam
but the description only refers to*.bam
files. Which is correct?