{"id":109,"date":"2009-03-10T12:58:24","date_gmt":"2009-03-10T16:58:24","guid":{"rendered":"http:\/\/www.dev-notes.com\/blog\/2009\/03\/10\/searching-for-file-name-or-file-content-in-linux\/"},"modified":"2009-03-10T12:58:24","modified_gmt":"2009-03-10T16:58:24","slug":"searching-for-file-name-or-file-content-in-linux","status":"publish","type":"post","link":"https:\/\/www.dev-notes.com\/blog\/2009\/03\/10\/searching-for-file-name-or-file-content-in-linux\/","title":{"rendered":"Searching for file name or file content in Linux"},"content":{"rendered":"<p>To search for a file by its name, follow the example below.<\/p>\n<pre class=\"code\">\n[username@machine ~]$ find \/ -name 'myLostFile*.txt'\n\n\/home\/username\/foldername\/myLostFile_Feb2009.txt\n\/home\/username\/foldername\/myLostFile_Mar2009.txt\n\n<\/pre>\n<p>Note that &#8220;find&#8221; is the command we wish to run.  The &#8220;\/&#8221; denotes where to start looking.  We began looking from the root &#8220;\/&#8221; here; if you know a more specific path where the file is, you can narrow it down by providing a path, such as &#8220;\/home\/username\/&#8221; or &#8220;.&#8221;.  The &#8220;-name&#8221; specifies we are searching by file name.<\/p>\n<p>By adding &#8220;-ls&#8221; at the end, we can return more details on the matched files.  Example below:<\/p>\n<pre class=\"code\">\n[username@machine ~]$ find . -name 'myLostFile*.txt' -ls\n\n13795398 2310924 -rw-r--r--   1 username  username 2364071936 Feb 19 14:08 .\/myLostFile_Feb2009.txt\n13795396 59012 -rw-r--r--   1 username  username 60362752 Mar 10 08:29 .\/myLostFile_Mar2009.txt\n<\/pre>\n<p>At times, we may need to search through the content of the files as well.  To do so, follow the example below:<\/p>\n<pre class=\"code\">\n[username@machine ~]$ find . -type f -exec grep -H \"dev-notes.com\" {} ;\n\n.\/myLostFile_Mar2009.txt:To do - write an article on Linux find command for dev-notes.com\n\n<\/pre>\n<p>We will get the file path and name in the first portion (before the colon symbol) of each output line.  The particular line of that matched our search criteria will be displayed in the second portion.  If multiple lines within the same file matches and\/or if matches are found across multiple files, more than one output line will be displayed.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The find command in Linux can be used to locate a file by its name or its content.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28],"tags":[],"class_list":["post-109","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/posts\/109","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/comments?post=109"}],"version-history":[{"count":0,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/posts\/109\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/media?parent=109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/categories?post=109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/tags?post=109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}