0

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
7
  • "i found by searching that i should change it" can you provide a link? it is more likely that all you needed to do was enable extended globbing inside your script (shopt -s extglob) May 20, 2020 at 14:03
  • ufff.. good question where i saw it..but it still does not work with the shopt -s extglob i get " ./Allclean: 16: ./Allclean: Syntax error: "(" unexpected" Allclean is the run script
    – franco
    May 20, 2020 at 14:25
  • Please edit your question to include at least the relevant portion of your script May 20, 2020 at 14:47
  • i just edited whit the extra information, from what you asked before i read it from here unix.stackexchange.com/questions/153862/…
    – franco
    May 20, 2020 at 14:57
  • 1
    It appears you are trying to use bash's extglob inside a /bin/sh script. sh (aka dash) does not support this feature. Change the shebang to #!/bin/bash May 20, 2020 at 15:25

1 Answer 1

0

use find with the delete option

yiheng@DESKTOP-LIJQMSN:/mnt/c/Users/ihen$ ls folder/
a  b  c  d  e
yiheng@DESKTOP-LIJQMSN:/mnt/c/Users/ihen$ find folder/ -mindepth 1 -not -path folder/d
folder/a
folder/b
folder/c
folder/e
yiheng@DESKTOP-LIJQMSN:/mnt/c/Users/ihen$ find folder/ -mindepth 1 -not -path folder/d -delete
yiheng@DESKTOP-LIJQMSN:/mnt/c/Users/ihen$ ls folder/
d
yiheng@DESKTOP-LIJQMSN:/mnt/c/Users/ihen$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .