Correct use of bash wildcards
2
1
Entering edit mode
3.7 years ago
caro-ca ▴ 20

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 • 1.3k views
ADD COMMENT
1
Entering edit mode
3.7 years ago
JC 13k

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 COMMENT
0
Entering edit mode

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 REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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 REPLY
1
Entering edit mode

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 REPLY
0
Entering edit mode

Thank you so much!! It worked!!

ADD REPLY
0
Entering edit mode

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 REPLY
1
Entering edit mode
3.7 years ago
Mensur Dlakic ★ 27k

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 COMMENT
0
Entering edit mode

Thank you for your reply.

ADD REPLY

Login before adding your answer.

Traffic: 1819 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6