{"id":178,"date":"2020-08-15T20:04:49","date_gmt":"2020-08-16T01:04:49","guid":{"rendered":"https:\/\/www.brezeale.com\/?p=178"},"modified":"2020-08-15T20:04:49","modified_gmt":"2020-08-16T01:04:49","slug":"unix-find-examples","status":"publish","type":"post","link":"https:\/\/www.brezeale.com\/?p=178","title":{"rendered":"Unix find Examples"},"content":{"rendered":"\n<p>I originally created and posted these online on March 8, 2000.<\/p>\n\n\n\n<ol>\n\t\t<li>To recursively remove all files with a given name starting at the current directory, e.g., files called <b class=\"example\">undo.Z<\/b>, use <\/li>\n\t\t<blockquote class=\"exambox\">\n \t\t  <table width=\"100%\" border=\"1\">\n\t\t\t<tr>\n\t  \t\t  <td bgcolor=\"#CCCCCC\"><font face=\"Courier New, Courier, mono\">find . -name undo.Z -exec rm {} \\;<\/font><\/td>\n\t\t\t<\/tr>\n  \t\t  <\/table>\n\t\t<\/blockquote>\n\t\t<li>To recursively move to the directory <b class=\"example\">~\/public_html\/cgi-bin\/<\/b> all         files belonging to user <b class=\"example\">brezeald<\/b>, use<\/li>\n\t\t<blockquote class=\"exambox\">\n \t\t  <table width=\"100%\" border=\"1\">\n\t\t\t<tr>\n\t  \t\t  <td bgcolor=\"#CCCCCC\"><font face=\"Courier New, Courier, mono\">find . -user brezeald -exec           mv {} ~\/public_html\/cgi-bin\/ \\;<\/font><\/td>\n\t\t\t<\/tr>\n  \t\t  <\/table>\n\t\t<\/blockquote>\n\t\t<li>To recursively find all files with the name <b class=\"example\">hosts<\/b> starting at        the current directory, use<\/li>\n\t\t<blockquote class=\"exambox\">\n \t\t  <table width=\"100%\" border=\"1\">\n\t\t\t<tr>\n\t  \t\t  <td bgcolor=\"#CCCCCC\"><font face=\"Courier New, Courier, mono\">find . -name hosts -print<\/font><\/td>\n\t\t\t<\/tr>\n  \t\t  <\/table>\n\t\t<\/blockquote>\n\t\t<li>To recursively find all files with the extension <b class=\"example\">.htm<\/b> or <b class=\"example\">.html<\/b>         starting at the current directory, use<\/li>\n\t\t<blockquote class=\"exambox\">\n \t\t  <table width=\"100%\" border=\"1\">\n\t\t\t<tr>\n\t  \t\t  <td bgcolor=\"#CCCCCC\"><font face=\"Courier New, Courier, mono\">find . -name &quot;*.htm*&quot;           -prin<\/font>t<\/td>\n\t\t\t<\/tr>\n  \t\t  <\/table>\n\t\t<\/blockquote>\n\t\t<li>To recursively find all files that don&#8217;t begin with a capital letter,         use<\/li>\n\t\t<blockquote class=\"exambox\">\n \t\t  <table width=\"100%\" border=\"1\">\n\t\t\t<tr>\n\t  \t\t  <td bgcolor=\"#CCCCCC\"><font face=\"Courier New, Courier, mono\">find . \\! -name &#8216;[A-Z]*&#8217; -print<\/font><\/td>\n\t\t\t<\/tr>\n  \t\t  <\/table>\n\t\t<\/blockquote>\n\t\t<li>Find all files, but trim out the relative location information<\/li>\n\t\t<blockquote>\n \t\t  <table width=\"100%\" border=\"1\">\n\t\t\t<tr>\n\t\t\t  <td bgcolor=\"#CCCCCC\"><p class=\"example\">\n\t\t\t\tfind . -exec basename {} \\;\n\t\t\t\t<\/p><\/td>\n\t\t\t<\/tr>\n  \t\t  <\/table>\n\t\t<\/blockquote>\n\t\t<li>To search for <span class=\"example\">the_word<\/span> in the current directory and all subdirectories        of the current directory, use<\/li>\n\t\t<blockquote class=\"exambox\">\n \t\t  <table width=\"100%\" border=\"1\">\n\t\t\t<tr>\n\t\t\t\t\n\t\t\t  <td bgcolor=\"#CCCCCC\"><font face=\"Courier New, Courier, mono\">\n\t\t\t\tfind . -type f -exec grep &#8216;the_word&#8217; {} \\;<\/font><\/td>\n\t\t\t<\/tr>\n  \t\t  <\/table>\n\t\t<\/blockquote>\n\t\t<li>To also print filenames when searching for <span class=\"example\">the_word<\/span> in the current directory and all subdirectories        of the current directory, use<\/li>\n\t\t<blockquote class=\"exambox\">\n \t\t  <table width=\"100%\" border=\"1\">\n\t\t\t<tr>\n\t\t\t\t\n\t\t\t  <td bgcolor=\"#CCCCCC\">\n\t\t\t\t<p><font face=\"Courier New, Courier, mono\">\n\t\t\t\tfind . -type f -exec grep &#8216;the_word&#8217; {} \\; -print<br>\n\t\t\t\t  <\/font><font face=\"Courier New, Courier, mono\">find . -type f -print | xargs grep the_word 2>\/dev\/null<\/font><\/p>\n\t\t\t  <\/td>\n\t\t\t<\/tr>\n  \t\t  <\/table>\n\t\t<\/blockquote>\n\t\t<li>To only see the file names, use the -l option for grep:<\/li>\n\t\t<blockquote class=\"exambox\">\n \t\t  <table width=\"100%\" border=\"1\">\n\t\t\t<tr>\n\t  \t\t  <td bgcolor=\"#CCCCCC\"><font face=\"Courier New, Courier, mono\">find . -type f -print | xargs grep -l the_word 2>\/dev\/null<\/font><\/td>\n\t\t\t<\/tr>\n  \t\t  <\/table>\n\t\t  <p>The &quot; -type f &quot; option for find indicates that the files should be plain, as opposed to links, directories, etc.<\/p>\n\t\t<\/blockquote>\n\t\t<li>To generate a list of all directories, use <\/li>\n\t\t<blockquote class=\"exambox\">\n \t\t  <table width=\"100%\" border=\"1\">\n\t\t\t<tr>\n\t  \t\t  <td bgcolor=\"#CCCCCC\"><font face=\"Courier New, Courier, mono\">find . -type d -local -ls -print           > \/tmp\/list.txt<\/font><\/td>\n\t\t\t<\/tr>\n  \t\t  <\/table>\n\t\t  <p>-type d &#8212; only directories <br>\n\t\t\t-local &#8212; only local<br>\n\t\t\t-ls &#8212; file statistics (date, size, etc.)<br>\n\t\t\t-print &#8212; display the results <\/p>\n\t\t<\/blockquote>\n\t\t<li>Using the GNU version of find, you can limit the depth of the search:<\/li>\n\t\t<blockquote class=\"exambox\">\n \t\t  <table width=\"100%\" border=\"1\">\n\t\t\t<tr>\n\t  \t\t  <td bgcolor=\"#CCCCCC\"><font face=\"Courier New, Courier, mono\">find . -type d -maxdepth 4           -print > \/tmp\/list.txt<\/font><\/td>\n\t\t\t<\/tr>\n  \t\t  <\/table>\n\t\t<\/blockquote>\n\t\t<li>To find files that are seven days old, use either of the following (<font face=\"Courier New, Courier, mono\">-mtime<\/font> \n        is the last modified time of the file):<\/li>\n\t\t<blockquote class=\"exambox\">\n \t\t  <table width=\"100%\" border=\"1\">\n\t\t\t<tr>\n\t  \t\t  <td bgcolor=\"#CCCCCC\"><font face=\"Courier New, Courier, mono\">find . -mtime 7 -print<br>\n\t\t            find . -mtime +6 -mtime -8 -print<\/font><\/td>\n\t\t\t<\/tr>\n  \t\t  <\/table>\n\t\t<\/blockquote>\n\t\t<li>To find files that have not been accessed in seven days, use<font face=\"Courier New, Courier, mono\"><br>\n\t\t            <\/font><\/li>\n\t\t<blockquote class=\"exambox\">\n \t\t  <table width=\"100%\" border=\"1\">\n\t\t\t<tr>\n\t  \t\t  <td bgcolor=\"#CCCCCC\"><font face=\"Courier New, Courier, mono\">find . -atime 7 -print<\/font><\/td>\n\t\t\t<\/tr>\n  \t\t  <\/table>\n\t\t<\/blockquote>\n\t\t<li>To find files or directories if only part of the name is known, e.g., if the directory name contains the word REMOVED, use<\/li>\n\t\t<blockquote class=\"exambox\">\n\t\t  <table width=\"100%\" border=\"1\">\n\t\t\t<tr>\n\t  \t\t  <td bgcolor=\"#CCCCCC\"><font face=\"Courier New, Courier, mono\">find . -name \\*REMOVED\\* -print<\/font><\/td>\n\t\t\t<\/tr>\n  \t\t  <\/table>\n\t\t<\/blockquote>\n\t\t<li>To set the group id bit on all directories, use<font face=\"Courier New, Courier, mono\"><br>\n          <\/font><\/li>\n\t\t<blockquote class=\"exambox\">\n \t\t  <table width=\"100%\" border=\"1\">\n\t\t\t<tr>\n\t  \t\t  <td bgcolor=\"#CCCCCC\"><font face=\"Courier New, Courier, mono\">sudo find . -type d -exec chmod           g+ws {} \\;<\/font><\/td>\n\t\t\t<\/tr>\n  \t\t  <\/table>\n\t\t<\/blockquote>\n\t\t<li> Try this one-liner to generate a list of the 10 largest files on the target file system (from <a href=\"http:\/\/www.unixreview.com\/documents\/s=8925\/ur0310e\/\">http:\/\/www.unixreview.com\/documents\/s=8925\/ur0310e\/<\/a>):<\/li>\n\t\t<blockquote class=\"exambox\">\n \t\t  <table width=\"100%\" border=\"1\">\n\t\t\t<tr>\n\t  \t\t  <td bgcolor=\"#CCCCCC\"><font face=\"Courier New, Courier, mono\">find \/somefilesys -follow -mount -type f -print | xargs ls -l |\\<br>\n\t\t   sort -r -n -k 5,5 | head -10<\/font><\/td>\n\t\t\t<\/tr>\n  \t\t  <\/table>\n\t\t<\/blockquote>\n\t\t<li>A version for files with spaces in the names<\/li>\n\t\t<blockquote class=\"exambox\">\n \t\t  <table width=\"100%\" border=\"1\">\n\t\t\t<tr>\n\t  \t\t  <td bgcolor=\"#CCCCCC\"><font face=\"Courier New, Courier, mono\">find \/somefilesys -follow -mount  -type f -exec find {} \\! -type l -print <br>\n          \\; | perl -ne &#8216;chomp; ($size) = (stat(&quot;$_&quot;))[7]; print &quot;$size  $_\\n&quot;;&#8217; <br>\n          | sort -nr +0 -1 | head -10 | cut -d&#8217; &#8216; -f2- | while read TheFile; do \n          <br>\n          ls -l &quot;$TheFile&quot; ; done | sort -nr +4 -5<\/font><\/td>\n\t\t\t<\/tr>\n  \t\t  <\/table>\n\t\t<\/blockquote>\n\t\t<li> Would you like to find file names that contain spaces? Try the following:<\/li>\n\t\t<blockquote class=\"exambox\">\n \t\t  <table width=\"100%\" border=\"1\">\n\t\t\t<tr>\n\t  \t\t  <td bgcolor=\"#CCCCCC\"><font face=\"Courier New, Courier, mono\">find . -type f -print | while read TheFileName; do echo &quot;${TheFileName}&quot; <br>\n          | awk &#8216;$NF &gt;= 2&#8217; ; done<\/font><\/td>\n\t\t\t<\/tr>\n  \t\t  <\/table>\n\t\t<\/blockquote>\n\t  <\/ol>\n","protected":false},"excerpt":{"rendered":"<p>I originally created and posted these online on March 8, 2000. To recursively remove all files with a given name starting at the current directory, e.g., files called undo.Z, use find . -name undo.Z -exec rm {} \\; To recursively move to the directory ~\/public_html\/cgi-bin\/ all files belonging to user brezeald, use find . -user brezeald -exec mv {} ~\/public_html\/cgi-bin\/ \\; To recursively find all files with the name hosts starting at the current directory, use find . -name hosts&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/www.brezeale.com\/?p=178\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[7],"tags":[],"class_list":["post-178","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/posts\/178","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=178"}],"version-history":[{"count":2,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/posts\/178\/revisions"}],"predecessor-version":[{"id":180,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=\/wp\/v2\/posts\/178\/revisions\/180"}],"wp:attachment":[{"href":"https:\/\/www.brezeale.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.brezeale.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}