@@ -10819,100 +10819,219 @@ <h3>Examples:</h3>
1081910819<h3>Syntax:</h3>
1082010820<pre><code class="language- hljs " data-lang="">chown [-OPTION] [DIRECTORY_PATH]
1082110821</code></pre>
10822- <div style="page-break-after: always;"></div><h1>The <code>find</code> command</h1>
10823- <p>The <code>find</code> command lets you <strong>search for files in a directory hierarchy</strong></p>
10824- <ul>
10825- <li>Search a file with specific name.</li>
10826- <li>Search a file with pattern</li>
10827- <li>Search for empty files and directories.</li>
10828- </ul>
10829- <h3>Examples:</h3>
10830- <ol>
10831- <li>Search a file with specific name:</li>
10832- </ol>
10833- <pre><code class="language-[linux]">find ./directory1 -name sample.txt
10822+ <div style="page-break-after: always;"></div><h1>The <code>find</code> Command</h1>
10823+ <p>The <code>find</code> command is one of the most powerful Linux utilities that lets you search for files and directories based on various conditions like name, size, modification time, permissions, and more.</p>
10824+ <h2>Basic Syntax</h2>
10825+ <pre><code class="language- hljs " data-lang="">find [path] [options] [expression]
1083410826</code></pre>
10835- <ol start="2">
10836- <li>Search a file with pattern:</li>
10837- </ol>
10838- <pre><code class="language-[linux]">find ./directory1 -name '*.txt'
10827+ <p><strong>In simple words:</strong></p>
10828+ <blockquote>
10829+ <p>“Search starting from <code>[path]</code>, apply <code>[options or filters]</code>, and then perform an <code>[action]</code> on the result.”</p>
10830+ </blockquote>
10831+ <p>Example:</p>
10832+ <pre><code class="language-php hljs php" data-lang="php">find /home/user -name <span class="hljs-string">"*.log"</span>
1083910833</code></pre>
10840- <ol start="3" >
10841- <li>To find all directories whose name is test in / directory.</li >
10842- </ol >
10843- <pre><code class="language-[linux]" >find / -type d - name test
10834+ <p>This searches for all <code>.log</code> files under <code>/home/user</code>.</p >
10835+ <h2>Common Use Cases</h2 >
10836+ <h3>1. Search a File by Exact Name</h3 >
10837+ <pre><code class="language- hljs " data-lang="" >find ./directory1 - name sample.txt
1084410838</code></pre>
10845- <ol start="4">
10846- <li>Searching empty files in current directory</li>
10847- </ol>
10848- <pre><code class="language-[linux]">find . -size 0k
10839+ <p>Finds <code>sample.txt</code> inside <code>directory1</code> and all its subdirectories.</p>
10840+ <p>Case-insensitive search:</p>
10841+ <pre><code class="language-php hljs php" data-lang="php">find ./directory1 -iname <span class="hljs-string">"sample.txt"</span>
1084910842</code></pre>
10850- <h3>Syntax:</h3>
10851- <pre><code class="language-[linux]">find [options] [paths] [expression]
10843+ <h3>2. Search Files by Pattern (Wildcard)</h3>
10844+ <p>Find all <code>.txt</code> files under <code>directory1</code>.</p>
10845+ <pre><code class="language-php hljs php" data-lang="php">find ./directory1 -name <span class="hljs-string">"*.txt"</span>
1085210846</code></pre>
10853- <p><strong>In Simple words</strong> </p>
10854- <pre><code class="language-[linux]" >find [where to start searching from]
10855- [expression determines what to find] [-options] [what to find]
10847+ <p>More examples: </p>
10848+ <pre><code class="language-php hljs php" data-lang="php" >find /<span class="hljs-keyword">var</span>/log -name <span class="hljs-string">"*.log"</span>
10849+ find /etc -name <span class="hljs-string">"conf*"</span>
1085610850</code></pre>
10857- <h3>Additional Flags and their Functionalities:</h3>
10858- <p>Commonly-used primaries include:</p>
10859- <ul>
10860- <li><code>name</code> pattern - tests whether the file name matches the shell-glob pattern given.</li>
10861- <li><code>type</code> type - tests whether the file is a given type. Unix file types accepted include:</li>
10862- </ul>
10851+ <h3>3. Find Directories by Name</h3>
10852+ <pre><code class="language-bash hljs bash" data-lang="bash">find / -<span class="hljs-built_in">type</span> d -name <span class="hljs-built_in">test</span>
10853+ </code></pre>
10854+ <p>Lists all directories named <code>test</code> from the root <code>/</code>.</p>
10855+ <h3>4. Find Empty Files and Directories</h3>
10856+ <pre><code class="language-php hljs php" data-lang="php">find . -<span class="hljs-keyword">empty</span>
10857+ </code></pre>
10858+ <p>Finds both empty files and directories in the current folder.</p>
10859+ <p>Only empty files:</p>
10860+ <pre><code class="language-php hljs php" data-lang="php">find . -type f -<span class="hljs-keyword">empty</span>
10861+ </code></pre>
10862+ <h3>5. Find Files by Modification or Access Time</h3>
1086310863<table>
1086410864<thead>
1086510865<tr>
10866- <th align="left"><strong>options</strong> </th>
10867- <th align="left"><strong> Description</strong> </th>
10866+ <th>Option </th>
10867+ <th> Description</th>
1086810868</tr>
1086910869</thead>
1087010870<tbody>
1087110871<tr>
10872- <td align="left" ><code>b </code></td>
10873- <td align="left">block device (buffered) </td>
10872+ <td><code>-mtime n </code></td>
10873+ <td>Modified <em>n</em> days ago </td>
1087410874</tr>
1087510875<tr>
10876- <td align="left" ><code>d </code></td>
10877- <td align="left">directory </td>
10876+ <td><code>-atime n </code></td>
10877+ <td>Accessed <em>n</em> days ago </td>
1087810878</tr>
1087910879<tr>
10880- <td align="left" ><code>f </code></td>
10881- <td align="left">regular file </td>
10880+ <td><code>-ctime n </code></td>
10881+ <td>Changed <em>n</em> days ago </td>
1088210882</tr>
1088310883<tr>
10884- <td align="left" ><code>l </code></td>
10885- <td align="left">Symbolic link </td>
10884+ <td><code>-mmin n </code></td>
10885+ <td>Modified <em>n</em> minutes ago </td>
1088610886</tr>
10887+ </tbody>
10888+ </table>
10889+ <p>Examples:</p>
10890+ <p>Find files modified within the last 2 days.</p>
10891+ <pre><code class="language-php hljs php" data-lang="php">find /<span class="hljs-keyword">var</span>/log -mtime <span class="hljs-number">-2</span>
10892+ </code></pre>
10893+ <p>Find files modified within the last hour.</p>
10894+ <pre><code class="language- hljs " data-lang="">find . -mmin -60
10895+ </code></pre>
10896+ <h3>6. Find Files by Size</h3>
10897+ <table>
10898+ <thead>
1088710899<tr>
10888- <td align="left"><code>-print</code></td >
10889- <td align="left">always returns true; prints the name of the current file plus a newline to the stdout.</td >
10900+ <th>Expression</th >
10901+ <th>Meaning</th >
1089010902</tr>
10903+ </thead>
10904+ <tbody>
1089110905<tr>
10892- <td align="left" ><code>-mtime n</code></td>
10893- <td align="left">find's all the files which are modified n days back. </td>
10906+ <td><code>+ n</code></td>
10907+ <td>Greater than n </td>
1089410908</tr>
1089510909<tr>
10896- <td align="left" ><code>-atime n</code></td>
10897- <td align="left">find's all the files which are accessed 50 days back. </td>
10910+ <td><code>-n</code></td>
10911+ <td>Less than n </td>
1089810912</tr>
1089910913<tr>
10900- <td align="left" ><code>-cmin n</code></td>
10901- <td align="left">find's all the files which are modified in the last 1 hour. </td>
10914+ <td><code>n</code></td>
10915+ <td>Exactly n </td>
1090210916</tr>
10917+ </tbody>
10918+ </table>
10919+ <p>Examples:</p>
10920+ <p>Find files larger than 100 MB.</p>
10921+ <pre><code class="language- hljs " data-lang="">find / -size +100M
10922+ </code></pre>
10923+ <p>Find files smaller than 10 KB.</p>
10924+ <pre><code class="language- hljs " data-lang="">find . -size -10k
10925+ </code></pre>
10926+ <h3>7. Find Files Modified More Recently than Another File</h3>
10927+ <pre><code class="language- hljs " data-lang="">find . -newer reference.txt
10928+ </code></pre>
10929+ <p>Lists files modified after <code>reference.txt</code>.</p>
10930+ <h3>8. Find Files by Type</h3>
10931+ <table>
10932+ <thead>
1090310933<tr>
10904- <td align="left"><code> -newer file</code></td>
10905- <td align="left">find's file was modified more recently than file.</td>
10934+ <th>Type</th>
10935+ <th>Description</th>
10936+ </tr>
10937+ </thead>
10938+ <tbody>
10939+ <tr>
10940+ <td><code>f</code></td>
10941+ <td>Regular file</td>
10942+ </tr>
10943+ <tr>
10944+ <td><code>d</code></td>
10945+ <td>Directory</td>
1090610946</tr>
1090710947<tr>
10908- <td align="left"><code>-size n</code></td>
10909- <td align="left">File uses n units of space, rounding up.</td>
10948+ <td><code>l</code></td>
10949+ <td>Symbolic link</td>
10950+ </tr>
10951+ <tr>
10952+ <td><code>b</code></td>
10953+ <td>Block device</td>
10954+ </tr>
10955+ <tr>
10956+ <td><code>c</code></td>
10957+ <td>Character device</td>
1091010958</tr>
1091110959</tbody>
1091210960</table>
10913- <h3>Help Command</h3>
10914- <p>Run below command to view the complete guide to <code>find</code> command or <a href="https://en.wikipedia.org/wiki/Find_(Unix)">click here</a>.</p>
10915- <pre><code class="language-[linux]">man find
10961+ <p>Example:</p>
10962+ <p>Find all block devices.</p>
10963+ <pre><code class="language-bash hljs bash" data-lang="bash">find /dev -<span class="hljs-built_in">type</span> b
10964+ </code></pre>
10965+ <h3>9. Find Files by Permission</h3>
10966+ <p>Find files with exactly 644 permissions.</p>
10967+ <pre><code class="language-php hljs php" data-lang="php">find /<span class="hljs-keyword">var</span>/www -type f -perm <span class="hljs-number">644</span>
10968+ </code></pre>
10969+ <p>Find files executable by anyone.</p>
10970+ <pre><code class="language-bash hljs bash" data-lang="bash">find /usr -<span class="hljs-built_in">type</span> f -perm /111
10971+ </code></pre>
10972+ <h3>10. Execute Commands on Found Files</h3>
10973+ <p>You can use the <code>-exec</code> option to run a command on each file found:</p>
10974+ <pre><code class="language-bash hljs bash" data-lang="bash">find . -<span class="hljs-built_in">type</span> f -name <span class="hljs-string">"*.log"</span> -<span class="hljs-built_in">exec</span> rm -f {} \;
10975+ </code></pre>
10976+ <p>Deletes all <code>.log</code> files under the current directory.</p>
10977+ <p><strong>Alternative (faster):</strong></p>
10978+ <pre><code class="language-bash hljs bash" data-lang="bash">find . -<span class="hljs-built_in">type</span> f -name <span class="hljs-string">"*.log"</span> | xargs rm -f
10979+ </code></pre>
10980+ <h3>11. Combine Multiple Conditions</h3>
10981+ <p>You can combine filters together:</p>
10982+ <pre><code class="language-php hljs php" data-lang="php">find /<span class="hljs-keyword">var</span>/log -name <span class="hljs-string">"*.log"</span> -size +<span class="hljs-number">50</span>M -mtime <span class="hljs-number">-2</span>
10983+ </code></pre>
10984+ <p>Finds <code>.log</code> files larger than 50 MB and modified within the last 2 days.</p>
10985+ <h3>12. Print Only File Names (Quiet Output)</h3>
10986+ <pre><code class="language-bash hljs bash" data-lang="bash">find /etc -<span class="hljs-built_in">type</span> f -name <span class="hljs-string">"*.conf"</span> -<span class="hljs-built_in">print</span>
10987+ </code></pre>
10988+ <p>The <code>-print</code> flag ensures output is displayed (it’s the default in most systems).</p>
10989+ <h2>Quick Reference Table</h2>
10990+ <table>
10991+ <thead>
10992+ <tr>
10993+ <th>Task</th>
10994+ <th>Command</th>
10995+ </tr>
10996+ </thead>
10997+ <tbody>
10998+ <tr>
10999+ <td>Find files with specific name</td>
11000+ <td><code>find . -name filename.txt</code></td>
11001+ </tr>
11002+ <tr>
11003+ <td>Case-insensitive search</td>
11004+ <td><code>find . -iname filename.txt</code></td>
11005+ </tr>
11006+ <tr>
11007+ <td>Find directories only</td>
11008+ <td><code>find . -type d</code></td>
11009+ </tr>
11010+ <tr>
11011+ <td>Find empty files</td>
11012+ <td><code>find . -type f -empty</code></td>
11013+ </tr>
11014+ <tr>
11015+ <td>Find files > 1 GB</td>
11016+ <td><code>find . -size +1G</code></td>
11017+ </tr>
11018+ <tr>
11019+ <td>Find modified in last day</td>
11020+ <td><code>find . -mtime -1</code></td>
11021+ </tr>
11022+ <tr>
11023+ <td>Delete <code>.tmp</code> files</td>
11024+ <td><code>find . -name "*.tmp" -delete</code></td>
11025+ </tr>
11026+ <tr>
11027+ <td>Search and execute</td>
11028+ <td><code>find . -name "*.log" -exec gzip {} \;</code></td>
11029+ </tr>
11030+ </tbody>
11031+ </table>
11032+ <h2>Getting Help</h2>
11033+ <p>To view the complete guide for the <code>find</code> command, run:</p>
11034+ <pre><code class="language- hljs " data-lang="">man find
1091611035</code></pre>
1091711036<div style="page-break-after: always;"></div><h1>The <code>rmdir</code> command</h1>
1091811037<p>The <strong>rmdir</strong> command is used to remove empty directories from the filesystem in Linux. The rmdir command removes each and every directory specified in the command line only if these directories are empty.</p>
0 commit comments