{"id":181,"date":"2021-06-30T10:10:14","date_gmt":"2021-06-30T10:10:14","guid":{"rendered":"https:\/\/tiruppurwebhosting.com\/blog\/?p=181"},"modified":"2021-08-10T09:24:04","modified_gmt":"2021-08-10T09:24:04","slug":"how-to-search-files-and-folders-via-ssh","status":"publish","type":"post","link":"https:\/\/tiruppurwebhosting.com\/blog\/how-to-search-files-and-folders-via-ssh\/","title":{"rendered":"How to search files and folders via SSH"},"content":{"rendered":"<h1>Search for Files and Folders via SSH<\/h1>\n<p>Here i will guide you how to search for files and folders via SSH.<\/p>\n<p>In some cases you would need to find the location of a given file or to search for a certain text in all files under a directory. SSH provides two different commands, which can be used to accomplish this.<\/p>\n<h2>To search a file location:<\/h2>\n<p>In order to search for a file location you can use the\u00a0<strong>find<\/strong>\u00a0command.\u00a0<strong>Find<\/strong>\u00a0is a very powerful tool and accepts various arguments allowing you to specify the exact search term (i.e search by name, by type or even by modified time).<\/p>\n<p>For example, to search for a file called\u00a0<em>myFile.txt<\/em>\u00a0under the current folder (and all subfolders), you would need to use the following command:<\/p>\n<pre>find . -name myFile.txt<\/pre>\n<p>If you are uncertain about the file name or would like to match a part of the name, you can use a wildcard pattern:<\/p>\n<pre>find . -name\"myFile*\"<\/pre>\n<p>If you would like to list only directories and leave all files out of the result:<\/p>\n<pre>find . -type d<\/pre>\n<p>Or if you want to filter only files modified in the last 2 days, you would need to use:<\/p>\n<pre>find . -mtime -2<\/pre>\n<figure id=\"attachment_1428\" aria-describedby=\"caption-attachment-1428\" style=\"width: 671px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-1428 size-full\" src=\"https:\/\/www.coimbatorewebhosting.com\/blog\/wp-content\/uploads\/2018\/08\/search-files.png\" alt=\"search file\" width=\"671\" height=\"662\" \/><figcaption id=\"caption-attachment-1428\" class=\"wp-caption-text\">search file<\/figcaption><\/figure>\n<p>You can also search for a given text in the files content as well. The command you should be using in this case is\u00a0<strong>grep<\/strong>. Grep is a very powerful tool and accepts various command line arguments. For a full list it is recommended to check the manual pages by typing\u00a0<strong>man grep<\/strong>.<\/p>\n<p>An example of using\u00a0<strong>grep<\/strong>\u00a0to find a certain text can be found below:<\/p>\n<pre>grep \"database\" configuration.php<\/pre>\n<p>The above command instructs\u00a0<strong>grep<\/strong>\u00a0to look for the string \u201c<em>database<\/em>\u201d in the\u00a0<em>configuration.php<\/em>\u00a0file and display the containing line. If you don\u2019t know which file contains the text, you can use:<\/p>\n<pre>grep -l \"database\" *<\/pre>\n<p>This will display the filenames containing the word &#8220;<em>database&#8221;<\/em>, but will not actually list the line containing it.<\/p>\n<p>Grep can also be used to filter the results from other commands. For example, the line below will only output configuration.php result:<\/p>\n<pre>ls -la | grep configuration.php<\/pre>\n<p>In some rare cases, find and grep may prove not useful. For example, to find a certain file in the whole<a href=\"https:\/\/www.squarebrothers.com\/dedicated-server-in-india\/\" target=\"_blank\" rel=\"noopener\"> server,<\/a> it would be best to use an alternative command &#8211;\u00a0<strong>whereis<\/strong>\u00a0or\u00a0<strong>which<\/strong>:<\/p>\n<pre>whereis perl<\/pre>\n<p>or<\/p>\n<pre>which perl<\/pre>\n<p>The execution of the above commands will locate the\u00a0<em>perl binary<\/em>\u00a0and display the full path(s) to it.<\/p>\n<p>And to know the basic or How to use the GREP commands in Linux <a href=\"https:\/\/tiruppurwebhosting.com\/blog\/2021\/07\/14\/use-the-grep-commands\/\">click here<\/a> to go.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Search for Files and Folders via SSH Here i will guide you how to search for files and folders via SSH. In some cases you would need to find the location of a given file or to search for a certain text in all files under a directory. SSH provides two different commands, which can<\/p>\n","protected":false},"author":1,"featured_media":183,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,1,9],"tags":[93,94,95],"class_list":["post-181","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-centos","category-home","category-linux","tag-configuration","tag-directories","tag-grep"],"_links":{"self":[{"href":"https:\/\/tiruppurwebhosting.com\/blog\/wp-json\/wp\/v2\/posts\/181","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tiruppurwebhosting.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tiruppurwebhosting.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tiruppurwebhosting.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tiruppurwebhosting.com\/blog\/wp-json\/wp\/v2\/comments?post=181"}],"version-history":[{"count":0,"href":"https:\/\/tiruppurwebhosting.com\/blog\/wp-json\/wp\/v2\/posts\/181\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tiruppurwebhosting.com\/blog\/wp-json\/wp\/v2\/media\/183"}],"wp:attachment":[{"href":"https:\/\/tiruppurwebhosting.com\/blog\/wp-json\/wp\/v2\/media?parent=181"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tiruppurwebhosting.com\/blog\/wp-json\/wp\/v2\/categories?post=181"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tiruppurwebhosting.com\/blog\/wp-json\/wp\/v2\/tags?post=181"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}