{"id":12607,"date":"2019-03-28T03:37:38","date_gmt":"2019-03-28T03:37:38","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=12607"},"modified":"2019-03-28T03:37:38","modified_gmt":"2019-03-28T03:37:38","slug":"ways-to-use-find-command-to-search-directories-more-efficiently","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/28\/ways-to-use-find-command-to-search-directories-more-efficiently\/","title":{"rendered":"Ways to Use \u2018find\u2019 Command to Search Directories More Efficiently"},"content":{"rendered":"<p>This tutorial will take you through the different ways of finding a directory in Linux. As you may already know, in\u00a0<a href=\"https:\/\/www.tecmint.com\/explanation-of-everything-is-a-file-and-types-of-files-in-linux\/\" target=\"_blank\" rel=\"noopener\">Linux everything is a file<\/a>\u00a0including directories. And one of the common things a Linux user will do within the command line is\u00a0<a href=\"https:\/\/www.tecmint.com\/35-practical-examples-of-linux-find-command\/\" target=\"_blank\" rel=\"noopener\">searching for a file or a directory<\/a>.<\/p>\n<p>There are several different means and utilities used for searching for files on the command line such as\u00a0<a href=\"https:\/\/www.tecmint.com\/linux-find-command-to-search-multiple-filenames-extensions\/\" target=\"_blank\" rel=\"noopener\">find<\/a>,\u00a0<strong>locate<\/strong>\u00a0and\u00a0<strong>which<\/strong>. However, the last utility (<strong>which<\/strong>) is only used for locating a command.<\/p>\n<p>For the scope of this tutorial, we will mainly focus on the\u00a0<a href=\"https:\/\/www.tecmint.com\/35-practical-examples-of-linux-find-command\/\" target=\"_blank\" rel=\"noopener\">find utility<\/a>, which searches files on a live Linux filesystem and is more efficient and reliable as compared to\u00a0<strong>locate<\/strong>.<\/p>\n<p>The downside of\u00a0<strong>locate<\/strong>\u00a0is that it reads one or more databases created by\u00a0<strong>updatedb<\/strong>, it does not search through a live filesystem. In addition, it does not as well offer flexibility regarding where to search from (starting point).<\/p>\n<p>Below is the syntax for running\u00a0<strong>locate<\/strong>\u00a0command:<\/p>\n<pre># locate [option] [search-pattern]\r\n<\/pre>\n<p>To demonstrate the disadvantage of\u00a0<strong>locate<\/strong>, let us assume we are searching for a directory named\u00a0<code>pkg<\/code>\u00a0in the current working directory.<\/p>\n<p><strong>Note<\/strong>: In the command below, the option\u00a0<code>--basename<\/code>\u00a0or\u00a0<code>-b<\/code>\u00a0tells\u00a0<strong>locate<\/strong>\u00a0to only match the file (directory) basename (which is exactly\u00a0<strong>pkg<\/strong>) but not the path (<strong>\/path\/to\/pkg<\/strong>). Where\u00a0<code>\\<\/code>\u00a0is a globbing character, it disables the implicit replacement of\u00a0<strong>pkg<\/strong>\u00a0by\u00a0<strong>*pkg*<\/strong>.<\/p>\n<pre>$ locate --basename '\\pkg'\r\n<\/pre>\n<div id=\"attachment_23426\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/11\/Find-Directory-Using-locate-Command.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-23426\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/11\/Find-Directory-Using-locate-Command.png\" sizes=\"auto, (max-width: 778px) 100vw, 778px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/11\/Find-Directory-Using-locate-Command.png 778w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/11\/Find-Directory-Using-locate-Command-768x96.png 768w\" alt=\"Find Directory Using locate Command\" width=\"778\" height=\"97\" aria-describedby=\"caption-attachment-23426\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-23426\" class=\"wp-caption-text\">Find Directory Using locate Command<\/p>\n<\/div>\n<p>As you can see from the command output above,\u00a0<strong>locate<\/strong>\u00a0will search beginning from the root\u00a0<strong>(\/)<\/strong>\u00a0directory, that is why other directories with the same name are matched.<\/p>\n<p>Therefore, to deal with this issue, use\u00a0<strong>find<\/strong>\u00a0by following the simplified syntax below:<\/p>\n<pre>$ find starting-point options [expression]\r\n<\/pre>\n<p>Let us look at a few examples.<\/p>\n<p>To search for the same directory\u00a0<code>(pkg)<\/code>\u00a0above, within the current working directory, run the following command, where the\u00a0<code>-name<\/code>\u00a0flag reads the expression which in this case is the directory basename.<\/p>\n<pre>$ find . -name \"pkg\"\r\n<\/pre>\n<p>If you encounter \u201c<strong>Permission denied<\/strong>\u201d errors, use\u00a0<a href=\"https:\/\/www.tecmint.com\/su-vs-sudo-and-how-to-configure-sudo-in-linux\/\" target=\"_blank\" rel=\"noopener\">sudo command\u00a0<\/a>like so:<\/p>\n<pre>$ sudo find . -name \"pkg\"\r\n<\/pre>\n<div id=\"attachment_23427\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/11\/Search-Directory-Using-find-Command.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-23427\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/11\/Search-Directory-Using-find-Command.png\" alt=\"Search a Directory Using find Command\" width=\"529\" height=\"192\" aria-describedby=\"caption-attachment-23427\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-23427\" class=\"wp-caption-text\">Search a Directory Using find Command<\/p>\n<\/div>\n<p>You can prevent\u00a0<a href=\"https:\/\/www.tecmint.com\/linux-find-command-to-search-multiple-filenames-extensions\/\" target=\"_blank\" rel=\"noopener\">find from searching for other file types<\/a>\u00a0except directories by using\u00a0<code>-type<\/code>\u00a0flag to specify the type of file (in the command below\u00a0<code>d<\/code>\u00a0means directory) as follows:<\/p>\n<pre>$ sudo find . -type d -name \"pkg\"\r\n<\/pre>\n<p>Furthermore, if you wish to\u00a0<a href=\"https:\/\/www.tecmint.com\/15-basic-ls-command-examples-in-linux\/\" target=\"_blank\" rel=\"noopener\">list the directory in a long listing format<\/a>, employ the action switch\u00a0<code>-ls<\/code>:<\/p>\n<pre>$ sudo find . -type d -name \"pkg\" -ls\r\n<\/pre>\n<div id=\"attachment_23428\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/11\/Find-and-List-Directory.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-23428\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/11\/Find-and-List-Directory.png\" sizes=\"auto, (max-width: 933px) 100vw, 933px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/11\/Find-and-List-Directory.png 933w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/11\/Find-and-List-Directory-768x111.png 768w\" alt=\"Find and List Directory\" width=\"933\" height=\"135\" aria-describedby=\"caption-attachment-23428\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-23428\" class=\"wp-caption-text\">Find and List Directory<\/p>\n<\/div>\n<p>Next, the option\u00a0<code>-iname<\/code>\u00a0will enable a case insensitive search:<\/p>\n<pre>$ sudo find . -type d -iname \"pkg\" \r\n$ sudo find . -type d -iname \"PKG\" \r\n<\/pre>\n<div id=\"attachment_23429\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/11\/Find-Directory-with-Case-insensitive.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-23429\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2016\/11\/Find-Directory-with-Case-insensitive.png\" alt=\"Find Directory with Case Sensitive\" width=\"595\" height=\"135\" aria-describedby=\"caption-attachment-23429\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p id=\"caption-attachment-23429\" class=\"wp-caption-text\">Find Directory with Case Sensitive<\/p>\n<\/div>\n<p>To find more interesting and advanced usage information, read the man pages of\u00a0<strong>find<\/strong>\u00a0and\u00a0<strong>locate<\/strong>.<\/p>\n<pre>$ man find\r\n$ man locate\r\n<\/pre>\n<p>As a last remark, the\u00a0<a href=\"https:\/\/www.tecmint.com\/35-practical-examples-of-linux-find-command\/\" target=\"_blank\" rel=\"noopener\">find command<\/a>\u00a0is more reliable and efficient for searching files ( or directories) in a Linux system when weighed against the\u00a0<strong>locate<\/strong>\u00a0command.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/find-directory-in-linux\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial will take you through the different ways of finding a directory in Linux. As you may already know, in\u00a0Linux everything is a file\u00a0including directories. And one of the common things a Linux user will do within the command line is\u00a0searching for a file or a directory. There are several different means and utilities &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/28\/ways-to-use-find-command-to-search-directories-more-efficiently\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Ways to Use \u2018find\u2019 Command to Search Directories More Efficiently&#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-12607","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\/12607","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=12607"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12607\/revisions"}],"predecessor-version":[{"id":12608,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/12607\/revisions\/12608"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=12607"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=12607"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=12607"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}