3

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:

file1.fastq
file1.sra
file2.fastq
file2.sra.cache
file3.fastq
file3.sra.cache

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

file2*
file3*

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

6

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}"*
done

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

2
  • 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 at 15:24
  • that is totally correct, thanks fore edit!
    – pLumo
    Jan 17 at 21:10
1

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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.