简体   繁体   中英

Linux Shell Script to run commands in each subdirectory

I'm trying to make a shell script that can go through all of the subdirectories in an images/ folder and run a conversion utility from ImageMagick.

I can get the script to work fine in a single folder but can't get the syntax correct to run it from the top directory.

# 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

What would be the correct way to get this done?

I think you will get far better performance, readability and simplicity using GNU Parallel like this:

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

Why pay for all those lovely Intel cores and only use one of them?

By the way the same thing with makefile:

pngs = $(wildcard */*.png)
svgs = $(pngs:.png=.svg)

all: $(svgs)

%.svg: %.png
    @echo $@ $^

This also can be run in parallel with make -j

Assuming the script is run from the images directory:

#!/bin/bash
shopt -s nullglob
for p in */*.png; do
    s="${p/.png/.svg}"
    echo "converting $p into $s"
    # convert "$p" "$s"
done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM