bash - 在目录路径中用空格列出 bash 中的文件的问题

标签 bash command-line ls

当在命令行中输入目录时,这是:

ls -d -1 "/Volumes/Development/My Project/Project"/**/* | grep \.png$

打印以 .png 结尾的所有文件的列表。

但是当我尝试创建脚本时:

#! /bin/bash

clear ;

# Tempoary dir for processing
mkdir /tmp/ScriptOutput/ ;

wdir="/Volumes/Development/My Project/Project" ;

echo "Working Dir: $wdir" ;

# Get every .PNG file in the project
for image in `ls -d -1 "$wdir"/**/* | grep \.png$`; do
...    
done

我得到错误:

cp: /Volumes/Development/My: No such file or directory

空格 导致了问题,但我不知道为什么?

最佳答案

另一种选择是更改 IFS:

OLDIFS="$IFS"  # save it
IFS="" # don't split on any white space
for file in `ls -R . | grep png`
do 
    echo "$file"
done
IFS=$OLDIFS # restore IFS

man bash 中阅读有关 IFS 的更多信息。

关于bash - 在目录路径中用空格列出 bash 中的文件的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6897190/

相关文章:

c - 文件路径重定向

vim - 显示整行 vim

bash - 使用 head -1 的输出

arrays - 如何在 Linux Bash 中将 ls 分配给数组?

linux - 为什么 Bash `(())` 在 `[[]]` 内不起作用?

bash - 使用sed从字符串中删除子字符串

linux - 目录大小奇怪。尺寸过大

command-line - ffmpeg 合唱过滤器错误 : At least one output file must be specified

php 获取所有子目录和所有文件的列表

bash - 递归删除服务器上 CVS 目录的脚本