0

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
  • 14,015
  • 12
  • 79
  • 140

2 Answers2

2

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 Quenot
  • 154,891
  • 35
  • 213
  • 206
lurker
  • 55,215
  • 8
  • 64
  • 98
  • 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
  • 19,833
  • 10
  • 94
  • 150
  • 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