我怎样才能“找到"?OS X 上的命令默认到当前目录? [英] How can I make the "find" Command on OS X default to the current directory?
问题描述
我是一个重度命令行用户,在我的构建系统脚本中广泛使用 find
命令.但是在 Mac OS X 上,当我不专心时,我经常会得到这样的输出:
I am a heavy command line user and use the find
command extensively in my build system scripts. However on Mac OS X when I am not concentrating I often get output like this:
$ 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
因为 BSD find
需要路径,而 GNU find
不需要(如果您不指定,它将假定当前目录).我经常同时使用 Linux、Mac OS X 和 Cygwin,所以让我的所有工具的行为都相同对我来说是非常有益的.我尝试编写一个 bash find
函数,如果我忘记了,它会添加./",但我失败了.谢谢你的帮助.:)
Because BSD find
requires the path and GNU find
doesn'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 find
function that added "./" if I forgot, but I failed. Thanks for your help. :)
推荐答案
如果你不能训练自己正确"使用 find
,那么为什么不安装 GNU find
(来自 findutils
)在系统 find
命令之前的 PATH 目录中.
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 find
command.
我曾经有我自己的 cp
私有变体,如果列表中的最后一项不是目录,它会将文件复制到当前目录.我将它保存在我的个人 bin
目录中多年 - 但最终将其删除,因为我不再使用该功能.(我的 'cp.sh' 写于 1987 年,并在 1990 年和 1997 年进行了两次编辑,作为版本控制系统符号更改的一部分.我想我在 1998 年左右删除了它.脚本的主要问题是 cpfile1 file2
在将一个文件复制到另一个文件和将两个文件复制到当前目录之间存在歧义.)
I used to have my own private variant of cp
that would copy files to the current directory if the last item in the list was not a directory. I kept that in my personal bin
directory 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 file2
is ambiguous between copying a file over another and copying two files to the current directory.)
考虑编写自己的包装器来find
:
Consider writing your own wrapper to find
:
#!/bin/sh
[ ! -d "$1" ] && set -- . "$@"
exec /usr/bin/find "$@"
第二行说如果参数 1 不是目录,则调整命令行参数以在命令的其余部分之前包含点.如果您键入会混淆:
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:
~/bin/find /non-existent/directory -name '*.plist' -print
因为不存在的目录不是目录,并且脚本会在命令行中添加点——这就是我停止使用我的私有 cp
命令的原因.
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 cp
command.
这篇关于我怎样才能“找到"?OS X 上的命令默认到当前目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!