Thank you so much for your reply, however, I got one single file containing all the filenames in the directory /hosts/strains/, instead I want to have a file in each /hosts/strains/SRR*/results/ containing all VCF filenames.
Hi, community!
I am not an expert in bash, but I am trying to collect all filenames (ls *vcf > sample) in a file per folder by using a single command line as follow:
for file in /hosts/strains/SRR*/results/*vcf ; do ls *vcf > sample ; done
I understand *vcf is assigned incorrectly, but I tried multiple combinations and it does not work.
Additionally I tried using find and -exec as:
find /home/caroca/strains/SRR*/results/ -type f -name "*nonredundant.vcf" -exec ls {} > sample \;
But I only got one big file with the entire *vcf filenames of the entire number of folders, instead, what I need is a single file per folder that includes all VCF filenames in it. I hope you could help me out. Thank you in advance.
2 answers
The for is passing the value in the file variable and you need to append (>>) the results, your command should be:
for file in /hosts/strains/SRR*/results/*vcf ; do ls $file >> sample ; done
at first sight you will need a double loop for this, fist loop all dirs and for each loop all files
Thank you for your reply. I tried:
for dir in /hosts/strains/SRR*/results; do for file in $dir/*nonredundant.vcf; do ls $file >> sample ; done; done
And I don't have individual files per folder, instead, there is one file in /hosts/strains/ where I'm running the code.
alternative for the 'appending' is redirecting the whole loop to a file
for file in /hosts/strains/SRR*/results/*vcf ; do ls $file ; done > sample
How about using find instead of bash scripting:
find /hosts/strains -maxdepth 3 -name "*vcf" | sort > sample
If you really want to use scripting:
rm sample
for dirs in /hosts/strains/SRR*/results ; do find $dirs -name "*vcf" | sort >> sample ; done
Thank you for your reply.
Log in to answer this question.