Sick Gaming
How to Search for Files from the Linux Command Line - Printable Version

+- Sick Gaming (https://www.sickgaming.net)
+-- Forum: Computers (https://www.sickgaming.net/forum-86.html)
+--- Forum: Linux, FreeBSD, and Unix types (https://www.sickgaming.net/forum-88.html)
+--- Thread: How to Search for Files from the Linux Command Line (/thread-87129.html)



How to Search for Files from the Linux Command Line - xSicKxBot - 11-02-2018

How to Search for Files from the Linux Command Line

<div style="margin: 5px 5% 10px 5%;"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/11/how-to-search-for-files-from-the-linux-command-line.jpg" width="1643" height="929" title="" alt="" /></div><div><div><img src="http://www.sickgaming.net/blog/wp-content/uploads/2018/11/how-to-search-for-files-from-the-linux-command-line.jpg" class="ff-og-image-inserted" /></div>
<p><strong><span><span>Learn how to use the find command in this tutorial from our archives. </span></span></strong></p>
<p><span><span>It goes without saying that every good Linux desktop environment offers the ability to search your file system for files and folders. If your default desktop doesn’t — because this is Linux — you can always install an app to make searching your directory hierarchy a breeze.</span></span></p>
<p><span><span>But what about the command line? If you happen to frequently work in the command line or you administer GUI-less Linux servers, where do you turn when you need to locate a file? Fortunately, Linux has exactly what you need to locate the files in question, built right into the system.</span></span></p>
<p><span><span>The command in question is </span><em><span>find</span></em><span>. To make the understanding of this command even more enticing, once you know it, you can start working it into your Bash scripts. That’s not only convenience, that’s power.</span></span></p>
<p><span><span>Let’s get up to speed with the </span><span>find </span><span>command so you can take control of locating files on your Linux servers and desktops, without the need of a GUI.</span></span></p>
<h3><span><span>How to use the find command</span></span></h3>
<p><span><span>When I first glimpsed Linux, back in 1997, I didn’t quite understand how the find command worked; therefore, it never seemed to function as I expected. It seemed simple; issue the command </span><em><span>find FILENAME</span></em><span><em> </em>(where FILENAME is the name of the file) and the command was supposed to locate the file and report back. Little did I know there was more to the command than that. Much more.</span></span></p>
<p><span><span>If you issue the command </span><em><span>man find</span></em><span>, you’ll see the syntax of the<em> </em></span><em><span>find </span></em><span>command is:</span></span></p>
<pre>
<em><span><span>find [-H] [-L] [-P] [-D debugopts] [-Olevel] [starting-point...] [expression]</span></span></em></pre>
<p><span><span>Naturally, if you’re unfamiliar with how </span><span>man </span><span>works, you might be confused about or overwhelmed by that syntax. For ease of understanding, let’s simplify that. The most basic syntax of a basic find command would look like this:</span></span></p>
<pre>
<span><span>find /path option filename</span></span></pre>
<p><span><span>Now we’ll see it at work.</span></span></p>
<h3><span><span>Find by name</span></span></h3>
<p><span><span>Let’s break down that basic command to make it as clear as possible. The most simplistic  structure of the find command should include a path for the file, an option, and the filename itself. You may be thinking, “If I know the path to the file, I’d already know where to find it!”. Well, the path for the file could be the root of your drive; so / would be a legitimate path. Entering that as your path would take </span><span>find</span><span> longer to process — because it has to start from scratch — but if you have no idea where the file is, you </span><span>can </span><span>start from there. In the name of efficiency, it is always best to have at least an idea where to start searching.</span></span></p>
<p><span><span>The next bit of the command is the option. As with most Linux commands, you have a number of available options. However, we are starting from the beginning, so let’s make it easy. Because we are attempting to find a file by name, we’ll use one of two options:</span></span></p>
<ul>
<li>
<p><span><span>name – case sensitive</span></span></p>
</li>
<li>
<p><span><span>iname – case insensitive</span></span></p>
</li>
</ul>
<p><span><span>Remember, Linux is very particular about case, so if you’re looking for a file named Linux.odt, the following command will return no results.</span></span></p>
<pre>
<span><span>find / -name linux.odt</span></span></pre>
<p><span><span>If, however, you were to alter the command by using the </span><span>-iname</span><span> option, the find command would locate your file, regardless of case. So the new command looks like:</span></span></p>
<pre>
<span><span>find / -iname linux.odt</span></span></pre>
<h3><span><span>Find by type</span></span></h3>
<p><span><span>What if you’re not so concerned with locating a file by name but would rather locate all files of a certain type? Some of the more common file descriptors are:</span></span></p>
<ul>
<li>
<p><span><span>f – regular file</span></span></p>
</li>
<li>
<p><span><span>d – directory</span></span></p>
</li>
<li>
<p><span><span>l – symbolic link</span></span></p>
</li>
<li>
<p><span><span>c – character devices</span></span></p>
</li>
<li>
<p><span><span>b – block devices</span></span></p>
</li>
</ul>
<p><span><span>Now, suppose you want to locate all block devices (a file that refers to a device) on your system. With the help of the </span><span><em>-type</em> </span><span>option, we can do that like so:</span></span></p>
<pre>
<em><span><span>find / -type c</span></span></em></pre>
<p><span><span>The above command would result in quite a lot of output (much of it indicating </span><span>permission denied</span><span>), but would include output similar to:</span></span></p>
<pre>
<em><span><span>/dev/hidraw6</span>
<span>/dev/hidraw5</span>
<span>/dev/vboxnetctl</span>
<span>/dev/vboxdrvu</span>
<span>/dev/vboxdrv</span>
<span>/dev/dmmidi2</span>
<span>/dev/midi2</span>
<span>/dev/kvm</span></span></em></pre>
<p><span><span>Voilà! Block devices.</span></span></p>
<p><span><span>We can use the same option to help us look for configuration files. Say, for instance, you want to locate all regular files that end in the .conf extension. This command would look something like:</span></span></p>
<pre>
<span><span>find / -type f -name "*.conf"</span></span></pre>
<p><span><span>The above command would traverse the entire directory structure to locate all regular files ending in .conf. If you know most of your configuration files are housed in </span><span>/etc</span><span>, you could specify that like so:</span></span></p>
<pre>
<span><span>find /etc -type f -name “*.conf”</span></span></pre>
<p><span><span>The above command would list all of your .conf files from /etc (</span><span>Figure 1</span><span>).</span></span></p>
<h3><span><span>Outputting results to a file</span></span></h3>
<p><span><span>One really handy trick is to output the results of the search into a file. When you know the output might be extensive, or if you want to comb through the results later, this can be incredibly helpful. For this, we’ll use the same example as above and pipe the results into a file called <em>conf_search</em>. This new command would look like: ​</span></span></p>
<pre>
<span><span>find /etc -type f -name “*.conf” &gt; conf_search</span></span></pre>
<p><span><span>You will now have a file (</span><em><span>conf_search</span></em><span>) that contains all of the results from the </span><span>find </span><span>command issued.</span></span></p>
<h3><span><span>Finding files by size</span></span></h3>
<p><span><span>Now we get to a moment where the </span><span>find </span><span>command becomes incredibly helpful. I’ve had instances where desktops or servers have found their drives mysteriously filled. To quickly make space (or help locate the problem), you can use the </span><span>find </span><span>command to locate files of a certain size. Say, for instance, you want to go large and locate files that are over 1000MB. The </span><span>find </span><span>command can be issued, with the help of the </span><em><span>-size</span></em><span><em> </em>option, like so:</span></span></p>
<pre>
<span><span>find / -size +1000MB</span></span></pre>
<p><span><span>You might be surprised at how many files turn up. With the output from the command, you can comb through the directory structure and free up space or troubleshoot to find out what is mysteriously filling up your drive.</span></span></p>
<p><span><span>You can search with the following size descriptions:</span></span></p>
<ul>
<li>
<p><span><span>c – bytes</span></span></p>
</li>
<li>
<p><span><span>k – Kilobytes</span></span></p>
</li>
<li>
<p><span><span>M – Megabytes</span></span></p>
</li>
<li>
<p><span><span>G – Gigabytes</span></span></p>
</li>
<li>
<p><span><span>b – 512-byte blocks</span></span></p>
</li>
</ul>
<h3><span><span>Keep learning</span></span></h3>
<p><span><span>We’ve only scratched the surface of the </span><span>find </span><span>command, but you now have a fundamental understanding of how to locate files on your Linux systems. Make sure to issue the command<em> </em></span><em><span>man find</span></em><span><em> </em>to get a deeper, more complete, knowledge of how to make this powerful tool work for you.</span></span></p>
<p><em>Learn more about Linux through the free <a href="https://training.linuxfoundation.org/linux-courses/system-administration-training/introduction-to-linux" target="_blank">“Introduction to Linux” </a>course from The Linux Foundation and edX.</em></p>
</div>