Glob并包含当前目录和递归子目录

时间:2017-02-20 16:27:49

标签: bash

假设我有以下目录结构:

file.txt
file.css
file.js
directory/file.txt
directory/file.css
directory/file.js
directory/subdirectory/file.txt
directory/subdirectory/file.css
directory/subdirectory/file.js

我希望能够在所有目录中找到所有txtcss文件(理想情况下不使用find,因为我希望我编写的命令是可扩展的)。文件扩展名列表应位于名为FILE_PATTERN

的变量中

该命令最终应输出:

  • directory/subdirectory/file.css
  • directory/subdirectory/file.txt
  • directory/file.css
  • directory/file.txt
  • file.css
  • file.txt

但我正在尝试的每个通配模式都没有返回结果:

$ FILE_PATTERN="{**/,}*.{css,txt}"
$ echo $FILE_PATTERN
# FAILS: Returns 0 results

$ FILE_PATTERN={**/,}*.{css,txt}
$ echo $FILE_PATTERN
# FAILS: Returns 0 results

$ echo {**/,}*.{css,txt}
# FAILS: Only returns:
directory/file.css directory/file.txt file.css file.txt

有什么建议吗?

2 个答案:

答案 0 :(得分:0)

不确定你有什么反对查找,只需稍加努力就可以实现非常可扩展(即使在MAC上):

let date = Date()
let calendar = Calendar.current
let hour = calendar.component(.hour, from: date)
let minutes = calendar.component(.minute, from: date)
let seconds = calendar.component(.second, from: date)
print("hours = \(hour):\(minutes):\(seconds)")

我没有可玩的mac,但我认为-E选项应该对你有用,如果没有,请看一下手册页:)

答案 1 :(得分:0)

根据@ grail的回应,我最终选择了这个:

FILE_EXTENSIONS="txt|css"
find -E . -iregex ".*\\/[^\\/]+\\.(${FILE_EXTENSIONS})\$"
./directory/file.css
./directory/file.txt
./directory/subdirectory/file.css
./directory/subdirectory/file.txt
./file.css
./file.txt