# \[find\]Exclure un répertoire d'une recherche

**URL:** <https://www.debian-fr.org/t/find-exclure-un-repertoire-dune-recherche/29090>\
**Category:** Programmation\
**Created:** [Février 20, 2016, 11:50 UTC](https://www.debian-fr.org/t/find-exclure-un-repertoire-dune-recherche/29090 "2016-02-20T23:50:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Glorf](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/g/d6d6ee/32.png) [@Glorf](https://www.debian-fr.org/u/Glorf)\
**Post date:** [Février 20, 2016, 11:50 UTC](https://www.debian-fr.org/t/find-exclure-un-repertoire-dune-recherche/29090/1 "2016-02-20T23:50:37Z")

</div>

Bonjour,  
j’ai un problème : j’arrive pas à exclure un répertoire avec find. J’ai trouvé [ça](http://www.xgarreau.org/forum5/read.php?1,24,3471,quote=1) en cherchant sur google, mais l’astuce a pas l’air de marcher, mon code :

`glorf@debian-desktop:~/test$ find -name "*.php"
./2/2.php
./3/3.php
./1.php
glorf@debian-desktop:~/test$ find -name "*.php" -path '2/' -prune -o -name *.php
./1.php
`

Là, il m’exclut tous les dossiers … Sachant que c’est pour traiter derrière (avec -exec), je peux pas faire un grep dessus.  
Cordialement,  
Glorf.

---

<div class="post-metadata">

**Author:** ![Totor](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/t/58f4c7/32.png) [@Totor](https://www.debian-fr.org/u/Totor)\
**Post date:** [Février 20, 2016, 11:51 UTC](https://www.debian-fr.org/t/find-exclure-un-repertoire-dune-recherche/29090/2 "2016-02-20T23:51:08Z")

</div>

Bonjour,

```auto
$ mkdir -p php/{hph,phph,aaaa}
$ touch php/{hph,phph,aaaa}/php{1,2,3}.php
$ printf "%s\n" */*/*
php/aaaa/php1.php
php/aaaa/php2.php
php/aaaa/php3.php
php/hph/php1.php
php/hph/php2.php
php/hph/php3.php
php/phph/php1.php
php/phph/php2.php
php/phph/php3.php

$ find . -path './*aaaa' -prune -o -name '*.php' -print
./php/hph/php3.php
./php/hph/php1.php
./php/hph/php2.php
./php/phph/php3.php
./php/phph/php1.php
./php/phph/php2.php
```

A titre d’info, si tu utilises bash et en particulier une version \>=4 :

```auto
$ shopt -s extglob globstar
$ mkdir -p php/hph/titi
$ touch php/hph/titi/php5.php
$ printf "%s\n" **/*.php
php/aaaa/php1.php
php/aaaa/php2.php
php/aaaa/php3.php
php/hph/php1.php
php/hph/php2.php
php/hph/php3.php
php/hph/titi/php5.php
php/phph/php1.php
php/phph/php2.php
php/phph/php3.php

$ printf "%s\n" **/!(titi)/*.php
php/aaaa/php1.php
php/aaaa/php2.php
php/aaaa/php3.php
php/hph/php1.php
php/hph/php2.php
php/hph/php3.php
php/phph/php1.php
php/phph/php2.php
php/phph/php3.php
```

---

<div class="post-metadata">

**Author:** ![Glorf](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/g/d6d6ee/32.png) [@Glorf](https://www.debian-fr.org/u/Glorf)\
**Post date:** [Février 20, 2016, 11:51 UTC](https://www.debian-fr.org/t/find-exclure-un-repertoire-dune-recherche/29090/3 "2016-02-20T23:51:09Z")

</div>

Merci pour l’astuce, par contre je n’arrive pas à exclure plusieurs répertoires :

`find . -path './*{Images,yofrankie}' -prune -o -name "*.jpg"
`  
Me sors aussi ce qu’il y a dans Images et yofrankie 😕

---

<div class="post-metadata">

**Author:** ![Totor](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/t/58f4c7/32.png) [@Totor](https://www.debian-fr.org/u/Totor)\
**Post date:** [Février 20, 2016, 11:51 UTC](https://www.debian-fr.org/t/find-exclure-un-repertoire-dune-recherche/29090/4 "2016-02-20T23:51:11Z")

</div>

Les pattern de find ne sont pas identiques à ceux du shell :

```auto
$ find . -path './*Images' -prune -o -path './yofrankie' -prune -o -name "*.jpg" -print

# ou 

$ find . ! -regex '.*/\(Images\|yofrankie\)/.*' -name '*.jpg'
```

et il ne faut pas oublier le -print pour la première solution

---

<div class="post-metadata">

**Author:** ![Glorf](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/g/d6d6ee/32.png) [@Glorf](https://www.debian-fr.org/u/Glorf)\
**Post date:** [Février 20, 2016, 11:51 UTC](https://www.debian-fr.org/t/find-exclure-un-repertoire-dune-recherche/29090/5 "2016-02-20T23:51:11Z")

</div>

Merci pour la regex, je note ^^. Mais pourquoi il faut pas mettre le -print ? (ça marche sans chez moi)  
Je passe en résolu sinon

---

<div class="post-metadata">

**Author:** ![Totor](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/t/58f4c7/32.png) [@Totor](https://www.debian-fr.org/u/Totor)\
**Post date:** [Février 20, 2016, 11:51 UTC](https://www.debian-fr.org/t/find-exclure-un-repertoire-dune-recherche/29090/6 "2016-02-20T23:51:13Z")

</div>

[quote=«&nbsp;Glorf&nbsp;»]Mais pourquoi il faut pas mettre le -print ? (ça marche sans chez moi)  
[/quote]

> [@man find](#):
>
> If the expression contains no actions other than -prune, -print is performed on all files for which the expression is true.

Cela signifie que si tu ne mets pas -print, l’action par défaut est d’afficher tous les fichiers trouvés.  
Ce qui équivaut à ceci :

```auto
find . \( -path './*/Images' -prune -o -path './*/yofrankie' -prune -o -name "*.jpg" \) -print
```

or en mettant -print, tu l’associes uniquement à la dernière condition, c.a.d :

```auto
find . \( -path './*/Images' -prune -o -path './*/yofrankie' -prune \) -o \( -name "*.jpg" -print \)
```

Comme on précise une action, elle n’est réalisée qu’à la condition associée
