小编Sam*_*hke的帖子

使 BASH 脚本 `for` 处理带有空格的文件名(或解决方法)

虽然我已经使用 BASH 好几年了,但我在 BASH 脚本方面的经验相对有限。

我的代码如下。它应该从当前目录中获取整个目录结构并将其复制到$OUTDIR.

for DIR in `find . -type d -printf "\"%P\"\040"`
do
  echo mkdir -p \"${OUTPATH}${DIR}\"        # Using echo for debug; working script will simply execute mkdir
  echo Created $DIR
done
Run Code Online (Sandbox Code Playgroud)

问题是,这是我的文件结构示例:

$ ls
Expect The Impossible-Stellar Kart
Five Iron Frenzy - Cheeses...
Five Score and Seven Years Ago-Relient K
Hello-After Edmund
I Will Go-Starfield
Learning to Breathe-Switchfoot
MMHMM-Relient K
Run Code Online (Sandbox Code Playgroud)

请注意空格 :-S 并for逐字接受参数,因此我的脚本输出如下所示:

Creating directory structure...
mkdir -p "/myfiles/multimedia/samjmusicmp3test/Learning"
Created Learning
mkdir …
Run Code Online (Sandbox Code Playgroud)

bash find filenames

12
推荐指数
3
解决办法
2万
查看次数

标签 统计

bash ×1

filenames ×1

find ×1