0

I am trying to write a script that displays files which do not have a .py extension Here is a small snippet of the code i am writing.

   echo " - Creating"
   cd ${dirs[$directory]}
   file=$(ls -pd !(*.py) | grep -v /)
   echo $file

But I keep getting -

command substitution: line 54: syntax error near unexpected token `('
command substitution: line 54: `ls -pd !(*.py) | grep -v /)'
6
  • shopt -s extglob is mandatory. Oct 10, 2023 at 22:32
  • What would be the right way. There are only 2 files in the folder one is a .py and one has a random extension Oct 10, 2023 at 22:35
  • shopt actually made it work :) thank you Oct 10, 2023 at 22:37
  • As an alternative, with bash you can use cd /the/directory; GLOBIGNORE='*.py'; echo * to get a list of names in the directory with the ones ending in .py omitted. See the bash man page description for the GLOBIGNORE environment variable.
    – Sotto Voce
    Oct 11, 2023 at 2:02
  • Should be set -o extendedglob; files=( $dirs[$directory]/^*.py(N.:t) ) and in zsh instead of bash. The output of ls is not post-processable reliably. Oct 11, 2023 at 5:38

0

Browse other questions tagged .