Why doesn't **find** find anything?

你说的曾经没有我的故事 提交于 2019-12-05 10:46:32

问题


I'm looking for shell scripts files installed on my system, but find doesn't work:

$ find /usr -name *.sh

But I know there are a ton of scripts out there. For instance:

$ ls /usr/local/lib/*.sh
/usr/local/lib/tclConfig.sh  
/usr/local/lib/tkConfig.sh

Why doesn't find work?


回答1:


Try quoting the wildcard:

$ find /usr -name \*.sh

or:

$ find /usr -name '*.sh'

If you happen to have a file that matches *.sh in the current working directory, the wildcard will be expanded before find sees it. If you happen to have a file named tkConfig.sh in your working directory, the find command would expand to:

$ find /usr -name tkConfig.sh

which would only find files named tkConfig.sh. If you had more than one file that matches *.sh, you'd get a syntax error from find:

$ cd /usr/local/lib
$ find /usr -name *.sh
find: bad option tkConfig.sh
find: path-list predicate-list

Again, the reason is that the wildcard expands to both files:

$ find /usr -name tclConfig.sh tkConfig.sh

Quoting the wildcard prevents it from being prematurely expanded.

Another possibility is that /usr or one of its subdirectories is a symlink. find doesn't normally follow links, so you might need the -follow option:

$ find /usr -follow -name '*.sh'



回答2:


On some systems (Solaris, for example), there's no default action, so you need to add the -print command.

find /usr -name '*.foo' -print



回答3:


For finding files on your disks, lean to use "locate" instead that is instantaneous (looks into a daily built index) you example would be:

locate '/usr*.sh'


来源:https://stackoverflow.com/questions/18836/why-doesnt-find-find-anything

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!