Home > Software design >  Why am i getting syntax error near unexpected token `('
Why am i getting syntax error near unexpected token `('

Time:12-15

I am trying to implement a linux command using a shell script to delete some files & directories from a directory ignoring "output.mp4" & "_Removed.avi" but getting the following Error :

./test.sh: line 11: syntax error near unexpected token `('
./test.sh: line 11: `sudo rm -rf !("output.mp4"|"_Removed.avi")'

Script That i am implementing is :

#!/bin/bash

if [ -d /home/Videos_bkp/ ]
then

cd /home/subham/Videos_bkp

sudo rm -rf !("output.mp4"|"_Removed.avi")

else

echo "Directory Not Available"

fi

#Thanks In Advance.

CodePudding user response:

You can try this type of script. I created this to delete file and folders exculding some folders thats why I used [-maxdepth 1] followed by folder names but you can use file name as well.

#!/bin/bash

if [[ -d  /home/Videos_bkp/ ]];
then

cd /home/subham/Videos_bkp

find . -maxdepth 1  ! -name "output.mp4" ! -name "_Removed.avi" ! -name . -exec rm -r {} \;

else

echo "Directory Not Available"

fi```

CodePudding user response:

The subshell needs to open extglob. Add the following content to the header of the file.

shopt -s extglob
...
  • Related