{"id":5693,"date":"2024-05-14T20:20:02","date_gmt":"2024-05-14T12:20:02","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=5693"},"modified":"2024-05-14T20:20:02","modified_gmt":"2024-05-14T12:20:02","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8%e4%bd%bf%e7%94%a8find%e5%91%bd%e4%bb%a4%e6%97%b6%e6%8e%92%e9%99%a4%e7%9b%ae%e5%bd%95%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/5693.html","title":{"rendered":"\u5982\u4f55\u5728\u4f7f\u7528find\u547d\u4ee4\u65f6\u6392\u9664\u76ee\u5f55\uff1f"},"content":{"rendered":"\n<p>=Start=<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u7f18\u7531\uff1a<\/h4>\n\n\n\n<p>\u7b80\u5355\u6574\u7406\u4e00\u4e0b\u7ed3\u8bba\uff0c\u65b9\u4fbf\u540e\u9762\u6709\u9700\u8981\u7684\u65f6\u5019\u53c2\u8003\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6b63\u6587\uff1a<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>question:<br>How do I exclude a specific directory when searching for *.js files using find?<br>\u5728\u4f7f\u7528 find \u641c\u7d22 *.js \u6587\u4ef6\u65f6\uff0c\u6211\u5982\u4f55\u6392\u9664\u7279\u5b9a\u76ee\u5f55?<\/p>\n\n\n\n<p><code>find . -name '*.js'<\/code><\/p>\n\n\n\n<p>answer(s):<\/p>\n\n\n\n<p><code>find -name \"*.js\" -not -path \".\/directory\/*\"<\/code> #\u8fd9\u4e2afind\u547d\u4ee4\u8fd8\u662f\u904d\u5386\u4e86\u4f60\u4e0d\u60f3\u904d\u5386\u7684\u76ee\u5f55\uff0c\u56e0\u6b64\u5982\u679c\u5e0c\u671b\u901f\u5ea6\u5feb\uff0c\u5c31\u662f\u7528 -prune \u9009\u9879<\/p>\n\n\n\n<p><code>find . -path .\/dir1 -prune -false -o -name \"*.txt\"<\/code> #\u6392\u96641\u4e2a\u76ee\u5f55<\/p>\n\n\n\n<p><code>find . -type d \\( -path .\/dir1 -o -path .\/dir2 -o -path .\/dir3 \\) -prune -false -o -name \"*.txt\"<\/code> #\u6392\u9664\u591a\u4e2a\u76ee\u5f55<\/p>\n\n\n\n<p><code>find . -not \\( -path .\/dir1 -prune \\) -name \"*.txt\"<\/code> #\u7b26\u5408\u9884\u671f\uff0c\u4e14\u5982\u679c\u4f60\u5173\u5fc3\u8fd0\u884c\u65f6\u95f4\uff0c\u5e76\u505a\u4efb\u4f55\u8fdc\u7a0b\u5bc6\u96c6\u7684\u4e8b\u60c5\uff0c\u4f60\u5e94\u8be5\u4f7f\u7528-prune\u3002<br><code>find . -not \\( -path .\/dir1 -prune \\) -not \\( -path .\/dir2 -prune \\) -name \"*.txt\"<\/code> #\u7b26\u5408\u9884\u671f\uff0c\u4e14\u5982\u679c\u4f60\u5173\u5fc3\u8fd0\u884c\u65f6\u95f4\uff0c\u5e76\u505a\u4efb\u4f55\u8fdc\u7a0b\u5bc6\u96c6\u7684\u4e8b\u60c5\uff0c\u4f60\u5e94\u8be5\u4f7f\u7528-prune\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<pre class=\"wp-block-code\"><code># \u6d4b\u8bd5\u7684\u6587\u4ef6\u76ee\u5f55\u7ed3\u6784\n$ find . -type f -iname \"*.txt\"\n.\/dir2\/a.txt\n.\/dir3\/a.txt\n.\/dir1\/a.txt\n.\/a.txt\n\n# \u8fd9\u662f GetFree \u7684\u7b54\u6848\n$ find . -type f -iname \"*.txt\" -not -path \".\/dir1\/*\" #\u7b26\u5408\u9884\u671f\uff0c\u8d77\u7801\u7ed3\u679c\u91cc\u6ca1\u6709\u5339\u914d\u7684\u9879\u76ee\u4e86\uff08\u4f46\u662f\u5b9e\u9645\u6548\u679c\u6700\u6162\uff0c\u5982\u679c\u76ee\u5f55\u4e0d\u5927\u6587\u4ef6\u4e0d\u591a\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\uff09\n.\/dir2\/a.txt\n.\/dir3\/a.txt\n.\/a.txt\n\n# \u8fd9\u662f f10bit \u7684\u7b54\u6848\n$ find . -type d \\( -path .\/dir1 -o -path .\/dir2 -o -path .\/dir3 \\) -prune -o -name \"*.txt\" #\u4e0d\u7b26\u5408\u9884\u671f\uff08\u4f46\u5b9e\u9645\u4e0a\u662f\u6ca1\u95ee\u9898\u7684\uff0c\u5b83\u53ea\u662f\u628a\u4e0d\u626b\u63cf\u7684\u76ee\u5f55\u5217\u51fa\u6765\u4e86\u800c\u5df2\uff09\n.\/dir2\n.\/dir3\n.\/dir1\n.\/a.txt\n\n# \u3010nice\uff0c\u63a8\u8350\u3011\u8fd9\u662f\u5728 f10bit \u7684\u7b54\u6848\u4e0b\u6839\u636e\u8bc4\u8bba\u5fae\u8c03\u800c\u6210\u7684\n$ find . -type d \\( -path .\/dir1 -o -path .\/dir2 -o -path .\/dir3 \\) -prune -false -o -name \"*.txt\" #\u7b26\u5408\u9884\u671f\uff0c\u5728\u3010-prune\u3011\u540e\u9762\u6dfb\u52a0\u4e0a\u3010-false\u3011\u5373\u53ef\n.\/a.txt\n\n# \u3010nice\uff0c\u63a8\u8350\u3011\u8fd9\u662f Daniel C. Sobral \u7684\u7b54\u6848\uff0c\u8fd0\u884c\u901f\u5ea6\u5feb\uff0c\u4e14\u53ef\u8bfb\u6027\u5f88\u9ad8\n$ find . -not \\( -path .\/dir1 -prune \\) -name \"*.txt\" #\u7b26\u5408\u9884\u671f\uff0c\u4e14\u5982\u679c\u4f60\u5173\u5fc3\u8fd0\u884c\u65f6\u95f4\uff0c\u5e76\u505a\u4efb\u4f55\u8fdc\u7a0b\u5bc6\u96c6\u7684\u4e8b\u60c5\uff0c\u4f60\u5e94\u8be5\u4f7f\u7528-prune\u3002\n.\/dir2\/a.txt\n.\/dir3\/a.txt\n.\/a.txt\n\n# you can also exclude multiple paths\n$ find . -not \\( -path .\/dir1 -prune \\) -not \\( -path .\/dir2 -prune \\) -name \"*.txt\" #\u7b26\u5408\u9884\u671f\uff0c\u4e14\u5982\u679c\u4f60\u5173\u5fc3\u8fd0\u884c\u65f6\u95f4\uff0c\u5e76\u505a\u4efb\u4f55\u8fdc\u7a0b\u5bc6\u96c6\u7684\u4e8b\u60c5\uff0c\u4f60\u5e94\u8be5\u4f7f\u7528-prune\u3002\n.\/dir3\/a.txt\n.\/a.txt<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<pre class=\"wp-block-code\"><code># Reinstate Monica \u7b54\u6848\u91cc\u7684\u6d4b\u8bd5\u811a\u672c\n\n#!\/bin\/bash\n\ndir='.performance_test'\n\nsetup() {\n  mkdir \"$dir\" || exit 1\n  mkdir -p \"$dir\/prune_me\/a\/b\/c\/d\/e\/f\/g\/h\/i\/j\/k\/l\/m\/n\/o\/p\/q\/r\/s\/t\/u\/w\/x\/y\/z\" \\\n    \"$dir\/other\"\n\n  find \"$dir\/prune_me\" -depth -type d -exec mkdir '{}'\/{A..Z} \\;\n  find \"$dir\/prune_me\" -type d -exec touch '{}'\/{1..1000} \\;\n  touch \"$dir\/other\/foo\"\n}\n\ncleanup() {\n  rm -rf \"$dir\"\n}\n\nstats() {\n  for file in \"$dir\"\/*; do\n    if &#91;&#91; -d \"$file\" ]]; then\n      count=$(find \"$file\" | wc -l)\n      printf \"%-30s %-10s\\n\" \"$file\" \"$count\"\n    fi\n  done\n}\n\nname1() {\n  find \"$dir\" -path \"$dir\/prune_me\" -prune -o -exec bash -c 'echo \"$0\"'  {} \\;\n}\n\nname2() {\n  find \"$dir\" -not \\( -path \"$dir\/prune_me\" -prune \\) -exec bash -c 'echo \"$0\"' {} \\;\n}\n\nname3() {\n  find \"$dir\" -not -path \"$dir\/prune_me*\" -exec bash -c 'echo \"$0\"' {} \\;\n}\n\nprintf \"Setting up test files...\\n\\n\"\nsetup\necho \"----------------------------------------------\"\necho \"# of files\/dirs in level one directories\"\nstats | sort -k 2 -n -r\necho \"----------------------------------------------\"\n\nprintf \"\\nRunning performance test...\\n\\n\"\n\necho \\&gt; find \\\"\"$dir\"\\\" -path \\\"\"$dir\/prune_me\"\\\" -prune -o -exec bash -c \\'echo \\\"\\$0\\\"\\'  {} \\\\\\;\nname1\ns=$(date +%s%N)\nname1_num=$(name1 | wc -l)\ne=$(date +%s%N)\nname1_perf=$((e-s))\nprintf \"  &#91;# of files] $name1_num &#91;Runtime(ns)] $name1_perf\\n\\n\"\n\necho \\&gt; find \\\"\"$dir\"\\\" -not \\\\\\( -path \\\"\"$dir\/prune_me\"\\\" -prune \\\\\\) -exec bash -c \\'echo \\\"\\$0\\\"\\' {} \\\\\\;\nname2\ns=$(date +%s%N)\nname2_num=$(name2 | wc -l)\ne=$(date +%s%N)\nname2_perf=$((e-s))\nprintf \"  &#91;# of files] $name2_num &#91;Runtime(ns)] $name2_perf\\n\\n\"\n\necho \\&gt; find \\\"\"$dir\"\\\" -not -path \\\"\"$dir\/prune_me*\"\\\" -exec bash -c \\'echo \\\"\\$0\\\"\\' {} \\\\\\;\nname3\ns=$(date +%s%N)\nname3_num=$(name3 | wc -l)\ne=$(date +%s%N)\nname3_perf=$((e-s))\nprintf \"  &#91;# of files] $name3_num &#91;Runtime(ns)] $name3_perf\\n\\n\"\n\necho \"Cleaning up test files...\"\ncleanup<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n\n\n\n<p>How do I exclude a directory when using <code>find<\/code>?<br><a href=\"https:\/\/stackoverflow.com\/questions\/4210042\/how-do-i-exclude-a-directory-when-using-find\">https:\/\/stackoverflow.com\/questions\/4210042\/how-do-i-exclude-a-directory-when-using-find<\/a><\/p>\n\n\n\n<p>Linux\u4e0b\u7528find\u547d\u4ee4\u4e00\u6b21\u67e5\u627e\u591a\u79cd\u7c7b\u578b\u7684\u6587\u4ef6<br><a href=\"https:\/\/ixyzero.com\/blog\/archives\/2510.html\">https:\/\/ixyzero.com\/blog\/archives\/2510.html<\/a><\/p>\n\n\n\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u7b80\u5355\u6574\u7406\u4e00\u4e0b\u7ed3\u8bba\uff0c\u65b9\u4fbf\u540e\u9762\u6709\u9700\u8981\u7684\u65f6\u5019\u53c2\u8003\u3002 \u6b63\u6587\uff1a \u53c2\u8003\u89e3\u7b54\uff1a question:H [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,11,7,12],"tags":[2274,89,30,19],"class_list":["post-5693","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-linux","category-programing","category-tools","tag-prune","tag-find","tag-linux","tag-tips"],"views":931,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5693","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=5693"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5693\/revisions"}],"predecessor-version":[{"id":5694,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5693\/revisions\/5694"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=5693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=5693"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=5693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}