Shell Script Approach
By default, globs don't work in a BASH script (although you can turn them on with shopt
). If the shell script ever gets run by a non-BASH interpreter, globs might not work at all.
You can get the same effect using the find
command, which is how I'd recommend doing it (because of how much more control you can have once your requirements grow).
Try this on for size:
find . -mindepth 1 -maxdepth 1 -not -iname "*.sh" -exec rm -rf {} \;
Makefile Approach
Another approach you could take:
If you're doing something that wants to be cleaned afterwards, there's a good chance that a Makefile is the right tool for the job, rather than a bunch of clean.sh, build.sh, install.sh, etc.
This is especially true if you want to make sure your recipes always happen in order, or if you don't always want to re-run the recipes that produce an output.
A simple Makefile that did the same thing would look like this:
(note that the whitespace before rm
needs to be a tab because that's how make rolls)
SOURCES := $(wildcard *.sh)
CLEAN_FILES := $(filter-out $(SOURCES),$(wildcard *))
CLEAN_FILES := $(filter-out Makefile,$(CLEAN_FILES))
build: $(SOURCES)
YOUR_RECIPE_HERE
clean:
rm -rf $(CLEAN_FILES)