bash 如何使 OS X 上的“查找”命令默认为当前目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/194725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I make the "find" Command on OS X default to the current directory?
提问by mxcl
I am a heavy command line user and use the findcommand extensively in my build system scripts. However on Mac OS X when I am not concentrating I often get output like this:
我是一个沉重的命令行用户,并且find在我的构建系统脚本中广泛使用该命令。然而,在 Mac OS X 上,当我不集中注意力时,我经常得到这样的输出:
$ find -name \*.plist
find: illegal option -- n
find: illegal option -- a
find: illegal option -- m
find: illegal option -- e
find: *.plist: No such file or directory
Basically, I forgot to add the little dot:
基本上,我忘了添加小点:
$ find . -name \*.plist
Because BSD findrequires the path and GNU finddoesn't (it assumes the current directory if you don't specify one). I use Linux, Mac OS X and Cygwin often all at the same time, so it's of great benefit to me to have all my tools behave the same. I tried writing a bash findfunction that added "./" if I forgot, but I failed. Thanks for your help. :)
因为 BSDfind需要路径而 GNUfind不需要(如果您不指定,它会假定当前目录)。我经常同时使用 Linux、Mac OS X 和 Cygwin,因此让我的所有工具的行为都相同对我来说非常有益。我尝试编写一个 bashfind函数,如果我忘记了,它会添加“./”,但我失败了。谢谢你的帮助。:)
采纳答案by Jonathan Leffler
If you can't discipline yourself to use find'correctly', then why not install GNU find(from findutils) in a directory on your PATH ahead of the system findcommand.
如果您不能自律以find“正确”使用,那么为什么不在系统命令之前在 PATH 上的目录中安装 GNU find(from findutils) find。
I used to have my own private variant of cpthat would copy files to the current directory if the last item in the list was not a directory. I kept that in my personal bindirectory for many years - but eventually removed it because I no longer used the functionality. (My 'cp.sh' was written in 1987 and edited twice, in 1990 and 1997, as part of changes to version control system notations. I think I removed it around 1998. The primary problem with the script is that cp file1 file2is ambiguous between copying a file over another and copying two files to the current directory.)
cp如果列表中的最后一项不是目录,我曾经拥有自己的私有变体,它将文件复制到当前目录。我将它保存在我的个人bin目录中多年 - 但最终将其删除,因为我不再使用该功能。(我的“cp.sh”写于 1987 年,并在 1990 年和 1997 年编辑了两次,作为版本控制系统符号更改的一部分。我想我是在 1998 年左右将其删除的。脚本的主要问题cp file1 file2是复制之间存在歧义一个文件覆盖另一个并将两个文件复制到当前目录。)
Consider writing your own wrapper to find:
考虑编写自己的包装器find:
#!/bin/sh
[ ! -d "" ] && set -- . "$@"
exec /usr/bin/find "$@"
The second line says "if argument 1 is not a directory, then adjust the command line arguments to include dot ahead of the rest of the command. That will be confusing if you ever type:
第二行表示“如果参数 1 不是目录,则调整命令行参数以在命令的其余部分之前包含点。如果您键入以下内容,那将会令人困惑:
~/bin/find /non-existent/directory -name '*.plist' -print
because the non-existent directory isn't a directory and the script will add dot to the command line -- the sort of reason that I stopped using my private cpcommand.
因为不存在的目录不是目录,脚本会在命令行中添加点——这就是我停止使用我的私有cp命令的原因。
回答by odinho - Velmont
Install GNU find instead.
安装 GNU find 代替。
$ brew install findutils
$ alias find=gfind
Yay, it works!
是的,它有效!
回答by tzot
If you must call it 'find', then you want:
如果您必须将其称为“查找”,那么您需要:
alias find=/usr/bin/find\ .
in your .profile or .bash_profile or …. Substitute the real path (if not /usr/bin/find) on your Mac OSX. Enter the full path to avoid cycles (bash normally would interpret alias find=findwithout issues, but better be sure).
在您的 .profile 或 .bash_profile 或 .... 替换 Mac OSX 上的真实路径(如果不是 /usr/bin/find)。输入完整路径以避免循环(bash 通常会解释alias find=find没有问题,但最好确定)。
But you better not name the alias find(findl, myfind etc), because it will become a habit and trouble for you if you try it on another system.
但是你最好不要命名别名find(findl、myfind 等),因为如果你在另一个系统上尝试它会成为你的习惯和麻烦。
回答by Owen
find ./ -name "*.plist"
edit: hmm, i may have misunderstood the question! if you were crazy, how about emulating it via a shell script? i routinely keep random utility scripts in ~/.bin, and that's the first thing in my PATH. if you had a similar setup perhaps you could do something like: (untested!)
编辑:嗯,我可能误解了这个问题!如果你疯了,那么通过 shell 脚本模拟它怎么样?我经常在 ~/.bin 中保存随机实用程序脚本,这是我的 PATH 中的第一件事。如果您有类似的设置,也许您可以执行以下操作:(未经测试!)
#!/bin/sh
# remapping find!
CMD=`echo | cut -c 1`
if [ $CMD = '-' ]
then
# pwd search
/usr/bin/find ./ $*
else
# regular find
/usr/bin/find $*
fi
回答by Greg Hewgill
I would suggest that if you're writing scripts (which are more likely to be migrated from one system to another sometime in the future) that you should try to use the more specific form of the command, that is specifying the "." instead of relying on a default. For the same reason, I might even suggest writing shscripts instead of relying on bashwhich might not be installed everywhere.
我建议,如果您正在编写脚本(将来某个时候更有可能从一个系统迁移到另一个系统),您应该尝试使用更具体的命令形式,即指定“。” 而不是依赖默认值。出于同样的原因,我什至建议编写sh脚本,而不是依赖于bash可能不会安装在任何地方的脚本。
回答by James Fassett
This is probably not what you want but how about: alias find="find ."
这可能不是您想要的,但是如何: alias find="find ."
or choose a new name (findlfor find local?)
或选择一个新名称(findl用于查找本地?)
回答by Pysis
You may want to run the commands found in this link: https://www.topbug.net/blog/2013/04/14/install-and-use-gnu-command-line-tools-in-mac-os-x/
您可能想要运行此链接中的命令:https: //www.topbug.net/blog/2013/04/14/install-and-use-gnu-command-line-tools-in-mac-os- X/
It is a bit outdated, for example I found I did not have to add many commands to my path at all.
它有点过时了,例如我发现我根本不需要在我的路径中添加很多命令。
This covers your problem by having your system use the Non-BSD find utility from the findutilspackage, while also installing other tools you may want as well.
这通过让您的系统使用findutils软件包中的非 BSD 查找实用程序来解决您的问题,同时还安装了您可能需要的其他工具。

