Question: Correct use of bash wildcards
1
gravatar for caro-ca
6 weeks ago by
caro-ca20
caro-ca20 wrote:

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.

bash wildcards • 169 views
ADD COMMENTlink modified 6 weeks ago by Mensur Dlakic6.6k • written 6 weeks ago by caro-ca20
1
gravatar for JC
6 weeks ago by
JC11k
Mexico
JC11k wrote:

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

check https://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-7.html

ADD COMMENTlink modified 6 weeks ago • written 6 weeks ago by JC11k

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.

ADD REPLYlink written 6 weeks ago by caro-ca20

at first sight you will need a double loop for this, fist loop all dirs and for each loop all files

ADD REPLYlink written 6 weeks ago by lieven.sterck8.5k

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.

ADD REPLYlink modified 6 weeks ago • written 6 weeks ago by caro-ca20
1

indeed, because you're still writing to the same (and single) result file.

try this:

for dir in /hosts/strains/SRR*/results;  do for file in $dir/*nonredundant.vcf; do ls $file >> ${dir}/sample ; done; done
ADD REPLYlink written 6 weeks ago by lieven.sterck8.5k

Thank you so much!! It worked!!

ADD REPLYlink written 6 weeks ago by caro-ca20

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
ADD REPLYlink written 6 weeks ago by lieven.sterck8.5k
1
gravatar for Mensur Dlakic
6 weeks ago by
Mensur Dlakic6.6k
USA
Mensur Dlakic6.6k wrote:

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
ADD COMMENTlink modified 6 weeks ago • written 6 weeks ago by Mensur Dlakic6.6k

Thank you for your reply.

ADD REPLYlink written 6 weeks ago by caro-ca20
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1720 users visited in the last hour