【问题标题】:Find a file (case insensitive) and get the real file name (case sensitive) back in a variable查找文件(不区分大小写)并在变量中获取真实文件名(区分大小写)
【发布时间】:2013-07-05 16:35:24
【问题描述】:

我知道我有一个文件。
我知道这个文件是 Hello.txt,或 HeLLo.txt,或 HELLo.txt,或其他大小写变体

所以我可以通过以下方式找到这个文件:

find . -iname hello.txt

它找到了。

如何将真实姓名(“HelLO.txt”)放入 linux 变量中?

【问题讨论】:

    标签: linux unix filenames


    【解决方案1】:
    var=`find . -iname hello.txt | head -n1 | sed 's/.*\///g'`
    

    应该做你想做的。

    【讨论】:

    • 谢谢。只是为其他读者记录: header -n1 获取第一个结果,并且 sed '...' 应用正则表达式,以便它仅捕获文件名而不是整个路径
    【解决方案2】:

    最简单的方法:

    var=`find . -iname hello.txt`
    

    请注意,这会将所有匹配的文件名分配给变量,因此如果您有多个变体(Hello HeLLO hello HELLO 等...),您将在 var 中获得所有变体。

    【讨论】:

    • 不会返回整个路径吗?
    猜你喜欢
    • 2012-02-03
    • 2019-06-16
    • 1970-01-01
    • 1970-01-01
    • 2012-03-24
    • 1970-01-01
    • 1970-01-01
    • 2014-03-31
    • 2016-11-24
    相关资源
    最近更新 更多