1

related to: Loop through all the files with a specific extension

I want to loop through files that matches a pattern:

for item in ./bob* ; do
    echo $item
done

I have a file list like:

bob
bobob
bobob.log

I only want to list files that have no extension:

bob
bobob

what is the best way to archive this? - can I do it in the loop somehow or do I need an if statement within the loop?

code_fodder
  • 15,263
  • 17
  • 90
  • 167

2 Answers2

3

In bash you can use features of xtended globbing:

shopt -s extglob

for item in ./bob!(*.*) ; do
    echo $item
done

You can put shopt -s extglob in your .bashrc file to enable it.

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
lurker
  • 56,987
  • 9
  • 69
  • 103
  • 1
    thanks works nicely - also the other answer does too, I accepted this one somewhat arbitrarily in that I prefer the syntax, but both are good – code_fodder Jun 08 '20 at 12:31
1

Recent Bash versions have regular expression support:

for f in *
do
  if [[ "$f" =~ .*\..*  ]]
  then
    : ignore
  else
    echo "$f"
  fi
done
ceving
  • 21,900
  • 13
  • 104
  • 178
  • thanks works nicely - also the other answer does too, I cant determine which the "accepted answer". So I am just accepting the answer that I used only because it has less (by about 1!?) and is slightly easier to read. – code_fodder Jun 08 '20 at 12:30