title: Bash delete a file excluding under a directory or subdirectory containing certain name pattern
body: <p>The file structure:</p>
  <pre><code>|-- ./a
  |   |-- ./a/b
  |   |   `-- ./a/b/file.json
  |   `-- ./a/file.json
  |-- ./file.json
  |-- ./folder
  |   `-- ./folder/file.json
  |-- ./guides
  |   `-- ./guides/file.json
  |-- ./network
  |   |-- ./network/file.json
  |   `-- ./network/guides
  |       `-- ./network/guides/file.json
  `-- ./script.sh
  
  6 directories, 8 files
  </code></pre>
  <p>When running the script, it should delete <code>file.json</code> file from every directory excluding the one with a &quot;guides&quot; name.</p>
  <p>So the output should give:</p>
  <pre><code>|-- ./a
  |   `-- ./a/b
  |-- ./file.json
  |-- ./folder
  |-- ./guides
  |   `-- ./guides/file.json
  |-- ./network
  |   `-- ./network/guides
  |       `-- ./network/guides/file.json
  `-- ./script.sh
  
  6 directories, 4 files
  </code></pre>
  <p>My attempt to write code:</p>
  <pre class="lang-sh prettyprint-override"><code>for directory in $(find . -maxdepth 40 -type d)
  do
      if [[ ${directory##*/} != &quot;guides&quot; &amp;&amp; ${directory##*/} != &quot;.&quot; ]]
      then
          find ${directory##*/} -name &quot;*file.json&quot; -type f -delete
      fi
  done
  </code></pre>
  <p>INCORRECT output of my code:</p>
  <pre><code>|-- ./a
  |   `-- ./a/b
  |-- ./file.json
  |-- ./folder
  |-- ./guides
  |   `-- ./guides/file.json
  |-- ./network
  |   `-- ./network/guides
  `-- ./script.sh
  
  6 directories, 3 files
  </code></pre>
  <p>However this code does not work as expected.</p>
  <p>What's the fix?</p>
  <p>EDIT:</p>
  <p><code>./guides/network/file.json</code> file.json in this path should also be deleted.</p>