Linux Shell 脚本在每个子目录中运行命令

标签 linux shell

我正在尝试制作一个 shell 脚本,它可以遍历 images/文件夹中的所有子目录,并从 ImageMagick 运行一个转换实用程序。

我可以让脚本在单个文件夹中正常工作,但无法获得正确的语法以从顶级目录运行它。

# convert a series of *.png images to *.svg
# images/subfolder1
# images/subfolder2
# etc.

for dir in `ls /*`;
do
    for x in `ls -1 /*.png`
    do
        y=`echo $x | cut -d "." -f1`
        echo "convert $x into $y.svg"
        convert $x $y.svg

    done  

done

完成这项工作的正确方法是什么?

最佳答案

我认为您将像这样使用 GNU Parallel 获得更好的性能、可读性和简单性:

find . -name \*.png | parallel convert {} {.}.svg

为什么要花钱购买所有那些可爱的 Intel 核心,而只使用其中一个?

关于Linux Shell 脚本在每个子目录中运行命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31349236/

相关文章:

c++ - 通过linux终端将数据发送到另一个进程的stdin

xml - 通过 shell 脚本解析 XML

regex - 删除文本文件中的所有超链接,linux脚本

bash - Bash 中单引号和双引号的区别

bash - 将脚本通过管道传输到 bash 中?

linux - 执行n行shell脚本

timespec 在 tv_nsec 字段中可以有超过 1 秒的纳秒值吗?

linux - 在嵌入式板上使用 NFS 文件系统

mysql - 从 Java 和 Shell 运行 cmd 时在 Mysqldump 中添加日期

python - 使用 shell 进行 Grepping - 长时间延迟