文章目录
- 1. Shell 通配符展开(Glob Expansion)
- 2. 有引号时的行为(推荐)
- 3. 无引号时的行为
- 4. 总结原因
- 5. 推荐实践
root@iZuf67xiyefycct0a9rdi3Z:~# find / -name *.jar
find: paths must precede expression: o2o.jar
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec|time] [path...] [expression]
root@iZuf67xiyefycct0a9rdi3Z:~# find / -name '*.jar'
/usr/share/java/gettext.jar
/usr/share/java/libintl.jar
/usr/share/ca-certificates-java/ca-certificates-java.jar
/usr/lib/jvm/java-8-openjdk-amd64/lib/tools.jar
/usr/lib/jvm/java-8-openjdk-amd64/lib/sa-jdi.jar
/usr/lib/jvm/java-8-openjdk-amd64/lib/jconsole.jar
/usr/lib/jvm/java-8-openjdk-amd64/lib/dt.jar
/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/charsets.jar
/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/icedtea-sound.jar
/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/zipfs.jar
/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/sunec.jar
/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/sunpkcs11.jar
/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/nashorn.jar
/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/jaccess.jar
/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/sunjce_provider.jar
/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/localedata.jar
/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/cldrdata.jar
/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/ext/dnsns.jar
/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/jce.jar
/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/jsse.jar
/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/resources.jar
/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/management-agent.jar
/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/rt.jar
/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/security/policy/unlimited/US_export_policy.jar
/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/security/policy/unlimited/local_policy.jar
/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/security/policy/limited/US_export_policy.jar
/usr/lib/jvm/java-8-openjdk-amd64/jre/lib/security/policy/limited/local_policy.jar
/root/o2o.jar
/root/api-0.0.1-SNAPSHOT.jar
/root/backups/api-0.0.1-SNAPSHOT-2005121808.jar
/root/backups/api-0.0.1-SNAPSHOT-2011111456.jar
/root/backups/o2o-2110271358.jar
/root/backups/api-0.0.1-SNAPSHOT-2003171037.jar
/root/backups/o2o-2207041636.jar
/root/backups/o2o-2106231520.jar
/root/backups/api-0.0.1-SNAPSHOT-2003131851.jar
/root/backups/o2o-2107051533.jar
/root/backups/o2o-2110271357.jar
/root/backups/api-0.0.1-SNAPSHOT-1912101123.jar
/root/backups/o2o-2106211559.jar
/root/backups/o2o-2106110854.jar
/root/backups/api-0.0.1-SNAPSHOT-2011111449.jar
/root/backups/api-0.0.1-SNAPSHOT-2009300856.jar
/root/backups/o2o-2109261013.jar
/root/backups/o2o-2101211406.jar
/root/backups/api-0.0.1-SNAPSHOT-1912161556.jar
/root/backups/api-0.0.1-SNAPSHOT-1911211056.jar
/root/backups/api-0.0.1-SNAPSHOT-1910311824.jar
/root/backups/o2o-2203071836.jar
/root/backups/api-0.0.1-SNAPSHOT-1910151722.jar
/root/backups/api-0.0.1-SNAPSHOT-1910141812.jar
/root/backups/api-0.0.1-SNAPSHOT-1910221142.jar
/root/backups/api-0.0.1-SNAPSHOT-2003251833.jar
/root/backups/api-0.0.1-SNAPSHOT-2003171241.jar
/root/backups/api-0.0.1-SNAPSHOT-1912271913.jar
/root/backups/o2o-2101261347.jar
/root/backups/api-0.0.1-SNAPSHOT-2003180826.jar
/root/backups/api-0.0.1-SNAPSHOT-1910141712.jar
/root/backups/api-0.0.1-SNAPSHOT-1910141735.jar
/root/backups/api-0.0.1-SNAPSHOT-2005131541.jar
/root/backups/o2o-2207071007.jar
/root/backups/o2o-2108121055.jar
/root/backups/api-0.0.1-SNAPSHOT-1910141717.jar
/root/backups/api-0.0.1-SNAPSHOT-2009300858.jar
/root/backups/o2o-2207041512.jar
/root/backups/api-0.0.1-SNAPSHOT-2003261317.jar
/root/backups/o2o-2309180346.jar
/root/backups/api-0.0.1-SNAPSHOT-2011111457.jar
/root/backups/api-0.0.1-SNAPSHOT-1910151715.jar
/root/backups/o2o-2101211432.jar
/root/backups/api-0.0.1-SNAPSHOT-2005131727.jar
/root/backups/api-0.0.1-SNAPSHOT-2006131922.jar
/root/backups/api-0.0.1-SNAPSHOT-1911221029.jar
/root/backups/api-0.0.1-SNAPSHOT-2003250858.jar
/root/backups/api-0.0.1-SNAPSHOT-2008181644.jar
/root/backups/o2o-2209211031.jar
/root/backups/api-0.0.1-SNAPSHOT-2005131540.jar
/root/backups/api-0.0.1-SNAPSHOT-1912271919.jar
/root/backups/api-0.0.1-SNAPSHOT-1912271629.jar
/root/backups/api-0.0.1-SNAPSHOT-1912040956.jar
/root/backups/o2o-2107061034.jar
/root/backups/o2o-2107271720.jar
/root/backups/api-0.0.1-SNAPSHOT-1912041413.jar
/root/backups/api-0.0.1-SNAPSHOT-1911201606.jar
/root/backups/api-0.0.1-SNAPSHOT-1912101143.jar
/root/backups/api-0.0.1-SNAPSHOT-1910141807.jar
/root/backups/o2o-2206161029.jar
/root/backups/o2o-2106211550.jar
/root/backups/api-0.0.1-SNAPSHOT-2002251723.jar
/root/backups/api-0.0.1-SNAPSHOT-2011121414.jar
/root/backups/api-0.0.1-SNAPSHOT-1911042213.jar
/root/backups/o2o-2111261253.jar
/root/backups/api-0.0.1-SNAPSHOT-1910221128.jar
/root/backups/api-0.0.1-SNAPSHOT-1910141914.jar
/root/backups/api-0.0.1-SNAPSHOT-1912300926.jar
/root/backups/api-0.0.1-SNAPSHOT-2003300911.jar
/root/backups/api-0.0.1-SNAPSHOT-1910151132.jar
/root/backups/api-0.0.1-SNAPSHOT-2005131556.jar
/root/backups/api-0.0.1-SNAPSHOT-1912271859.jar
/root/backups/api-0.0.1-SNAPSHOT-2005131609.jar
/root/backups/api-0.0.1-SNAPSHOT-1910151058.jar
/root/backups/api-0.0.1-SNAPSHOT-2003251742.jar
/root/backups/api-0.0.1-SNAPSHOT-1910141937.jar
/root/backups/api-0.0.1-SNAPSHOT-1910141821.jar
/root/backups/api-0.0.1-SNAPSHOT-1912040939.jar
/root/backups/api-0.0.1-SNAPSHOT-2004071550.jar
/root/backups/api-0.0.1-SNAPSHOT-2005131615.jar
/root/backups/api-0.0.1-SNAPSHOT-1912161610.jar
/root/backups/api-0.0.1-SNAPSHOT-1912271720.jar
/root/backups/o2o-2207051114.jar
/root/backups/api-0.0.1-SNAPSHOT-1912101122.jar
/root/backups/api-0.0.1-SNAPSHOT-2003251728.jar
/root/backups/api-0.0.1-SNAPSHOT-2005131754.jar
/root/backups/o2o-2107151440.jar
/root/backups/api-0.0.1-SNAPSHOT-1912040948.jar
/root/backups/api-0.0.1-SNAPSHOT-2003171027.jar
/root/backups/api-0.0.1-SNAPSHOT-1910211009.jar
/root/backups/api-0.0.1-SNAPSHOT-1912161611.jar
在使用 find / -name '*.jar'
时,是否需要加引号取决于 shell 的通配符展开行为。让我们详细分析一下为什么有时需要加引号,有时不需要:
1. Shell 通配符展开(Glob Expansion)
- 在执行
find
命令之前,shell 会先处理命令行中的通配符(如*
、?
)。 - 如果当前目录下存在与
*.jar
匹配的文件,shell 会将*.jar
展开为匹配的文件列表(如file1.jar file2.jar
),然后传递给find
命令。 - 如果没有匹配文件,shell 会将
*.jar
原样传递给find
。
2. 有引号时的行为(推荐)
- 命令:
find / -name '*.jar'
- 效果:引号阻止了 shell 对
*.jar
进行通配符展开,find
接收到的参数就是*.jar
,并按预期查找所有.jar
文件。 - 推荐原因:无论当前目录是否存在匹配的文件,
find
都会搜索文件名匹配*.jar
的文件,行为更稳定。
3. 无引号时的行为
- 命令:
find / -name *.jar
- 效果:
- 如果当前目录有匹配的文件(如
file1.jar
和file2.jar
),shell 会将*.jar
展开为file1.jar file2.jar
,变成:
此时find / -name file1.jar file2.jar
find
可能会报错或只查找第一个文件。 - 如果当前目录没有匹配的文件,
*.jar
原样传递给find
,效果等同于加引号,正常执行。
- 如果当前目录有匹配的文件(如
4. 总结原因
- 不加引号时:依赖于当前目录是否有匹配的文件:
- 有匹配文件时,shell 展开,行为可能不符合预期。
- 无匹配文件时,
find
正常执行。
- 加引号时:始终按预期查找
*.jar
文件,不受当前目录影响。
5. 推荐实践
始终给通配符加引号,例如:
find / -name '*.jar'
这样可以避免 shell 提前展开通配符,确保 find
按预期运行。