How do I delete all files that start with 'labtest,' but keep the file 'labtest' itself undeleted?

I was trying

rm labtest*[!"labtest"]

But that didn't work out too well.

Question 5

Enter a command to delete all files that have filenames starting with 'labtest',
 except 'labtest' itself, from the current directory (Delete all files starting
with 'labtest' followed by one or more characters).

You entered: rm *labtest?![labtest]
Please try again.

  • 5
    Please don't paste images, just copy and paste the text instead.
    – cutrightjm
    Sep 10, 2016 at 18:53
  • 2
    @cuonglm not sure it's a duplicate. Your suggestion deletes all files except one. This one desires all files with a specified prefix except the prefix itself. I don't think it's obvious to a relative learner how to apply your suggested duplicate to this situation when they clearly haven't got their head around globbing wildcards.
    – roaima
    Sep 10, 2016 at 23:22

1 Answer 1


This will delete all files in the current directory starting with labtest except for the file labtest itself. It will not consider files unless they begin with labtest

echo labtest?*    # List files that begin with "labtest"

rm labtest?*      # Delete them

The question-mark is a single character wildcard: it must match something. The asterisk is a globbing wildcard and will match zero or more characters. The combination ensures that we cannot match just labtest by itself.

  • This is what I was looking for. Couldnt find it anywhere online. Thank you so much
    – Mike K
    Sep 11, 2016 at 17:16

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.