我想编写一个 shell 脚本,将文件名与我在运行时在命令行中输入的给定字符串匹配。我希望能够匹配文件名中的模式。就像字符串是“questi”并且文件夹包含“question1.c”、“question2.c”、“questions.doc”一样,这些应该显示为答案。
问问题
812 次
2 回答
1
该脚本可以很简单,如下所示:
$!/bin/bash
shopt -s nullglob # To return nothing if there is no match.
echo *$1*
然后将其称为script.sh questi
.
于 2013-03-05T11:07:28.547 回答
1
这可以使用find
:
find /path/to/directory -type f -iname "*questi*"
该选项-type f
导致仅返回文件并-iname
在 glob 上进行不区分大小写的匹配*questi*
,因此应返回 'question1.txt'、'five_questions.txt' 等。
如果您希望可以将其放入 shell 脚本中,如下所示:
#!/bin/sh
find $1 -type f -iname "*$2*"
并称它为:filefind.sh /path/to/directory questi
于 2013-03-05T11:10:02.420 回答