【问题标题】:Get extensions inside directory bash获取目录 bash 中的扩展
【发布时间】:2019-04-14 13:34:49
【问题描述】:

我试图获取作为 $1 参数传递的目录中的所有扩展。 问题是我得到的扩展不在 1 美元之内。我使用 cd 命令进入第一行的目录,但它似乎不起作用。

cd $1

 find . -type f | perl -ne 'print $1 if m/\.([^.\/]+)$/' | sort -u > $1extensions.txt

【问题讨论】:

  • "extension" 到底是什么意思?为避免使用find 进入子目录,您还可以使用maxdepth 选项,例如:find . -maxdepth 1 -type f
  • cd 是否与find 在同一个shell 中执行?请为$1 提供一个最小目录结构和值,以证明该问题。

标签: bash perl directory


【解决方案1】:

你可以试试下面的 Perl 命令:

perl -MList::Util=uniq -sE '
   chdir $dir; say for uniq map { /\.([^.\/]+)$/ ? $1 : () } grep -f, <*>;
' -- -dir="$1" > "$1"-extensions.txt

【讨论】:

    【解决方案2】:

    你显然没有说些什么,因为find 中没有会列出指定目录之外的文件的错误,而且你的 Perl 程序中也没有会发明扩展的错误。

    $ find
    .
    ./foo
    ./foo/a.good
    ./a.bad
    ./script
    
    $ cat ./script
    #!/bin/bash
    cd $1
    find . -type f | perl -ne 'print $1 if m/\.([^.\/]+)$/' | sort -u > $1extensions.txt
    
    $ ./script foo
    
    $ cat foo/fooextensions.txt
    good
    txt        <-- From the output file you created inside the directory in which you search.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-22
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 2021-09-18
      • 2017-10-28
      • 1970-01-01
      • 2012-01-23
      相关资源
      最近更新 更多