1
FILES=`find . -type f -name '*.js'`
for file in $FILES
do
  # some task
done

This will loop over all *.js files but let's say there are some .spec.js files as well which I want to skip.

a.js
a.spec.js
b.js
x.spec.js
c.js

should iterate over:

a.js
b.js
c.js
Cyrus
  • 77,979
  • 13
  • 71
  • 125
Vaibhav Nigam
  • 982
  • 9
  • 18
  • 1
    See: [Exclude specific filename from shell globbing](http://stackoverflow.com/q/2643929/3776858) and [What expands to all files in current directory recursively?](http://stackoverflow.com/q/1690809/3776858) – Cyrus Apr 14 '17 at 07:42

3 Answers3

4

this is the code you looking for :

find . -type f \( -iname "*.js" -not -iname "*.spec.js" \) 
MJ Sameri
  • 1,107
  • 2
  • 15
  • 28
1

Just add a condition that implements that you do not want a specific file name pattern:

FILES=`find . -type f -name '*.js'`
for file in $FILES
do
  if [[ $file != *.spec.js ]]; then
    echo $file
  fi
done
arkascha
  • 39,439
  • 7
  • 52
  • 86
0

You are looking for the negate ! feature of find to not match files with specific names. Additionally using wholename should be faster I think.

find . ! -name '*.spec*'

So your final command (not tested) would be:

find . -type f ! -name '*.js' -wholename '*.php'
mayersdesign
  • 4,657
  • 4
  • 31
  • 46