【问题标题】:unix list files with a full stop in nameunix 列出名称中带有句号的文件
【发布时间】:2013-07-22 10:57:33
【问题描述】:

我有4个文件:命名约定如下:

hello.account.sub1.001  
hello.account.sub2.001  
hello.account.sub3.001  
hello.account.sub4.001 

如果我使用ls -l hello.account*001搜索文件,没有问题。

但是,使用ls -l hello.account.*.001时存在问题

系统抱怨No such file or directory

我假设系统认为 hello.account.*.001 是单个文件。

这让我很感兴趣,所以我问:如果将句号指定为搜索条件,您如何搜索文件?

非常感谢

【问题讨论】:

  • The system complains that No such file or directory - 不,这对我有用。您使用的是哪个外壳?
  • 应该仍然有效 - 文件名通配是由 shell 完成的,除非模式被转义,所以你仍然应该看到所有四个文件。如果你这样做会有所不同ls -l "hello.account.*.001"
  • 因为我在学校的unix系统中遇到了这个问题,所以目前无法测试。我想我可以在文件名中添加引号。
  • 无论如何,谢谢您的热情回复!

标签: unix ls


【解决方案1】:

Filename globbingshell 在实际执行命令之前完成(例如您的情况下的 ls)。以下作品:

$ ksh --version
  version         sh (AT&T Research) 93t+ 2010-06-21

$ ls -l hello.account.*.001
-rw-r--r-- 1 andreas users 0 Jul 22 03:59 hello.account.sub1.001
-rw-r--r-- 1 andreas users 0 Jul 22 03:59 hello.account.sub2.001
-rw-r--r-- 1 andreas users 0 Jul 22 03:59 hello.account.sub3.001
-rw-r--r-- 1 andreas users 0 Jul 22 03:59 hello.account.sub4.001

而以下不是:

$ ls -l "hello.account.*.001"
ls: hello.account.*.001: No such file or directory

原因是在第一种情况下,shell 在执行 ls 之前扩展了文件名模式 - ls 然后会看到四个不同的文件名作为参数传递。

在第二种情况下,由于文件名模式用双引号转义,ls 将模式本身传递(ls 不会进一步扩展 - 它会查找名称为 hello.account.*.001 的文件)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-06
    • 1970-01-01
    • 1970-01-01
    • 2012-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多