{"id":11368,"date":"2019-03-13T08:14:54","date_gmt":"2019-03-13T08:14:54","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=11368"},"modified":"2019-03-13T08:14:54","modified_gmt":"2019-03-13T08:14:54","slug":"10-useful-locate-command-practical-examples-for-linux-newbies","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/13\/10-useful-locate-command-practical-examples-for-linux-newbies\/","title":{"rendered":"10 Useful \u2018locate\u2019 Command Practical Examples for Linux Newbies"},"content":{"rendered":"<p>One of most obnoxious experiences that most new users of the Linux platform usually face is the inability to find the simplest and yet more effective means of looking up files on their system.<\/p>\n<p>Linux, like almost any other operating system, utilizes several mechanisms to answer search queries for users. Two of the most popular file searching utilities accessible to users are called\u00a0<a href=\"https:\/\/www.tecmint.com\/35-practical-examples-of-linux-find-command\/\" target=\"_blank\" rel=\"noopener\">find<\/a>\u00a0and\u00a0<strong>locate<\/strong>.<\/p>\n<p>Now, it is important to note that both search processes work extremely well but nonetheless, the center of this article will be more on the\u00a0<strong>locate<\/strong>\u00a0utility, which is the more convenient of the two as it uses more efficient ways to quickly process queries inputted by the users.<\/p>\n<p>The\u00a0<strong>locate<\/strong>\u00a0utility works better and faster than it\u2019s\u00a0<strong>find<\/strong>\u00a0counterpart because instead of searching the file system when a file search is initiated \u2013 Something find does \u2013 locate would look through a database. This database contains bits and parts of files and their corresponding paths on your system.<\/p>\n<p>Here are ten simple locate commands to set you up in becoming more productive with your Linux machine.<\/p>\n<h3>1. Using locate Command<\/h3>\n<p>Firing locate command to look for a file is pretty easy and straightforward. All you need to do is type:<\/p>\n<pre><strong>$ locate LAMP-Setup.odt<\/strong>\r\n\r\n\/home\/tecmint\/LAMP-Setup.odt\r\n\/home\/tecmint\/TecMint.com\/LAMP-Setup.odt\r\n<\/pre>\n<h3>2. Limit Search Queries to a Specific Number<\/h3>\n<p>You can limit your search returns to a required number to avoid redundancy with your search results using the\u00a0<code>-n<\/code>\u00a0command.<\/p>\n<p>For example, if you want just\u00a0<strong>20<\/strong>\u00a0results from your queries, you can type the following command:<\/p>\n<pre><strong>$ locate \"*.html\" -n 20<\/strong>\r\n\r\n\/home\/tecmint\/.config\/google-chrome\/Default\/Extensions\/aapocclcgogkmnckokdopfmhonfmgoek\/0.9_0\/main.html\r\n\/home\/tecmint\/.config\/google-chrome\/Default\/Extensions\/aohghmighlieiainnegkcijnfilokake\/0.9_0\/main.html\r\n\/home\/tecmint\/.config\/google-chrome\/Default\/Extensions\/felcaaldnbdncclmgdcncolpebgiejap\/1.1_0\/main.html\r\n\/home\/tecmint\/.config\/google-chrome\/Default\/Extensions\/kbfnbcaeplbcioakkpcpgfkobkghlhen\/14.752.848_0\/forge.html\r\n\/home\/tecmint\/.config\/google-chrome\/Default\/Extensions\/kbfnbcaeplbcioakkpcpgfkobkghlhen\/14.752.848_0\/src\/popup.html\r\n\/home\/tecmint\/.config\/google-chrome\/Default\/Extensions\/nlipoenfbbikpbjkfpfillcgkoblgpmj\/3.9.16_0\/additional-feature.html\r\n\/home\/tecmint\/.config\/google-chrome\/Default\/Extensions\/nlipoenfbbikpbjkfpfillcgkoblgpmj\/3.9.16_0\/background.html\r\n\/home\/tecmint\/.config\/google-chrome\/Default\/Extensions\/nlipoenfbbikpbjkfpfillcgkoblgpmj\/3.9.16_0\/edit.html\r\n\/home\/tecmint\/.config\/google-chrome\/Default\/Extensions\/nlipoenfbbikpbjkfpfillcgkoblgpmj\/3.9.16_0\/help.html\r\n\/home\/tecmint\/.config\/google-chrome\/Default\/Extensions\/nlipoenfbbikpbjkfpfillcgkoblgpmj\/3.9.16_0\/options.html\r\n\/home\/tecmint\/.config\/google-chrome\/Default\/Extensions\/nlipoenfbbikpbjkfpfillcgkoblgpmj\/3.9.16_0\/popup.html\r\n\/home\/tecmint\/.config\/google-chrome\/Default\/Extensions\/nlipoenfbbikpbjkfpfillcgkoblgpmj\/3.9.16_0\/purchase.html\r\n\/home\/tecmint\/.config\/google-chrome\/Default\/Extensions\/nlipoenfbbikpbjkfpfillcgkoblgpmj\/3.9.16_0\/upload.html\r\n\/home\/tecmint\/.config\/google-chrome\/Default\/Extensions\/nlipoenfbbikpbjkfpfillcgkoblgpmj\/3.9.16_0\/oauth2\/oauth2.html\r\n\/home\/tecmint\/.config\/google-chrome\/Default\/Extensions\/nmmhkkegccagdldgiimedpiccmgmieda\/1.0.0.2_0\/html\/craw_window.html\r\n\/home\/tecmint\/.config\/google-chrome\/Default\/Extensions\/pkedcjkdefgpdelpbcmbmeomcjbeemfm\/5516.1005.0.3_0\/cast_route_details.html\r\n\/home\/tecmint\/.config\/google-chrome\/Default\/Extensions\/pkedcjkdefgpdelpbcmbmeomcjbeemfm\/5516.1005.0.3_0\/feedback.html\r\n\/home\/tecmint\/.config\/google-chrome\/Default\/Extensions\/pkedcjkdefgpdelpbcmbmeomcjbeemfm\/5516.1005.0.3_0\/cast_setup\/devices.html\r\n\/home\/tecmint\/.config\/google-chrome\/Default\/Extensions\/pkedcjkdefgpdelpbcmbmeomcjbeemfm\/5516.1005.0.3_0\/cast_setup\/index.html\r\n\/home\/tecmint\/.config\/google-chrome\/Default\/Extensions\/pkedcjkdefgpdelpbcmbmeomcjbeemfm\/5516.1005.0.3_0\/cast_setup\/offers.html\r\n<\/pre>\n<p>The results will show the first 20 files that end with\u00a0<code>.html<\/code>.<\/p>\n<h3>3. Display The Number of Matching Entries<\/h3>\n<p>If you want to display the count of all matching entries of file \u201c<strong>tecmint<\/strong>\u201c, use the\u00a0<strong>locate -c<\/strong>\u00a0command.<\/p>\n<pre><strong>$ locate -c [tecmint]*<\/strong>\r\n\r\n1550\r\n<\/pre>\n<h3>4. Ignore Case Sensitive Locate Outputs<\/h3>\n<p>By default,\u00a0<strong>locate<\/strong>\u00a0is configured to process queries in a case sensitive manner meaning\u00a0<code>TEXT.TXT<\/code>\u00a0will point you to a different result than\u00a0<code>text.txt<\/code>.<\/p>\n<p>To have\u00a0<strong>locate<\/strong>\u00a0command ignore case sensitivity and show results for both uppercase and lowercase queries, input commands with the\u00a0<code>-i<\/code>\u00a0option.<\/p>\n<pre><strong>$ locate -i *text.txt*<\/strong>\r\n\r\n\/home\/tecmint\/TEXT.txt\r\n\/home\/tecmint\/text.txt\r\n<\/pre>\n<h3>5. Refresh mlocate Database<\/h3>\n<p>Since\u00a0<strong>locate<\/strong>\u00a0command relies on a database called\u00a0<strong>mlocate<\/strong>. The said database needs to be updated regularly for the command utility to work<br \/>\nefficiently.<\/p>\n<p>To update the\u00a0<strong>mlocate<\/strong>\u00a0database, you use a utility called\u00a0<strong>updatedb<\/strong>. It should be noted that you will need superuser privileges for this to work properly, is it needs to be executed as root or sudo privileges.<\/p>\n<pre>$ sudo updatedb\r\n<\/pre>\n<h3>6. Display Only Files Present in Your System<\/h3>\n<p>When you have an updated\u00a0<strong>mlocate<\/strong>\u00a0database**,\u00a0<strong>locate<\/strong>\u00a0command still produces results of files whose physical copies are deleted from your system.<\/p>\n<p>To avoid seeing results of files not present in your machine at the time of punching in the command, you will need to use the\u00a0<strong>locate-e<\/strong>\u00a0command. The process searches your system to verify the existence of the file you\u2019re looking for even if it is still present in your\u00a0<strong>mlocate.db<\/strong>.<\/p>\n<pre><strong>$ locate -i -e *text.txt*<\/strong>\r\n\r\n\/home\/tecmint\/text.txt\r\n<\/pre>\n<h3>7. Separate Output Entries Without New Line<\/h3>\n<p><strong>locate<\/strong>\u00a0command\u2019s default separator is the newline\u00a0<code>(\\\\n)<\/code>\u00a0character. But if you prefer to use a different separator like the\u00a0<strong>ASCII NUL<\/strong>, you can do so using the\u00a0<code>-0<\/code>\u00a0command line option.<\/p>\n<pre><strong>$ locate -i -0 *text.txt*<\/strong>\r\n\r\n\/home\/tecmint\/TEXT.txt\/home\/tecmint\/text.txt\r\n<\/pre>\n<h3>8. Review Your Locate Database<\/h3>\n<p>If you\u2019re in doubt as to the current status of your\u00a0<strong>mlocate.db<\/strong>, you can easily view the locate database statistics by using the\u00a0<code>-S<\/code>\u00a0command.<\/p>\n<pre><strong>$ locate -S<\/strong>\r\n\r\nDatabase \/var\/lib\/mlocate\/mlocate.db:\r\n\t32,246 directories\r\n\t4,18,850 files\r\n\t2,92,36,692 bytes in file names\r\n\t1,13,64,319 bytes used to store database\r\n<\/pre>\n<h3>9. Suppress Error Messages in Locate<\/h3>\n<p>Constantly trying to access your locate database does sometimes yield unnecessary error messages stating that you do not have the required privileges to have root access to the\u00a0<strong>mlocate.db<\/strong>, because you\u2019re only a normal user and not the required Superuser.<\/p>\n<p>To completely do away with these message, use the\u00a0<code>-q<\/code>\u00a0command.<\/p>\n<pre>$ locate \"\\*.dat\" -q*\r\n<\/pre>\n<h3>10. Choose a Different mlocate Location<\/h3>\n<p>If you\u2019re inputting queries looking for results not present in the default\u00a0<strong>mlocate<\/strong>\u00a0database and want answers from a different\u00a0<strong>mlocate.db<\/strong>\u00a0located somewhere else in your system, you can point the locate command to a different\u00a0<strong>mlocate<\/strong>\u00a0database at a different part of your system with the\u00a0<code>-d<\/code>\u00a0command.<\/p>\n<pre>$ locate -d &lt;new db path&gt; &lt;filename&gt;\r\n<\/pre>\n<p><strong>locate<\/strong>\u00a0command might seem like one of those utilities that does everything you asked it to do without much of a hustle but in truth, in order for the process to keep its efficiency, the\u00a0<strong>mlocate.db<\/strong>\u00a0needs to be fed with information every now and then. Failure to do so might render the program a bit useless.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/linux-locate-command-practical-examples\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of most obnoxious experiences that most new users of the Linux platform usually face is the inability to find the simplest and yet more effective means of looking up files on their system. Linux, like almost any other operating system, utilizes several mechanisms to answer search queries for users. Two of the most popular &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/13\/10-useful-locate-command-practical-examples-for-linux-newbies\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;10 Useful \u2018locate\u2019 Command Practical Examples for Linux Newbies&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-11368","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11368","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=11368"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11368\/revisions"}],"predecessor-version":[{"id":11370,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11368\/revisions\/11370"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=11368"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=11368"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=11368"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}