Trying to delete files using a list of the headers and a wildcard. The headers for the files are the same but the endings are different. Not sure how to get the wildcard in there.

I was trying to download some SRA files from NCBI and ran into download issues. Files that didn't download correctly created file1.fastq and file1.sra.cache (and so on for file2, file3, etc...) Files that downloaded correctly give me file.fastq and file.sra.cache, so my downloads look like this:


Where file1 is a successful download, but not file2 or file3. I want to delete all files associated with file2 and file3.

I figured out which files didn't download correctly with ls *.sra.cache. I now have a list of the faulty file headers (e.g., file2, file3, ...; so just the beginning part). How could I feed in the list of filenames and add a wildcard to remove them?

I'd like to make a list of filenames with a wildcard, like


and do something like cat list.txt | xargs rm, but I'm not sure how to get the wildcard in there to work. Unix thinks * is part of the filename if I put it into the list itself.

2 Answers 2


No need to use a separate file list or alike, you can use a simple for-loop like:

for f in *.sra.cache; do
    rm -- "${f%sra.cache}"*

${f%sra.cache} removes the sra.cache extension from the file name.

  • 2
    I recommend only removing sra.cache, not the . before it. Otherwise, when you mean to remove file1.* you'll also remove file10.*, file11.*, etc.
    – Barmar
    Jan 17, 2022 at 15:24
  • that is totally correct, thanks fore edit!
    – pLumo
    Jan 17, 2022 at 21:10

With zsh:

rm -f -- *.sra.cache(e['reply=($REPLY $REPLY:r:r.fastq)'])

Which removes the .sra.cache and corresponding .fastq files for each *.sra.cache file.

$var:r gives you the root name of $var like in csh / vim ($var with one extension removed).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .