Bash shell...查找命令...带有通配符的名称...别名或函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19352820/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 08:17:19  来源:igfitidea点击:

Bash shell...find command...names with wildcards...alias or function

bashfunctionaliaswildcard

提问by Rudi

I've long used the find command for finding files and directories in the current directory and all subdirectories that match a pattern:

我长期以来一直使用 find 命令在当前目录和与模式匹配的所有子目录中查找文件和目录:

find . -name "*.txt" -print
find . -name "Bill*" -print

But what I'd really like is an alias or function that will properly pass the wildcard. (I used to be able to do this in csh, but now I'm using bash.) If an alias or function named "fn" were set properly, I could save some time by just typing:

但我真正想要的是可以正确传递通配符的别名或函数。(我以前可以在 csh 中执行此操作,但现在我使用 bash。)如果正确设置了名为“fn”的别名或函数,我只需键入以下内容即可节省一些时间:

fn "*.txt"
fn "Bill*"

Ideally, I'd like to lose the quotation marks too, but I'm guessing that might not be possible because the shell will expand them before calling "fn".

理想情况下,我也想丢失引号,但我猜这可能是不可能的,因为 shell 会在调用“fn”之前扩展它们。

Any advice would be greatly appreciated and will postpone carpal tunnel syndrome.... :)

任何建议将不胜感激,并将推迟腕管综合症...... :)

SOLVED: After the discussion below, I put this in my .bashrc file:

解决:在下面的讨论之后,我把它放在我的 .bashrc 文件中:

fn () {
  find . -name "" -print
}

Note the quotes around the argument: "$1". This can then be called with more quotes around the filename expression:

注意参数周围的引号:“$1”。然后可以在文件名表达式周围使用更多引号来调用它:

fn "*.txt"

EDIT: must have spaces between the function name and the parentheses, so fn () { ... [works] fn() { ... [doesn't work]

编辑:函数名和括号之间必须有空格,所以 fn () { ... [有效] fn() { ... [不起作用]

回答by Ignacio Vazquez-Abrams

Unfortunately the wildcards must be quoted or the shell will expand them if possible.

不幸的是,必须引用通配符,否则 shell 会在可能的情况下扩展它们。

Fortunately there are multiple ways of quoting them.

幸运的是,有多种引用它们的方法。

fn '*.txt'
fn Bill\*

回答by Shicheng Guo

find ./ -name "*pbs.e*"  -type f -size +10c