Home > Software engineering >  In: for i in temp/*; do when I use i it contains the path temp/ and I do not want this?
In: for i in temp/*; do when I use i it contains the path temp/ and I do not want this?

Time:01-06

I have the magick absolute in a folder and in a sub-folder temp I have images. I need/want to process the images and write them to another sub-folder output.

folder (with magick)
    temp sub-folder (with input files)
    output sub-folder (where processed images are placed)

But I am unable to stop the variable i picking up the path temp/. How do I stop it? Do I have to strip the characters temp/ from i then use it? Or is there a simple way to do this?

for i in temp/*; do
    ./magick convert -resize 450 -strip -quality 90% temp/$i output/$i
done

CodePudding user response:

Yes you can with parameter expansion:

shopt -s nullglob

for i in temp/*; do
    ./magick convert -resize 450 -strip -quality 90% "$i" "output/${i#temp/}"
done

The nullglob prevents 0 matches from expanding to the pattern.

Maybe you can also just change working directory to temp and use ... I'm just not sure if magick will still work if referred from a different directory.

cd temp || exit

for i in *; do
    ../magick convert -resize 450 -strip -quality 90% "$i" "../output/$i"
done
  •  Tags:  
  • Related