{"id":4181,"date":"2011-09-16T12:17:24","date_gmt":"2011-09-16T06:47:24","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=4181"},"modified":"2012-02-20T04:49:48","modified_gmt":"2012-02-19T23:19:48","slug":"find-vs-locate-optimize-your-search","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/find-vs-locate-optimize-your-search\/","title":{"rendered":"Find v\/s Locate &#8230; Optimize your search .."},"content":{"rendered":"<p>A few days back, I was on a server wherein I had to find the location of a file in the\u00a0file system. Naturally like most newbies, I instantly hit the &#8216;find&#8217; command. \u00a0Something like<\/p>\n<p>[shell]sudo find \/ -name someFileName.ext[\/shell]<\/p>\n<p>Naturally the command took a lot of time to find the file (Local to server relay not being helpful either).<br \/>\nAt one point it grew so frustrating that I had to stop it and search for an alternative . Thats when I met <strong>Locate<\/strong>.. The lesser known brother of Find, WhereIs and Grep.<\/p>\n<p style=\"padding-bottom: 10px\">\n<p>As soon as i hit the command<\/p>\n<p>[shell]locate someFileName.ext[\/shell]<\/p>\n<p><strong>BING!!! <\/strong><\/p>\n<p style=\"padding-bottom: 10px\">\n<p>The result popped up.. in less than a second !!!<br \/>\nThis was a real time saving alternative. Comparing 1 sec to over 20 sec and that too over a remote server, man I could have killed for this sort of performance. Thanks to &#8216;locate&#8217; for having saved me some\u00a0prison\u00a0time \ud83d\ude00 .<\/p>\n<p style=\"padding-bottom: 10px\">\n<p>Now here&#8217;s the real thing &#8230;<br \/>\n<strong>Locate<\/strong> searches for files from a <strong>database<\/strong> that is created automatically. This database contains the list of files in a\u00a0file system\u00a0and their associated paths. So essentially locate simply searches through a file and returns a match.<br \/>\n<strong> Find<\/strong> on the other hand searches through files in the <strong>file system in real time<\/strong>. It&#8217;ll definitely be slower because it will search each and every path recursively from it&#8217;s start point.<\/p>\n<p style=\"padding-bottom: 10px\">\n<p><strong>Locate<\/strong> therefore is much <strong>faster<\/strong> as compared to <strong>find<\/strong> when searching for unique items.<\/p>\n<p style=\"padding-bottom: 10px\">\n<p style=\"padding-bottom: 10px\">\n<p>But there are a few disadvantages to this method as well.<\/p>\n<p style=\"padding-bottom: 10px\">\n<p><strong>Locate<\/strong> might miss out on files created within a 24 hour period. This is because the database through which locate searches is updated automatically through a cron job that usually runs in 24 hour intervals. <strong>Find<\/strong> is the winner here &#8230; however to manually run the job for updating the database in Ubuntu, you can simply run a command<\/p>\n<p>[shell]sudo updatedb[\/shell]<\/p>\n<p>and get the job done.<\/p>\n<p style=\"padding-bottom: 10px\">\n<p><strong>Find<\/strong> also contains a number of filters like \u00a0specifying type -d (Directory) which aren&#8217;t present in locate. \u00a0This means that locate command for a directory would return files as well as directories.\u00a0So in essence<strong> locate returns a large number of results whereas find can narrow them down<\/strong>.<\/p>\n<p style=\"padding-bottom: 10px\">Essentially it&#8217;s the situation that should describe which command to use. But I would still suggest you give locate a try. You would be\u00a0pleasantly\u00a0surprised.<\/p>\n<p style=\"padding-bottom: 10px\">\n<p style=\"padding-bottom: 10px\">\n<div>Manoj Mohan<\/div>\n<div>manoj<span style=\"text-decoration: line-through\">(at)<\/span>intelligrape<span style=\"text-decoration: line-through\">(dot)<\/span>com<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>A few days back, I was on a server wherein I had to find the location of a file in the\u00a0file system. Naturally like most newbies, I instantly hit the &#8216;find&#8217; command. \u00a0Something like [shell]sudo find \/ -name someFileName.ext[\/shell] Naturally the command took a lot of time to find the file (Local to server relay [&hellip;]<\/p>\n","protected":false},"author":32,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":6},"categories":[1],"tags":[634,260,635],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/4181"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/32"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=4181"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/4181\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=4181"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=4181"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=4181"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}