0

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.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Mike K
  • 133
  • 5
    Please don't paste images, just copy and paste the text instead. – cutrightjm Sep 10 '16 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. – Chris Davies Sep 10 '16 at 23:22

1 Answers1

4

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.

Chris Davies
  • 116,213
  • 16
  • 160
  • 287