Home > Back-end >  How to list a number of files with the same ending?
How to list a number of files with the same ending?

Time:06-21

I have a list 15 files all with the ending ******_filteredSNPs.txt. The starred bit is individual names for each of the 15 file. How do I list all these files?

I need the output files to have the individual names at the start but with the ending clumped.

E.g.

cd /data/PRS/

i=$PBS_ARRAYID

file="${i}_filteredSNPs.txt"
out="${i}_clumped"


./plink \
    --bfile filesforPRS \
    --clump-p1 1 \
    --clump-r2 0.1 \
    --clump-kb 250 \
    --clump ${file} \
    --clump-snp-field ID \
    --clump-field P \
    --out ${out}

I am trying the above but get an error as it fails to open my input files.

CodePudding user response:

Your question remains unclear and probably a duplicate, but I'm guessing it's either of the following. Please follow up in a comment, or edit your question to clarify it still.

Perhaps you are looking for a way to run plink on each matching file separately?

for file in *_filteredSNPs.txt; do
    ./plink \
        --bfile filesforPRS \
        --clump-p1 1 \
        --clump-r2 0.1 \
        --clump-kb 250 \
        --clump "$file" \
        --clump-snp-field ID \
        --clump-field P \
        --out "${file%_filteredSNPs.txt}_clumped"
done

Notice also how double quotes (but not braces {...}) are necessary to avoid problems when handling files with unusual characters in their names; see When to wrap quotes around a shell variable? The parameter expansion ${file%_filteredSNPs.txt} returns the value of the variable with the suffix after % removed.

This uses no Bash features, and so will work with any sh variant.

Or, if your plink command allows more than one --clump option, and you want to add them all into the same command line, you can just interpolate them into it.

# Put beginning of command into array
cmd=(./plink \
    --bfile filesforPRS \
    --clump-p1 1 \
    --clump-r2 0.1 \
    --clump-kb 250)
# Add matches to array
for file in *_filteredSNPs.txt; do
    cmd =(--clump "$file")
done
# Then add tail of command
cmd =(--clump-snp-field ID \
    --clump-field P \
    --out "$out")
# Finally, execute it
"${cmd[@]}"

If you have 15 matching files, this will add the --clump option 15 times, each followed by another one of the 15 file names, then run plink once.

Arrays are a Bash feature, so this will not work portably with sh.

  • Related