LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1677|回复: 15

莫名其妙的 find 命令

[复制链接]
发表于 2008-3-16 10:36:09 | 显示全部楼层 |阅读模式
很奇怪。

find ./ -name *.gz 这个命令怎么也看不出有什么错,但就是不停地报错。而倒数第七行又不报错了。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
发表于 2008-3-16 12:21:44 | 显示全部楼层
你用的是bash吗?以前用fish时常出类似问题,不过find的-name选项有时确实有毛病,过几天又没事了...

root@un:~# find ./ -name *.gz
./.Trash/boot_from_hd/boot/initrd.gz
./initrd.gz
./Desktop/initrd.gz
root@un:~# find --version
GNU find 版本 4.2.28
已激活的功能: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION
回复 支持 反对

使用道具 举报

发表于 2008-3-16 16:12:49 | 显示全部楼层
find . -name "*.gz"
回复 支持 反对

使用道具 举报

发表于 2008-3-16 19:06:11 | 显示全部楼层
Bash自动补全的问题
回复 支持 反对

使用道具 举报

发表于 2008-3-16 19:14:33 | 显示全部楼层
通配符的展开实际上是bash的功能,所以你输了一个 *.gz ,bash就会先把它展开成当前文件夹下的gz文件列表,再作为参数传送给find执行。如果加上引号,就像fridaychen说的那样,就可以避免bash的展开功能。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-3-16 23:36:39 | 显示全部楼层
可是, 在 ~ 下不报错,在 /etc/X11 不报错,在/usr/bin不报错, 在 /var/log/cups 不报错,
为什么就是在 /var/log  下要报错?

输入内容绝对无误,我都是用 上箭头 输的,所以命令字符是完全相同。

......

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2008-3-16 23:45:16 | 显示全部楼层
增加一幅图, 证明这条命令是可以找到 .gz 文件的。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复 支持 反对

使用道具 举报

发表于 2008-3-17 01:27:15 | 显示全部楼层
楼主,别人给你的回答你好好看了没有
Post by chenxingchen;1827223
通配符的展开实际上是bash的功能,所以你输了一个 *.gz ,bash就会先把它展开成当前文件夹下的gz文件列表,再作为参数传送给find执行。如果加上引号,就像fridaychen说的那样,就可以避免bash的展开功能。
回复 支持 反对

使用道具 举报

发表于 2008-3-17 09:44:23 | 显示全部楼层
楼上别激动,bash在进行glob扩展时,如果没有找到可匹配的文件,就会放弃扩展, 把*.gz之类的东西传递给应用程序。

这就是为什么find . -name *.gz 有时行,有时不行

不过,这样的问题很容易自己判断解决,不论怎样弄linux就需要一点研究的精神
回复 支持 反对

使用道具 举报

发表于 2008-3-17 10:57:13 | 显示全部楼层
呵呵,应该就是楼上所说的原因,贴一段Bash的manpage吧,注意加粗的部分。
Pathname Expansion
       After  word  splitting,  unless  the -f option has been set, bash scans
       each word for the characters *, ?, and [.  If one of  these  characters
       appears,  then  the word is regarded as a pattern, and replaced with an
       alphabetically sorted list of file names matching the pattern.   If  no
       matching  file  names  are found, and the shell option nullglob is dis-
       abled, the word is left unchanged.
  If the nullglob option is set,  and
       no  matches  are  found,  the  word  is removed.  If the failglob shell
       option is set, and no matches are found, an error  message  is  printed
       and  the  command  is  not executed.  If the shell option nocaseglob is
       enabled, the match is performed without regard to the  case  of  alpha-
       betic  characters.   Note  that when using range expressions like [a-z]
       (see below), letters of the other case may be  included,  depending  on
       the  setting of LC_COLLATE.  When a pattern is used for pathname expan-
       sion, the character "."  at the start of a name or immediately  follow-
       ing a slash must be matched explicitly, unless the shell option dotglob
       is set.  When matching a pathname, the slash character must  always  be
       matched  explicitly.  In other cases, the "."  character is not treated
       specially.  See the description of shopt below under SHELL BUILTIN COM-
       MANDS for a description of the nocaseglob, nullglob, failglob, and dot-
       glob shell options.
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表