【发布时间】:2022-12-03 05:57:36
【问题描述】:
为递归目录中的每个文件创建文件夹,将文件放入文件夹
在 MacOS 上,到目前为止我有 . .
for file in $(ls -R); do if [[ -f "$file" ]]; then mkdir "${file%.*}"; mv "$file" "${file%.*}"; fi; done
这在嵌套文件夹的顶层正确运行,但对较低层没有任何作用。
为了隔离错误,我尝试了这个,对 rtf 文件进行操作。 .
for i in $(ls -R);do
if [ $i = '*.rtf' ];then
echo "I do something with the file $i"
fi
done
这挂起,所以我简化为 . .
for i in $(ls -R); do echo "file is $i" done
那也挂了,所以我试过了。 .
for i in $(ls -R); do echo hello
那也挂了。
ls -R 致力于提供所有文件的递归列表。
建议表示赞赏!
【问题讨论】:
-
要获得一些有用的提示,请将合适的 shebang (
#!/bin/bash) 添加到您的脚本中,然后将其粘贴到 shellcheck.net。
标签: bash macos loops directory nested