.. grep (+find, tar)

intro

introduction à "grep" (unix)
recherche d'un pattern

voir aussi egrep, fgrep ...

easy

# grep ABC *.htm

# grep -i ABC *.htm
        ignore maj/min

# grep -v ABC *.htm
        ne contient pas

# grep -c ABC *.htm
        count

# grep -l ABC *.htm
        nom du fic


more

recherche de fic contenant le pattern bibi
# find . -type f -print | xargs grep bibi



-- ne contient pas "nav" --
be4@indy3:/be4.free.fr/ski$ grep -L "nav>" *.htm 
paradis2013_map.htm paradis2013v2.htm ...
ou
be4@indy3:/be4.free.fr/ski$ grep -rvlH "nav>" *.htm
paradis2013_map.htm paradis2013v2.htm ...
-- contient "nav" -- be4@indy3:/be4.free.fr/ski$ grep -rlH "nav>" *.htm
argentiere2013.htm
-- verif -- be4@indy3:/be4.free.fr/ski$ grep nav *.htm
argentiere2013.htm:nav> argentiere2013.htm:/nav>
-- ne contient ni "nav" ni "map" -- be4@indy3:/be4.free.fr/ski$ grep -Ev "nav|map" *.htm
index.htm

wiki


  find+exec (lent) VS find+xargs (rapide)

be4@indy3:
$ cd /mnt/d/data_html/internet/be4.free.fr
$ time find . -name \*.htm -type f -print0 | xargs -0 grep -Hn 'wiki' |grep class
                                   ======= nom du fichier sur la même ligne     
                                             ====== plus rapide que -exec  

./alsace/index.htm:.wk { background-color: #333333; color: white; font-size: 60%; padding-left:6px; padding-right:6px } ./alsace/index.htm:<a href="https://fr.wikipedia.org/wiki/Alsace">Alsace</a> <span class="wk">wiki</span><br> ./doc/continent/index.htm:<a name="Sardaigne"></a> Sardaigne <a href="http://fr.wikipedia.org/wiki/Sardaigne"><span class="wiki"></span></a>, ./rando/20190915/index.htm:101:info 2019: pas d'article <span class="mywiki">wikipedia</span> pour Gaschney ... ./rando/nanga.htm:42:suiv... du <a href="https://fr.wikipedia.org/wiki/Nanga_Parbat">Nanga Parbat <span class="mywiki"> W </span></a> ./vtt2020/markstein_neige.htm:44:<a class="wikimap" href="https://fr.wikipedia.org/wiki/Markstein#/maplink/0"> 47°56′52″N, 7°01′38″ E</a> ./wiki.htm:44:<h2 class="ti">wiki</h2> ./wiki.htm:110:<b class="foot">be4.free.fr/wiki.htm</b> real 0m0.459s user 0m0.031s sys 0m0.406s
be4@indy3: $ time find . -name "*htm" -exec grep -w wiki {} \;
<a href="#wiki">wiki</a>, <a name="wiki"></a> <h2 class="ti">wiki</h2> <del>be4.free.fr/wikini/wakka.php?wiki= <b class="foot">be4.free.fr/wiki.htm</b> Binary file ./wiki.htm matches real 0m12.799s user 0m0.797s sys 0m9.344s
be4@indy3: find . -name index.htm |xargs grep wiki
./voyage/c2019/index.htm:<a class="wiki" href="https://fr.wikipedia.org/wiki/Khmer">khmer</a> <br> ./voyage/c2019/index.htm:quelques articles wikipedia<br> ./voyage/c2019/index.htm:<a class="wiki" href="https://fr.wikipedia.org/wiki/Pol_Pot">Pol Pot</a> 1925-1998<br> ./voyage/c2019/index.htm:<a class="wiki" href="https://fr.wikipedia.org/wiki/Norodom_Sihanouk">Norodom Sihanouk</a><br>

find

2 3 exemples évolués

compter
-------
be4@indy3: find . -name index.htm |xargs grep wiki|wc -l
478



fichiers modifiés ces 90 derniers jours (sauf répertoire)
---------------------------------------
be4@indy3:/mnt/d/data_html$ find . -path ./internet -prune -o -name "*.htm" -mtime -90 -print |wc -l ========================== = sauf dans le rep. internet 114 be4@indy3:/mnt/d/data_html$ cdbe4 be4@indy3:/mnt/d/data_html/internet/be4.free.fr$ find . -mtime -90 -print |wc -l 1088 be4@indy3:/mnt/d/data_html/internet/be4.free.fr$ cd ../.. be4@indy3:/mnt/d/data_html$ find . -mtime -90 -print |wc 1388 be4@indy3:/mnt/d/data_html$ find . -path ./internet -prune -o -mtime -90 -print |wc -l 296

tar

be4@indy3:/mnt/d/data_html$ history 2004 find . -mtime -27 |wc 2005 history |grep find be4@indy3:/mnt/d/data_html$ find . -type f -mtime -30 -exec tar -rvf fic_add30j_til20230220.tar {} \; ./divers/histoire/index.htm ./genea/cartes/milhusina1920.jpg .... be4@indy3:/mnt/d/data_html$ ll -rwxrwxrwx 1 be4 be4 152524800 Feb 20 06:31 fic_add30j_til20230220.tar

link

dos.htm = DOS vs Unix commands
en.wikipedia.org/wiki/grep
en.wikipedia.org/wiki/Find_(Unix)
les articles francais sont trop nuls :-(