{"id":7067,"date":"2014-04-09T05:06:17","date_gmt":"2014-04-08T19:06:17","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/wordpress\/?p=7067"},"modified":"2014-04-09T05:06:17","modified_gmt":"2014-04-08T19:06:17","slug":"linux-find-andor-locate-filesdirectories-primer-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/linux-find-andor-locate-filesdirectories-primer-tutorial\/","title":{"rendered":"Linux Find and\/or Locate Files\/Directories Primer Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/Linux\/locate\/LocateFind_Linux.jpg\"><img decoding=\"async\" style=\"float:left; border: 15px solid pink;\" alt=\"Linux Find and\/or Locate Files\/Directories Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/Linux\/locate\/LocateFind_Linux.jpg\" title=\"Linux Find and\/or Locate Files\/Directories Primer Tutorial\" \/><\/a><p class=\"wp-caption-text\">Linux Find and\/or Locate Files\/Directories Primer Tutorial<\/p><\/div>\n<p>Linux (or Unix) servers have two really useful commands which help the user find files and\/or directories &#8230; <a target=_blank title='Linux\/unix locate command help from Computerhope ... thanks' href='http:\/\/www.computerhope.com\/unix\/ulocate.htm'>locate<\/a> and <a target=_blank title='Linux\/unix find command help from Computerhope ... thanks' href='http:\/\/www.computerhope.com\/unix\/ufind.htm'>find<\/a>.  Doubt there are many people out there who have not mislaid a computer file at some time or other.   The GUI options are there, such as Windows Explorer and Mac Finder, but you tend to think, with these, of the one area or folder to search (mind you both are capable of better), whereas you often face the dilemma of having no idea where the file is.   The GUIs can help here, but find, for myself, that I always jump to the command line for this, with Windows command line DIR C: [filespec] \/s or the Linux\/Unix commands for today&#8217;s tutorial &#8230; locate and find.<\/p>\n<p>So why confuse things with two choices?   Well, locate is fast, once set up, because it sets up its own &#8220;database&#8221; of previous knowledge, and so can pluck out whatever you want really fast from then on, once you use a &#8220;<a target=_blank title='Linux\/unix sudo command help from Computerhope ... thanks.' href='http:\/\/www.computerhope.com\/unix\/sudo.htm'>sudo<\/a>&#8221; command to set up locate for future use.<\/p>\n<p>The strength of the Linux\/Unix find command are its switches like &#8220;-type ?&#8221; to define what type of file to find, and the way you can use &#8220;exec&#8221; in a piped scenario, to add more functionality.<\/p>\n<p>So some words about the syntax (and for this, found <i>Linux<\/i> by Steve Oualline and Eric Foster-Johnson (pp 72-73) an excellent source) where you can hover or click for further information:<\/p>\n<ul>\n<script>function showt(ospan) {\n  ospan.style.backgroundColor = 'yellow';\n  alert(ospan.title);\n  ospan.style.backgroundColor = '';\n}<\/script><\/p>\n<li><span id=\"a1\" title=\"The locate command in Linux or unix.\" onclick=\"showt(this);\">locate<\/span> <span id=\"a2\" title=\"File name or file name fragment to look for.\" onclick=\"showt(this);\">gimp<\/span> <span id=\"a3\" title=\"Pipe operator in Linux or unix.\" onclick=\"showt(this);\">|<\/span> <span id=\"a4\" title=\"Pipe to more command for 13 lines at a time.\" onclick=\"showt(this);\">more -y 13<\/span><\/li>\n<li><span id=\"b1\" title=\"The find command in Linux or unix.\" onclick=\"showt(this);\">find<\/span> <span id=\"b2\" title=\"This is start directory of search.\" onclick=\"showt(this);\">\/<\/span> <span id=\"b3\" title=\"Find command searches for all files satisfying *gimp* filespec.\" onclick=\"showt(this);\">-name &#8220;*gimp*&#8221;<\/span> <span id=\"b4\" title=\"Filter search to files of type f (file) so that directores and special files are excluded.\" onclick=\"showt(this);\">-type f<\/span> <span id=\"b5\" title=\"What to do when file is found - print it to standard output..\" onclick=\"showt(this);\">-print<\/span> <span id=\"b6\" title=\"Ignore outputting any errors found performing this command.\" onclick=\"showt(this);\">2> \/dev\/null<\/span> <span id=\"by\" title=\"Pipe operator in Linux or unix.\" onclick=\"showt(this);\">| <\/span> <span id=\"b8\" title=\"Pipe to more command for 13 lines at a time.\" onclick=\"showt(this);\">more -y 13<\/span><\/li>\n<li><span id=\"c1\" title=\"The find command in Linux or unix.\" onclick=\"showt(this);\">find<\/span> <span id=\"c2\" title=\"This is start directory of search.\" onclick=\"showt(this);\">$HOME\/Documents<\/span> <span id=\"c3\" title=\"Find command searches for all files satisfying *.htm* filespec.\" onclick=\"showt(this);\">-name &#8220;*.htm*&#8221;<\/span> <span id=\"c4\" title=\"Filter search to files of type f (file) so that directores and special files are excluded.\" onclick=\"showt(this);\">-type f<\/span> <span id=\"c5\" title=\"Use to execute command on the file.\" onclick=\"showt(this);\">-exec<\/span> <span id=\"c6\" title=\"This is the command executed on each file where {} is replaced by the actual filename ( eg. fgrep body $HOME\/Documents\/index.html \/dev\/null ).\" onclick=\"showt(this);\">fgrep body {} \/dev\/null<\/span> <span id=\"c7\" title=\"End of command.\" onclick=\"showt(this);\"> ;<\/span><\/li>\n<\/ul>\n<p>So today we see some actions to find and\/or locate files\/directories with this <a target=_blank href=\"\/Linux\/locate\/LocateFind_Linux.jpg\" title=\"Click picture\">tutorial<\/a>.<\/p>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d7067' onclick='var dv=document.getElementById(\"d7067\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"http:\/\/www.rjmprogramming.com.au\/wordpress\/?tag=Linux\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d7067' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Linux (or Unix) servers have two really useful commands which help the user find files and\/or directories &#8230; locate and find. Doubt there are many people out there who have not mislaid a computer file at some time or other. &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/linux-find-andor-locate-filesdirectories-primer-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/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":[12,29,37],"tags":[435,707,715,885,1339],"class_list":["post-7067","post","type-post","status-publish","format-standard","hentry","category-elearning","category-operating-system","category-tutorials","tag-find","tag-linux","tag-locate","tag-operating-system-2","tag-unix"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/7067"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=7067"}],"version-history":[{"count":0,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/7067\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=7067"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=7067"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=7067"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}