Vaibhav Nigam
Vaibhav Nigam

Reputation: 1467

Loop over all *.js files in a directory skipping *.spec.js files in bash script

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

Upvotes: 2

Views: 1425

Answers (3)

arkascha
arkascha

Reputation: 42915

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

Upvotes: 1

Javad Sameri
Javad Sameri

Reputation: 1319

this is the code you looking for :

find . -type f \( -iname "*.js" -not -iname "*.spec.js" \) 

Upvotes: 4

mayersdesign
mayersdesign

Reputation: 5310

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'

Upvotes: 0

Related Questions