0

我可以使用以下内容来获取没有空格的文件夹的内容:

files=~/Movies/MyFolder/MySubfolder*

但是,如果文件夹名称中有空格,则以下内容不起作用:

files=~/Movies/My\ Folder/My\ Subfolder/*

我正在尝试回显文件夹内容,如下所示:

for f in "$files"
do
    echo "$f"
done

我也尝试在路径周围加上引号:

files=~/"Movies/My Folder/My Subfolder"/*

感谢您的时间。

4

2 回答 2

0
#Works better, and recursively.
find ~/"Movies/My Folder/My Subfolder" -iname \*jpg | while read f ; do
    echo $f
done
于 2012-06-20T11:43:56.647 回答
0

使用数组:

shopt -s nullglob   # If the folder is empty, don't treat * literally
files=(~/Movies/My\ Folder/My\ Subfolder/*)
for f in "${files[@]}"; do
    echo $f
done

或者,正如丹尼斯所说,直接迭代 glob(我错误地认为这行不通):

shopt -s nullglob
for f in ~/Movies/My\ Folder/My\ Subfolder/*; do
    echo $f
done
于 2012-06-20T12:33:21.730 回答