I am writing a shell script and want to delete everything from a folder with exception of one file.
This code does exactly what I am looking for:
rm -r -v subfolder/!("file")
First, from the shell script, I can not use directly the (), I found by searching that I should change it to: /(file/)
So in my script I wrote:
rm -r -v subfolder/!/("file"/)
But, when I run the script it keeps giving me the following error:
rm: cannot remove 'subfolder/!(file)': No such file or directory
I am running the script from the same folder as when I do the first bash command (the "subfolder" is a present folder in the current location of the bash and also the script is placed there in the same current location).
Complete script name of the file "Allclean" is:
#!/bin/sh
cd ${0%/*} || exit 1 # Run from this directory
# Source tutorial clean functions
. $WM_PROJECT_DIR/bin/tools/CleanFunctions
shopt -s extglob
echo "\n
Cleaning case (Transilient Scalar Transport Equation)
------------------------------------------------------------------------------------------------------------------------
"
echo " - Deleting 0 folder"
rm -f -r 0
echo " - Removing constant folder content except transportProperties"
cd constant
rm -r -v constant/!("transportProperties")
When I run rm -r -v constant/!("transportProperties")
from the bash works properly. When I run ./Allclean
I get the following error:
./Allclean: 6: ./Allclean: shopt: not found
Cleaning case (Transilient Scalar Transport Equation)
------------------------------------------------------------------------------------------------------------------------
- Deleting 0 folder
- Removing constant folder content except transportProperties
./Allclean: 16: ./Allclean: Syntax error: "(" unexpected
shopt -s extglob
)extglob
inside a/bin/sh
script.sh
(akadash
) does not support this feature. Change the shebang to#!/bin/bash