I have 5 files in a directory


Now I want to Move files having no extension i.e 1CC and 1DD to a another directory i.e tree.

whenever i am manually using command it will work.

command : 
shopt -s extglob
mv !(*.*) tree

but in a bash script it shows the error script :

mkdir tree
shopt -s extglob
mv !(*.*) tree/

and saved as stuct.sh after running it

it shows the following error:

struc.sh: line 11: syntax error near unexpected token `('
struc.sh: line 11: `mv !(*.*) tree/'

I have No idea why this is happening. Please provide solution for the same. thanks


This is because you're using special characters in your script. You'll have to escape them.

You can better use find for this:

find . -maxdepth 1 -type f ! -name "*.*" -exec mv -t ./tree/ {} \+

This command will:

  • find search
  • . in the current directory
  • -maxdepth 1 with a depth of 1 (so no subdirectories)
  • type -f only files
  • ! -name "*.*" where the filename does not match *.*
  • -exec mv -t ./tree/ {} \+ execute the mv command, the -t argument specifies the target directory (tree in the current directory (.)) and appends (\+) the filename ({}) to it.

This will work in your bash script and it does not try to move directories or your own tree directory to itself (which your mv command will try).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.