查找带有正则表达式的目录:white spaces,command find

时间:2015-12-24 16:35:08

标签: regex bash shell

我必须编写一个bash脚本,必须在当前目录中找到目录,并且目录的名称必须以字母[A-z]字母开头。对于我写的shell:

find . -maxdepth 1 -name '[[:alpha:]]*' -type d

没关系。但在我写的脚本中:

#! /bin/bash
files=$(find . -maxdepth 1 -name '[[:alpha:]]*' -type d)
for FILE in $files; do echo 'you are in', $FILE; done;

但是,当它找到一个带有空格的目录(例如./New Directory)时,输出是

./New
Directory

因为它是2个不同的目录。为什么?我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

这可能对您有用:

find . -maxdepth 1 -name '[[:alpha:]]*' -type d | sed 's/^/You are in /'

答案 1 :(得分:1)

find  -maxdepth 1 -type d -regextype posix-awk  -regex ".*/[A-Z].*" -exec echo "you are in" {} \;
相关问题