https://wiki.hevs.ch/uit/index.php5?title=Languages/shell/find&feed=atom&action=historyLanguages/shell/find - Revision history2024-03-28T19:29:42ZRevision history for this page on the wikiMediaWiki 1.18.1https://wiki.hevs.ch/uit/index.php5?title=Languages/shell/find&diff=6200&oldid=prevMarc.pignat at 10:11, 17 February 20172017-02-17T10:11:55Z<p></p>
<table class='diff diff-contentalign-left'>
<col class='diff-marker' />
<col class='diff-content' />
<col class='diff-marker' />
<col class='diff-content' />
<tr valign='top'>
<td colspan='2' style="background-color: white; color:black;">← Older revision</td>
<td colspan='2' style="background-color: white; color:black;">Revision as of 10:11, 17 February 2017</td>
</tr><tr><td colspan="2" class="diff-lineno">Line 46:</td>
<td colspan="2" class="diff-lineno">Line 46:</td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>== Removing empty directories ==</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>== Removing empty directories ==</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>find . -type d -empty -print -delete</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>find . -type d -empty -print -delete</div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins style="color: red; font-weight: bold; text-decoration: none;"></ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins style="color: red; font-weight: bold; text-decoration: none;">= Traps =</ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins style="color: red; font-weight: bold; text-decoration: none;">== find: missing argument to `-exec' ==</ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins style="color: red; font-weight: bold; text-decoration: none;">* The <code>-exec</code> command must be terminated by " \;", so the "\;" is missing or the space.</ins></div></td></tr>
</table>Marc.pignathttps://wiki.hevs.ch/uit/index.php5?title=Languages/shell/find&diff=6199&oldid=prevMarc.pignat: /* Finding files containing a string */2017-02-17T10:07:24Z<p><span class="autocomment">Finding files containing a string</span></p>
<table class='diff diff-contentalign-left'>
<col class='diff-marker' />
<col class='diff-content' />
<col class='diff-marker' />
<col class='diff-content' />
<tr valign='top'>
<td colspan='2' style="background-color: white; color:black;">← Older revision</td>
<td colspan='2' style="background-color: white; color:black;">Revision as of 10:07, 17 February 2017</td>
</tr><tr><td colspan="2" class="diff-lineno">Line 31:</td>
<td colspan="2" class="diff-lineno">Line 31:</td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>* The <code>-exec</code> command will execute grep on every file found by file, and evaluates it's return value.</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>* The <code>-exec</code> command will execute grep on every file found by file, and evaluates it's return value.</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>* The <code>-print</code> command is necessary to display the file that match the <code>-exec</code> command.</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>* The <code>-print</code> command is necessary to display the file that match the <code>-exec</code> command.</div></td></tr>
<tr><td class='diff-marker'>−</td><td style="background: #ffa; color:black; font-size: smaller;"><div>* The <code>{}</code> is the filename given by find to grep</div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div>* The <code>{}</code> is the filename given by find to grep<ins class="diffchange diffchange-inline">.</ins></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>* The <code>\;</code> is the end of the <code>-exec</code> command.</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>* The <code>\;</code> is the end of the <code>-exec</code> command.</div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins style="color: red; font-weight: bold; text-decoration: none;">* '''Warning''' there is always a space before <code>\;</code></ins></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>* '''Warning''' grep uses [http://en.wikipedia.org/wiki/Regular_expression regular expressions] for matching, so the dot (.) must be escaped</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>* '''Warning''' grep uses [http://en.wikipedia.org/wiki/Regular_expression regular expressions] for matching, so the dot (.) must be escaped</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td></tr>
</table>Marc.pignathttps://wiki.hevs.ch/uit/index.php5?title=Languages/shell/find&diff=6198&oldid=prevAlain.woeffray: /* Finding files containing a string */2017-02-17T10:03:36Z<p><span class="autocomment">Finding files containing a string</span></p>
<table class='diff diff-contentalign-left'>
<col class='diff-marker' />
<col class='diff-content' />
<col class='diff-marker' />
<col class='diff-content' />
<tr valign='top'>
<td colspan='2' style="background-color: white; color:black;">← Older revision</td>
<td colspan='2' style="background-color: white; color:black;">Revision as of 10:03, 17 February 2017</td>
</tr><tr><td colspan="2" class="diff-lineno">Line 32:</td>
<td colspan="2" class="diff-lineno">Line 32:</td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>* The <code>-print</code> command is necessary to display the file that match the <code>-exec</code> command.</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>* The <code>-print</code> command is necessary to display the file that match the <code>-exec</code> command.</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>* The <code>{}</code> is the filename given by find to grep</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>* The <code>{}</code> is the filename given by find to grep</div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins style="color: red; font-weight: bold; text-decoration: none;">* The <code>\;</code> is the end of the <code>-exec</code> command.</ins></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>* '''Warning''' grep uses [http://en.wikipedia.org/wiki/Regular_expression regular expressions] for matching, so the dot (.) must be escaped</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>* '''Warning''' grep uses [http://en.wikipedia.org/wiki/Regular_expression regular expressions] for matching, so the dot (.) must be escaped</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td></tr>
</table>Alain.woeffrayhttps://wiki.hevs.ch/uit/index.php5?title=Languages/shell/find&diff=5638&oldid=prevMarc.pignat: /* Removing empty directories */2016-06-09T08:15:58Z<p><span class="autocomment">Removing empty directories</span></p>
<table class='diff diff-contentalign-left'>
<col class='diff-marker' />
<col class='diff-content' />
<col class='diff-marker' />
<col class='diff-content' />
<tr valign='top'>
<td colspan='2' style="background-color: white; color:black;">← Older revision</td>
<td colspan='2' style="background-color: white; color:black;">Revision as of 08:15, 9 June 2016</td>
</tr><tr><td colspan="2" class="diff-lineno">Line 43:</td>
<td colspan="2" class="diff-lineno">Line 43:</td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>== Removing empty directories ==</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>== Removing empty directories ==</div></td></tr>
<tr><td class='diff-marker'>−</td><td style="background: #ffa; color:black; font-size: smaller;"><div>find . -type d -empty -<del class="diffchange diffchange-inline">exec rmdir {} \;</del></div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div>find . -type d -empty -<ins class="diffchange diffchange-inline">print -delete</ins></div></td></tr>
</table>Marc.pignathttps://wiki.hevs.ch/uit/index.php5?title=Languages/shell/find&diff=4383&oldid=prevMarc.pignat: /* Finding files containing a string */2015-02-03T15:42:06Z<p><span class="autocomment">Finding files containing a string</span></p>
<table class='diff diff-contentalign-left'>
<col class='diff-marker' />
<col class='diff-content' />
<col class='diff-marker' />
<col class='diff-content' />
<tr valign='top'>
<td colspan='2' style="background-color: white; color:black;">← Older revision</td>
<td colspan='2' style="background-color: white; color:black;">Revision as of 15:42, 3 February 2015</td>
</tr><tr><td colspan="2" class="diff-lineno">Line 33:</td>
<td colspan="2" class="diff-lineno">Line 33:</td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>* The <code>{}</code> is the filename given by find to grep</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>* The <code>{}</code> is the filename given by find to grep</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>* '''Warning''' grep uses [http://en.wikipedia.org/wiki/Regular_expression regular expressions] for matching, so the dot (.) must be escaped</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>* '''Warning''' grep uses [http://en.wikipedia.org/wiki/Regular_expression regular expressions] for matching, so the dot (.) must be escaped</div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins style="color: red; font-weight: bold; text-decoration: none;"></ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins style="color: red; font-weight: bold; text-decoration: none;">== Editing files containing a string ==</ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins style="color: red; font-weight: bold; text-decoration: none;"><code>EDITOR=vi find . -type f -exec grep -q "heivs/error\.h" {} \; -exec $EDITOR {} \;</code><br /></ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins style="color: red; font-weight: bold; text-decoration: none;">* '''Warning''' Set <code>$EDITOR</code> to your preferred editor</ins></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>== Finding files with any of 2 suffixes ==</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>== Finding files with any of 2 suffixes ==</div></td></tr>
</table>Marc.pignathttps://wiki.hevs.ch/uit/index.php5?title=Languages/shell/find&diff=4382&oldid=prevMarc.pignat: /* Finding files containing a string */2015-02-03T15:37:41Z<p><span class="autocomment">Finding files containing a string</span></p>
<table class='diff diff-contentalign-left'>
<col class='diff-marker' />
<col class='diff-content' />
<col class='diff-marker' />
<col class='diff-content' />
<tr valign='top'>
<td colspan='2' style="background-color: white; color:black;">← Older revision</td>
<td colspan='2' style="background-color: white; color:black;">Revision as of 15:37, 3 February 2015</td>
</tr><tr><td colspan="2" class="diff-lineno">Line 27:</td>
<td colspan="2" class="diff-lineno">Line 27:</td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>== Finding files containing a string ==</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>== Finding files containing a string ==</div></td></tr>
<tr><td class='diff-marker'>−</td><td style="background: #ffa; color:black; font-size: smaller;"><div><code>find . -type f -exec grep -q "heivs/error.h" {} \; -print</code><br /></div></td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><code>find . -type f -exec grep -q "heivs/error<ins class="diffchange diffchange-inline">\</ins>.h" {} \; -print</code><br /></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>Will find all files containing heivs/error.h</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>Will find all files containing heivs/error.h</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>* The <code>-exec</code> command will execute grep on every file found by file, and evaluates it's return value.</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>* The <code>-exec</code> command will execute grep on every file found by file, and evaluates it's return value.</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>* The <code>-print</code> command is necessary to display the file that match the <code>-exec</code> command.</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>* The <code>-print</code> command is necessary to display the file that match the <code>-exec</code> command.</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>* The <code>{}</code> is the filename given by find to grep</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>* The <code>{}</code> is the filename given by find to grep</div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins style="color: red; font-weight: bold; text-decoration: none;">* '''Warning''' grep uses [http://en.wikipedia.org/wiki/Regular_expression regular expressions] for matching, so the dot (.) must be escaped</ins></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>== Finding files with any of 2 suffixes ==</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>== Finding files with any of 2 suffixes ==</div></td></tr>
</table>Marc.pignathttps://wiki.hevs.ch/uit/index.php5?title=Languages/shell/find&diff=4200&oldid=prevMarc.pignat at 17:02, 3 December 20142014-12-03T17:02:37Z<p></p>
<table class='diff diff-contentalign-left'>
<col class='diff-marker' />
<col class='diff-content' />
<col class='diff-marker' />
<col class='diff-content' />
<tr valign='top'>
<td colspan='2' style="background-color: white; color:black;">← Older revision</td>
<td colspan='2' style="background-color: white; color:black;">Revision as of 17:02, 3 December 2014</td>
</tr><tr><td colspan="2" class="diff-lineno">Line 36:</td>
<td colspan="2" class="diff-lineno">Line 36:</td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div><code>find . -type f -name "*.h" -o -name "*.c"</code><br /></div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div><code>find . -type f -name "*.h" -o -name "*.c"</code><br /></div></td></tr>
<tr><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>* The <code>-o</code> parameter means OR.</div></td><td class='diff-marker'> </td><td style="background: #eee; color:black; font-size: smaller;"><div>* The <code>-o</code> parameter means OR.</div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins style="color: red; font-weight: bold; text-decoration: none;"></ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins style="color: red; font-weight: bold; text-decoration: none;">== Removing empty directories ==</ins></div></td></tr>
<tr><td colspan="2"> </td><td class='diff-marker'>+</td><td style="background: #cfc; color:black; font-size: smaller;"><div><ins style="color: red; font-weight: bold; text-decoration: none;">find . -type d -empty -exec rmdir {} \;</ins></div></td></tr>
</table>Marc.pignathttps://wiki.hevs.ch/uit/index.php5?title=Languages/shell/find&diff=2761&oldid=prevPim: Created page with "{{public}} {{TOC right}} = The <code>find</code> command = This command can be used to find files (or directories) recursively. == Finding all files and directories starting ..."2013-10-10T07:35:24Z<p>Created page with "{{public}} {{TOC right}} = The <code>find</code> command = This command can be used to find files (or directories) recursively. == Finding all files and directories starting ..."</p>
<p><b>New page</b></p><div>{{public}}<br />
{{TOC right}}<br />
= The <code>find</code> command =<br />
This command can be used to find files (or directories) recursively.<br />
<br />
== Finding all files and directories starting from the current directory ==<br />
<code>find .</code><br /><br />
Will display all files and directories below the current directory.<br />
<br />
== Finding all files and directories in a directory ==<br />
<code>find /etc</code><br /><br />
Will display all files and directories below /etc.<br />
<br />
== Finding all files and directories in many directory ==<br />
<code>find /etc /home </code><br /><br />
Will display all files and directories below /etc and /home (in that order).<br />
<br />
== Finding only files in a directory ==<br />
<code>find /etc -type f </code><br /><br />
Will display all files (no directories) below /etc.<br />
<br />
== Finding files with a suffix ==<br />
<code>find /etc -type f -name "*.conf"</code><br />
Will find all files with name ending by .conf below /etc.<br /><br />
* Warning: Don't forget the double quotes (") around the file name<br />
* Warning: find is case sensitive, if you also want files with "*.Conf", use the <code>-iname</code><br />
<br />
== Finding files containing a string ==<br />
<code>find . -type f -exec grep -q "heivs/error.h" {} \; -print</code><br /><br />
Will find all files containing heivs/error.h<br />
* The <code>-exec</code> command will execute grep on every file found by file, and evaluates it's return value.<br />
* The <code>-print</code> command is necessary to display the file that match the <code>-exec</code> command.<br />
* The <code>{}</code> is the filename given by find to grep<br />
<br />
== Finding files with any of 2 suffixes ==<br />
<code>find . -type f -name "*.h" -o -name "*.c"</code><br /><br />
* The <code>-o</code> parameter means OR.</div>Pim