{"id":45716,"date":"2024-03-03T15:57:39","date_gmt":"2024-03-03T15:57:39","guid":{"rendered":"https:\/\/ubuntuhandbook.org\/?p=45716"},"modified":"2025-01-09T02:38:45","modified_gmt":"2025-01-09T02:38:45","slug":"locate-command-examples","status":"publish","type":"post","link":"https:\/\/ubuntuhandbook.org\/index.php\/2024\/03\/locate-command-examples\/","title":{"rendered":"Use Locate command to Find Files (with Examples)"},"content":{"rendered":"<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/03\/terminal-logo.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-thumbnail wp-image-37909\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/03\/terminal-logo-250x250.webp\" alt=\"\" width=\"250\" height=\"250\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/03\/terminal-logo-250x250.webp 250w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/03\/terminal-logo-300x300.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/03\/terminal-logo-600x600.webp 600w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/03\/terminal-logo-768x768.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/03\/terminal-logo.webp 1200w\" sizes=\"auto, (max-width: 250px) 100vw, 250px\" \/><\/a><\/p>\n<p>This tutorial shows how to use <code>locate<\/code> command to quickly find files in your Linux system.<\/p>\n<p>In Ubuntu Linux, the <code>locate<\/code> command is provided by the plocate package. It&#8217;s very fast command line search tool, that can find all files in the system matching the given pattern. It rarely needs to scan through its entire database, and most I\/O is done asynchronously, but the results are synchronized.<\/p>\n<p>Most importantly plocate is easy to use! I regularly use it to search app icon images that are in use in most pages of this website.<\/p>\n<p><!--more--><\/p>\n<h3>1. Install Plocate<\/h3>\n<p>Plocate is available in system repository, but not pre-installed in Ubuntu Linux<\/p>\n<p>To get the tool, you need to first run command to install the package:<\/p>\n<pre>sudo apt install plocate<\/pre>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/apt-plocate.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-45717\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/apt-plocate-700x429.webp\" alt=\"\" width=\"610\" height=\"374\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/apt-plocate-700x429.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/apt-plocate-300x184.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/apt-plocate.webp 723w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<h3>2. Update Plocate database<\/h3>\n<p>Installing plocate command will automatically build the database, which is by default available in <code>\/var\/lib\/plocate<\/code> directory.<\/p>\n<p>For the time being, the files vary in your system. So, you may need to manually update the database regularly by running command:<\/p>\n<pre>sudo updatedb<\/pre>\n<p>In my case in Ubuntu 22.04 Desktop, the database only takes 18 MB disk space.<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/updatedb.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-45719\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/updatedb-700x346.webp\" alt=\"\" width=\"610\" height=\"302\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/updatedb-700x346.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/updatedb-300x148.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/updatedb-768x380.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/updatedb.webp 786w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<h3>3. Use locate command to find files<\/h3>\n<p>The locate command is quite easy to use. You can use it to <b>find file with accurate filename<\/b>. For example:<\/p>\n<pre>locate firefox.desktop<\/pre>\n<p><i>The command above will list all <code>firefox.desktop<\/code> files in your system, including the full path.<\/i><\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/locate-firefox.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-45720\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/locate-firefox-700x354.webp\" alt=\"\" width=\"610\" height=\"308\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/locate-firefox-700x354.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/locate-firefox-300x152.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/locate-firefox-768x389.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/locate-firefox.webp 804w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p>Sometimes, the files you&#8217;re going to search include both upper-case and lower-case letters.<\/p>\n<p>In the case, you may use <code>-i<\/code> (or <code>--ignore-case<\/code>) to <b>do case-insensitive search<\/b>.<\/p>\n<pre>locate -i gthumb.desktop<\/pre>\n<p><i>As you see in the screenshot, the command outputs <code>gThumb.desktop<\/code> in the search results.<\/i><br \/>\n<a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/locate-ignore-case.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-45721\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/locate-ignore-case-700x411.webp\" alt=\"\" width=\"610\" height=\"358\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/locate-ignore-case-700x411.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/locate-ignore-case-300x176.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/locate-ignore-case-768x450.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/locate-ignore-case.webp 786w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p>The locate command supports multiple patterns. When more than one are given, then it will search for files that match all of them!<\/p>\n<p>For example, you can <b>search files in certain directory<\/b> by command:<\/p>\n<pre>locate \/usr\/share\/icons firefox<\/pre>\n<p><i>The command above will search files under <code>\/usr\/share\/icons<\/code> directory with <code>firefox<\/code> in filename or path.<\/i><br \/>\nYou can add more conditions, for example the command below will search files under <code>\/usr\/share\/icons<\/code> directory with <code>firefox<\/code>, <code>.svg<\/code> and <code>64x64<\/code> in either path or filename.<\/p>\n<pre>locate \/usr\/share\/icons\/ firefox .svg 64x64<\/pre>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/locate-multiple-keywords.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-45722\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/locate-multiple-keywords-700x367.webp\" alt=\"\" width=\"610\" height=\"320\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/locate-multiple-keywords-700x367.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/locate-multiple-keywords-300x157.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/locate-multiple-keywords-768x403.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/locate-multiple-keywords.webp 1104w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p>As you see above, <code>locate<\/code> also search the given keyword from the path name (folder and directory).<\/p>\n<p>To skip them, and <b>only match against the file-name<\/b>, use either <code>-b<\/code> or (<code>--basename<\/code>) flag.<\/p>\n<pre>locate --basename Papirus<\/pre>\n<p><i>The command above only search files include <code>Papirus<\/code> in file-names, but exclude the folders and directories (but still printed).<\/i><\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/locate-basename.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-45723\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/locate-basename-700x342.webp\" alt=\"\" width=\"610\" height=\"298\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/locate-basename-700x342.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/locate-basename-300x147.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/locate-basename-768x375.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/locate-basename.webp 1087w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p>If you want to <b>count how many matches<\/b>, use either <code>-c<\/code> or <code>--count<\/code> option. In the case, it won&#8217;t print the files, instead only shows a total number.<\/p>\n<p>For choice, you may also <strong>limit matches<\/strong> with <code>-l<\/code> or <code>--limit<\/code> option. In the case, it stops searching after limited number of matches have been found.<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/plocate-count.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-45724\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/plocate-count-700x475.webp\" alt=\"\" width=\"610\" height=\"414\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/plocate-count-700x475.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/plocate-count-300x203.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/plocate-count-768x521.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/plocate-count.webp 786w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p>Advanced users can <b>use regular expression as search pattern<\/b> by using either <code>-r<\/code> or <code>--regexp<\/code> (or <code>--regex<\/code> for POSIX ex\u2010tended regular expressions).<\/p>\n<p>For example, use command below to search all <code>.png<\/code> files under user&#8217;s Pictures folder, with <code>screenshot<\/code> follow with number in file-name.<\/p>\n<pre>locate --regexp --ignore-case $HOME\/pictures .*screenshot.*[0-9].*\\.png$<\/pre>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/locate-regex.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-45725\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/locate-regex-700x435.webp\" alt=\"\" width=\"610\" height=\"379\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/locate-regex-700x435.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/locate-regex-300x186.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/locate-regex-768x477.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/03\/locate-regex.webp 858w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<h3>Summary<\/h3>\n<p>Ubuntu Linux provides <code>plocate<\/code> package in system repository, which includes <code>locate<\/code> command to quickly search files in the system.<\/p>\n<p>User can simply run <code>locate<\/code> command follow with pattern to search in files and paths. Multiple patterns (separated with space) are allowed, and it will\u00a0search for files that match all of them! It by default also search in path (folder\/directory names), which can be disabled via either <code>-b<\/code> or <code>--basename<\/code> flag. Use <code>-i<\/code> or <code>--ignore-case<\/code> can do case-insensitive search in case you don&#8217;t know if there are upper-case (or lower-case) letters in filenames.<\/p>\n<p>And, there are a few other options, such as <code>--limit<\/code> to limit search results, and <code>count<\/code> to print total file number. Advanced users can use <code>-r<\/code> or <code>--regexp<\/code> regular expressions to do file search in more flexible way.<\/p>","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to use locate command to quickly find files in your Linux system. In Ubuntu Linux, the locate command is provided by the plocate package. It&#8217;s very fast command line search tool, that can find all files in the system matching the given pattern. It rarely needs to scan through its entire [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":37909,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[2159,2161,2160],"class_list":["post-45716","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-howtos","tag-linux-command","tag-locate","tag-plocate"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts\/45716","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/comments?post=45716"}],"version-history":[{"count":0,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts\/45716\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/media\/37909"}],"wp:attachment":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/media?parent=45716"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/categories?post=45716"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/tags?post=45716"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}