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
  • 84,225
  • 14
  • 89
  • 153
Vaibhav Nigam
  • 1,334
  • 12
  • 21
  • 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" \) 
Javad Sameri
  • 1,218
  • 3
  • 17
  • 30
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
  • 41,620
  • 7
  • 58
  • 90
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
  • 5,062
  • 4
  • 35
  • 47