【问题标题】:Find all files ending in extension in bash在bash中查找所有以扩展名结尾的文件
【发布时间】:2018-09-06 23:39:02
【问题描述】:

编写一个接受两个参数的 bash 脚本:目录和扩展名。 该脚本必须将目录中所有文件的路径打印到标准输出(并且, 递归地,它的子目录),名称以扩展名结尾。

到目前为止我有:

find {directory} -type f -name ' *.extension *

当我给它 2 个参数时,它似乎什么也没找到。

【问题讨论】:

  • 您的报价无与伦比。最后的* 是干什么用的?

标签: bash


【解决方案1】:

这个脚本应该可以解决问题:

#!/bin/bash

find "$1" -type f -name "*.$2"

运行脚本的示例:

/bin/bash script.sh /srv/directory css
/bin/bash script.sh '/srv/directory with space' css

【讨论】:

    【解决方案2】:

    在这个答案中,我只是对 Viktor 的代码做一些解释。

    这个解释可能会帮助那些正在学习bash的人。在我发布“空间问题”部分之后以及在我完成编写并保存此解释之前,维克多更新了他的答案。这就是我所希望的。

    非常简洁,有很好的解释。原来的代码是:

    #!/bin/bash
    #@filename: script.sh
    
    find $1 -type f -name "*.$2"
    

    听起来这个问题来自某种作业,我想这个原始代码可能满足作业的期望。它可能也适用于您将编写的大多数程序,如果问题不是来自作业,这一点尤其重要。

    我经常在 Windows 或 Cygwin 上开发,所以我遇到了带有空格的路径和文件名。如果我在 Cygwin 上使用 @Viktor 的代码(请注意,当我在 linux 上使用 Viktor 的代码时会发生相同的行为)

    $ pwd
    /home/me
    
    #Write the program
    $ vim script.sh
    
    #view the program
    $ cat script.sh
    #!/bin/bash
    
    find $1 -type f -name "*.$2"
    
    
    #Create a path with spaces in one of the directory names
    #Name it after Viktor, for the great answer
    CORP+dblack@CAP-D-ENG-INT3 ~
    $ mkdir -p viktor/dir\ with\ spaces/directory
    
    #put some css files in that directory
    $ touch viktor/dir\ with\ spaces/directory/a.css
    
    $ touch viktor/dir\ with\ spaces/directory/b.css
    
    $ touch viktor/dir\ with\ spaces/directory/c.css
    
    # run Viktor's code, using the well-written instructions
    $ /bin/bash ./script.sh 'victor/dir with spaces/directory' css
    find: ‘viktor/dir’: No such file or directory
    find: ‘spaces/directory’: No such file or directory
    

    请注意,我使用了./script.sh 而不是普通的script.sh,因为我没有将script.sh 设置为可执行。如果我第一次运行,我可以像 Viktor 显示的那样运行它

    $ chmod +x script.sh
    

    问题来自bash shell 和find 命令处理空格的方式。将第一个参数简单地作为$1 给出,空格不会被以特殊方式处理。这可能不是描述事物的最佳方式。也许解释这个概念的更好方法是表明bash 将分配以下内容。

    $0='script.sh'
    $1='viktor/dir with spaces/directory'
    $2='css'
    

    即使我们告诉bash我们的目录有空格也会发生这种情况

    $ /bin/bash script.sh viktor/dir\ with\ spaces/directory css
    find: ‘viktor/dir’: No such file or directory
    find: ‘spaces/directory’: No such file or directory
    

    这可以通过添加行来查看,echo "\$0 is $0"、echo "\$1 is $1"、echo "\$2 is $2",如果您愿意,还可以添加具有$(insert_number_here_but_no_parentheses) 的其他行。

    那么,我们该如何解决这个问题呢?让我们看看find的手册页

    $ man find
    
    # (press 'q' to get out of the display of the man page)
    

    好吧,那有点吓人。

    让我们把原来的概要行删减到

    find  [starting-point...] [expression]
    

    DESCRIPTON 在其“起点”解释中的意思是 [starting-point...] 是一个目录(或多个目录),您将从该目录开始搜索。现在,从表面上看,“表达式”是限制搜索的东西——在你的情况下,你只查找文件,然后通过指定它们具有特定的文件扩展名来进一步过滤结果。

    让我们回到find $1 -type f -name "*.$2" 命令。这里要知道的重点是这里显示的命令会被展开为

    find viktor/dir with spaces/directory -type f -name "*.css"
    

    “空间问题”是find和shell会解释如下

    find victor/dir with spaces/directory -type f -name "*.css"
     |     \         |      \             `--------------------'
     |      \        |       \              /
     |       |       |        \       Another '[expression]' (2)
     |       |       |         -- Another '[starting-point]'
     |       |        -- '[expression]' (1)
     |        -- '[starting-point]' (supposedly a directory)
      -- Command name
    

    请注意,我们的 '[expression]' (1) 和 '[expression]' (2) 不会影响发生的情况,因为这两个 '[starting-place]' 路径失败。

    现在 Viktor 的代码中显示的内容

    #!/bin/bash
    #@filename viktor_script.sh
    
    find "$1" -type f -name "*.$2"
    

    导致

    find "viktor/dir with spaces/directory" -type f -name "*.css"
    `--' `--------------------------------' `-------------------'
     |               |                             /
     |               |             valid '[expression]' to define
     |               |             a "filter" for files outputted
     |               |
     |                -- '[starting-point]' (a real directory)
     |                    bash knows to include the spaces because
      -- command name                    of the quotation marks
    

    让我们运行它,首先确保它是可执行的

    $ chmod +x viktor_script.sh
    
    $ /bin/bash ./viktor_script.sh css
    
    $ /bin/bash viktor_script.sh 'viktor/dir with spaces/directory' css
    viktor/dir with spaces/directory/a.css
    viktor/dir with spaces/directory/b.css
    viktor/dir with spaces/directory/c.css
    

    【讨论】:

    • 你是对的。我更新了我的脚本,将目录用双引号括起来,这样它就可以正确输入空间。
    • 这是一个很好的答案,应该说基于其他答案的答案在 Stack Overflow 上非常受欢迎; CC 许可条件明确允许这样做。无需在帖子中对此进行任何免责声明,也无需指导人们如何投票。
    • 感谢 @halfer 帮助我理解一些元数据,感谢您的编辑。
    【解决方案3】:

    您似乎没有在这里使用参数。

    您应该将扩展名和目录替换为 $1 和 $2 以使用参数 1 和 2。

    此外,您似乎忘记关闭该引号...并且目录周围的大括号看起来不对...我认为您需要大括号周围的引号。

    (很遗憾,目前无法测试结果,抱歉)。

    【讨论】:

    • 好的,我已将其更改为查找 "$1" -type f -name "*$2" 但它不打印文件
    • 尝试参考这里,它类似但有一个稍微复杂的要求。 stackoverflow.com/a/25694217/857994。也许你需要 -print?
    • @JohnHumphreys-w00te -print 是默认值。
    • @jsmith147 这应该可以,虽然我会写"*.$2",所以它需要一个. 在扩展之前。
    • @jsmith147 把set -x放在脚本的开头,这样就可以看到变量展开了。
    猜你喜欢
    • 1970-01-01
    • 2010-11-23
    • 2023-03-12
    • 2012-01-14
    • 2011-10-01
    • 2017-03-07
    • 1970-01-01
    • 2012-01-26
    • 2021-11-09
    相关资源
    最近更新 更多