使用 ?与ls通配符 [英] Using ? wildcard with ls
问题描述
我正在尝试使用?通配符仅显示1个字符文件,而?.*显示1个带扩展名的字符文件.
I'm trying to use the ? wildcard to display only 1 character files, and ?.* to display 1 character files with extensions.
有效方法:
cd/mydir
ls??.*
我正在尝试在shell脚本中使用它,因此我不能使用"cd"
I'm trying to use this in a shell script so therefor i cant use "cd"
我要开始工作的地方
ls ? ?.* /mydir
它给了我输出:
ls: cannot access ?.*: No such file or directory
我也尝试过:
ls /mydir ? ?.*
这给了我与以前完全相同的输出.
which gives me the exact same output as before.
推荐答案
编写 ls时??.*/mydir
,您尝试显示与三种不同模式匹配的文件:?
,?.*
和/mydir
.您只想匹配/mydir/?
和/mydir/?.*
,因此此命令: ls/mydir/?/mydir/?.*
.
When you write ls ? ?.* /mydir
, you're trying to display the files matching three distincts patterns: ?
, ?.*
, and /mydir
. You want to match only /mydir/?
and /mydir/?.*
, hence this command: ls /mydir/? /mydir/?.*
.
虽然这是对初始问题的正确答案(列出/mydir/?
和/mydir/?.*
),但OP希望这样做解析输出并获取文件计数.请参阅@gniourf_gniourf的答案,这是一种更好的方法.
while this is a correct answer to the initial question (listing /mydir/?
and /mydir/?.*
), OP wanted to do this to parse the output and get the file count. See @gniourf_gniourf's answer, which is a much better way to do this.
这篇关于使用 ?与ls通配符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!