I would like to delete everything in one folder, with the exception of two files:exec.js.gitI found this:rm !(exec.js|.git)Which works in the CL, but when I try to store in a sh file, and call with bash file.sh, this happens:test.sh: line 1: syntax error near unexpected token `('test.sh: line 1: `rm !(exec.js|.git)'Any ideas why?
Remove all files, except for two
bash;wildcards
You needshopt -s extglobinside the script.Your bash configuration presumably turns this on automatically for interactive shells (perhaps because you load the bash-completion package for context-sensitive completion); you need to turn it on manually inside scripts.Also make sure that your script starts with #!/bin/bash (or a variant like #!/usr/bin/env bash, otherwise it may be executed by a different shell that doesn't support this syntax.For example, this script will create a few files and then remove all except the two you want to keep:$ cat x#!/bin/bashshopt -s extglobtouch f1 f2 f3 exec.js .git foo barecho Before: $(ls -A)rm !(exec.js|.git)echo After: $(ls -A)In execution:$ bash xBefore: .git bar exec.js f1 f2 f3 foo xrm: cannot remove '.': Is a directoryrm: cannot remove '..': Is a directoryAfter: .git exec.jsNote that you get errors with . and ..; you'll have seen those on the command line as well:$ touch f1 f2 f3 exec.js .git foo bar$ rm !(exec.js|.git)rm: cannot remove '.': Is a directoryrm: cannot remove '..': Is a directory