Bash shell ...使用通配符...别名或函数查找命令...名称

Rud*_*udi 10 bash alias function wildcard

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

find . -name "*.txt" -print
find . -name "Bill*" -print
Run Code Online (Sandbox Code Playgroud)

但我真正喜欢的是一个能够正确传递通配符的别名或函数.(我曾经能够在csh中执行此操作,但现在我正在使用bash.)如果正确设置了名为"fn"的别名或函数,我只需键入以下内容即可节省一些时间:

fn "*.txt"
fn "Bill*"
Run Code Online (Sandbox Code Playgroud)

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

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

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

fn () {
  find . -name "$1" -print
}
Run Code Online (Sandbox Code Playgroud)

请注意参数周围的引号:"$ 1".然后可以使用围绕文件名表达式的更多引号来调用它:

fn "*.txt"
Run Code Online (Sandbox Code Playgroud)

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

Ign*_*ams 13

不幸的是,必须引用通配符,否则shell会尽可能扩展它们.

幸运的是,有多种方式可以引用它们.

fn '*.txt'
fn Bill\*
Run Code Online (Sandbox Code Playgroud)


Shi*_*Guo 7

find ./ -name "*pbs.e*"  -type f -size +10c
Run Code Online (Sandbox Code Playgroud)