有没有一种快速的方法可以知道我的
我问这个问题的原因是我有几个带有字符
$PATH
中可执行文件是否包含特定的字符串?例如,我想快速列出包含SRA
的可执行文件。我问这个问题的原因是我有几个带有字符
SRA
的脚本。问题是我总是忘记文件的起始字符(如果我记得,我使用tab键自动补全来查找)。find
及其各种选项:IFS=":" read -ra paths <<< "$PATH"
find "${paths[@]}" -type f -executable -name '*SRA*'
IFS=":" read -ra paths <<< "$PATH"
读取所有路径到一个数组中,将字段分隔符暂时设置为:
,就像在Setting IFS for a single statement中所看到的那样。-type f
查找文件。-executable
查找可执行文件。在OSX中,您可以使用-perm +111
替代 (source)。由于FreeBSD或OSX不支持-executable
选项,因此ghoti推荐使用-perm
选项:
find -perm -o=x,-g=x,-u=x
例如:
find ${PATH//:/ } -maxdepth 1 -executable -name '*SRA*'
如果您的$PATH(或其他有害字符)中包含空格,则可以使用@fedorqui答案中借用的<<<
技巧:
tr ":\n" "\\000" <<< "$PATH" | \
xargs -0r -I{} -n1 find {} -maxdepth 1 -executable -name '*SRA*'
IFS=:
的解决方案更加安全。 - ghotifind $(echo $PATH | tr : ' ') -name \*SRA\*
#!/usr/bin/env bash
function show_help()
{
ME=$(basename "$0")
IT=$(cat <<EOF
returns a list of files in the path that match a substring
usage: $ME SUBSTRING
e.g.
# Find all files in the path that have "git" in their name
$ME git
EOF
)
echo "$IT"
echo
exit
}
if [ -z "$1" ]
then
show_help
fi
if [ "$1" == "help" ] || [ "$1" == '?' ] || [ "$1" == "--help" ] || [ "$1" == "h" ]; then
show_help
fi
SUBSTRING="$1"
IFS=":" read -ra paths <<< "$PATH"
find "${paths[@]}" -type f -perm +111 -name "*$SUBSTRING*" 2>/dev/null | sort | uniq
find "${paths[@]}" -type f ...
呢? - ghoti-ls
或-exec
参数,-print
将是默认选项。不过需要提醒的是,在FreeBSD或OSX中,find
命令并不包含-executable
选项,该选项是GNU findutils才有的。但我相信你可以使用-perm +u=x,g=x,o=x
来达到同样的效果。 - ghotifind "${paths[@]}" -type f -executable -name *SRA*' 2>/dev/null | sort | uniq
。 - Brad Parks-executable
的POSIX可移植版本,就是使用-perm +111
,这是我在Mac上唯一能够正常工作的变体,来源于这里。 - Brad Parks