{"id":4232,"date":"2024-04-27T12:05:28","date_gmt":"2024-04-27T06:35:28","guid":{"rendered":"https:\/\/cthecosmos.com\/?p=4232"},"modified":"2024-04-27T12:05:28","modified_gmt":"2024-04-27T06:35:28","slug":"find-and-grep","status":"publish","type":"post","link":"https:\/\/cthecosmos.com\/?p=4232","title":{"rendered":"FIND AND GREP"},"content":{"rendered":"\n<p class=\"has-black-color has-white-background-color has-text-color has-background has-link-color wp-elements-282fd59d8ee006e3582ade2cb8c708b2\" style=\"font-size:17px;line-height:1.8\">For anyone who is working on Ubuntu\/ on a linux platform, they might have initially found it difficult for navigation along directories. In this post, we shall see a few of the commands and their syntax that would help you understand and make your navigation easy.<br><br>This is the base pattern we would be following:<br><br>\ud835\udc1f\ud835\udc22\ud835\udc27\ud835\udc1d \/\ud835\udc29\ud835\udc1a\ud835\udc2d\ud835\udc21\/\ud835\udc2d\ud835\udc28\/\ud835\udc2c\ud835\udc2d\ud835\udc1a\ud835\udc2b\ud835\udc2d -\ud835\udc2d\ud835\udc32\ud835\udc29\ud835\udc1e \ud835\udc1f -\ud835\udc1e\ud835\udc31\ud835\udc1e\ud835\udc1c \ud835\udc20\ud835\udc2b\ud835\udc1e\ud835\udc29 -\ud835\udc07 &#8216;\ud835\udc29\ud835\udc1a\ud835\udc2d\ud835\udc2d\ud835\udc1e\ud835\udc2b\ud835\udc27&#8217; {} \\;<br><br>&#8211; <em><strong>\/path\/to\/start<\/strong><\/em>: The directory where you want your search to begin.<br>&#8211; <em><strong>-type f<\/strong><\/em>: This restricts the search to files only.<br>&#8211; <em><strong>-exec<\/strong><\/em>: This allows you to run another command on the files that find locates.<br>&#8211; <strong><em>grep -H &#8216;pattern&#8217;<\/em><\/strong>: The grep command searches within the file for the specified pattern. The -H option makes grep print the filename along with the matching line.<br>&#8211; <strong><em>{}<\/em><\/strong>: This is a placeholder that represents the current file that find is processing.<br>&#8211; <strong><em>\\;<\/em><\/strong>: This ends the -exec command.<br><br><br>1)<strong> Search recursively but exclude certain directories:<\/strong><br><br><strong><em>find \/path\/to\/start -type d \\( -name &#8220;exclude_dir1&#8243; -o -name  exclude_dir2&#8221; \\) -prune -o -type f -print | xargs grep -H &#8216;pattern&#8217;<br><\/em><\/strong><br><br><strong>2) Search with case-insensitive matching using grep:<\/strong><br><br><strong><em>find \/path\/to\/start -type f -print | xargs grep -iH &#8216;pattern&#8217;<\/em><\/strong><br><br><br><br><strong>3) Search for multiple patterns using grep with the -e flag:<\/strong><br><br><strong><em>find \/path\/to\/start -type f -print | xargs grep -H -e &#8216;pattern1&#8217; -e &#8216;pattern2&#8217;<\/em><\/strong><br><br><br><strong>4) Find files that do NOT contain a certain pattern using grep -L:<\/strong><br><br><strong><em>find \/path\/to\/start -type f -print | xargs grep -L &#8216;pattern&#8217;<\/em><\/strong><br><br><br><br><strong>5) Search for a pattern in files modified in the last N days using find with -mtime:<\/strong><br><strong><br><em>find \/path\/to\/start -type f -mtime -N -print | xargs grep -H &#8216;pattern&#8217;<\/em><\/strong><br><br><br><br><strong>6) Using xargs for faster searching with grep in large file sets:<\/strong><br><br><strong><em>find \/path\/to\/start -type f -name &#8220;*.php&#8221; -print | xargs grep -H &#8216;database&#8217;<\/em><\/strong><br><br><br><br><strong>7) Search for files based on size and then grep within those files:<\/strong><br><br><strong><em>find \/path\/to\/start -type f -size +1M -print | xargs grep -H &#8216;pattern&#8217;<\/em><\/strong><br><br><br><br><strong>8) Search for a pattern within files accessed or modified recently:<\/strong><br><br><strong><em>find \/path\/to\/start -type f -atime -2 -print | xargs grep -H &#8216;pattern&#8217;<\/em><\/strong><br><strong><br><\/strong><br><br><strong>9) Find and grep within specific file types:<\/strong><br><br><strong><em>find \/path\/to\/start \\( -name &#8220;*.txt&#8221; -o -name &#8220;*.md&#8221; \\) -print | xargs grep -H &#8216;pattern&#8217;<br><\/em><\/strong><br><br><br><strong>10) Using grep with context to show lines before and after the match:<\/strong><br><br><strong><em>find \/path\/to\/start -type f -print | xargs grep -H -A3 -B3 &#8216;pattern&#8217;<\/em><\/strong><br><br><br><br><strong>11) Searching for a pattern in all but certain file types:<\/strong><br><br><strong><em>find \/path\/to\/start -type f ! -name &#8220;*.log&#8221; -print | xargs grep -H &#8216;pattern&#8217;<\/em><\/strong><br><br><br><br><strong>12) Search for a pattern within files, excluding certain patterns:<\/strong><br><strong><em>find \/path\/to\/start -type f -print | xargs grep -H &#8216;pattern&#8217; | grep -v &#8216;exclude_pattern&#8217;<br><\/em><\/strong><br><br><br><strong>13) Find files based on depth to avoid recursive searching and grep within those:<\/strong><br><strong><br><em>find \/path\/to\/start -maxdepth 1 -type f -print | xargs grep -H &#8216;pattern&#8217;<\/em><\/strong><br><br><br><br><strong>14) Using zgrep to search within compressed files:<\/strong><br><strong><em>find \/path\/to\/start -type f -name &#8216;*.gz&#8217; -print | xargs zgrep &#8216;pattern&#8217;<\/em><\/strong><br><br><br><br><strong>15) Using grep to show only the filenames without the matching content:<\/strong><br><br><strong><em>find \/path\/to\/start -type f -print | xargs grep -l &#8216;pattern&#8217;<\/em><\/strong><\/p>\n\n\n\n<p class=\"has-black-color has-white-background-color has-text-color has-background has-link-color wp-elements-3f23b0d0995cfe5e34db846de0a57349\">LinkedIn Post: <a href=\"https:\/\/www.linkedin.com\/posts\/t-yashwanth-naidu_earlycareer-embeddedsystems-learning-activity-7110845532901707776-u87N\/?utm_source=share&amp;utm_medium=member_desktop\">https:\/\/www.linkedin.com\/posts\/t-yashwanth-naidu_earlycareer-embeddedsystems-learning-activity-7110845532901707776-u87N\/?utm_source=share&amp;utm_medium=member_desktop<\/a><\/p>\n\n\n\n<p class=\"has-white-color has-text-color has-background has-link-color has-medium-font-size wp-elements-486e7799b35920eece0029316d5e98bd\" style=\"background:linear-gradient(135deg,rgb(35,23,11) 0%,rgb(254,45,45) 50%,rgb(107,0,62) 100%)\">~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br><strong>An Article by: <\/strong>Yashwanth Naidu Tikkisetty<br>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post provides essential commands for navigating directories on Ubuntu\/Linux, making directory navigation easier. The post includes commands for searching for specific patterns within files, filtering by file type, and excluding certain directories. Each command is accompanied by a brief explanation and example. This valuable resource can greatly enhance your Linux command line skills.<\/p>\n<a href=\"https:\/\/cthecosmos.com\/?p=4232\" class=\"more-link\">Read More <span class=\"screen-reader-text\">FIND AND GREP<\/span><\/a>","protected":false},"author":120055267,"featured_media":4233,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":false,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_wpas_customize_per_network":false},"categories":[28627,30181,35264203,769114260,5495,952411],"tags":[772321195,772321193,3723469,18790084,771202926,88204,771201861,1354040,34920936],"class_list":{"0":"post-4232","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","6":"hentry","7":"category-embedded","8":"category-embedded-systems","9":"category-linux-3","10":"category-linuxlearnings","11":"category-operating-system","12":"category-short-articles","13":"tag-c-programming","14":"tag-embedded-systems","15":"tag-find-command","16":"tag-grep-command","17":"tag-grep-find-linux-command-line","18":"tag-linux-fun","19":"tag-linuxlearning-2","20":"tag-short-article","21":"tag-ubuntu-2","23":"fallback-thumbnail"},"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/cthecosmos.com\/wp-content\/uploads\/2024\/04\/find_grep.png?fit=5016%2C2096&ssl=1","jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8CiEf-16g","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/posts\/4232","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/users\/120055267"}],"replies":[{"embeddable":true,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4232"}],"version-history":[{"count":7,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/posts\/4232\/revisions"}],"predecessor-version":[{"id":4240,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/posts\/4232\/revisions\/4240"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=\/wp\/v2\/media\/4233"}],"wp:attachment":[{"href":"https:\/\/cthecosmos.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cthecosmos.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}