Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8003233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:36:08+00:00 2026-06-04T16:36:08+00:00

I’m working on a Bash script that will take an array of directories, iterate

  • 0

I’m working on a Bash script that will take an array of directories, iterate through it, create a directory named “processed” inside each directory, and run a command on each file within the directory. Here’s my code (read the comment in the code to see what I’m stuck on). Any ideas?

#!/bin/bash

command -v convert >/dev/null 2>&1 || {
    echo >&2 "Convert is not installed. Aborting.";
    exit 1;
}

declare -a directories_to_process=(
    "$HOME/Desktop/Album 1"
    "$HOME/Desktop/Album 2"
    "$HOME/Desktop/Album 3"
);

for directory in "${directories_to_process[@]}"
do
    if [ -d "$directory" ]; then
        if [ ! -d "$directory/processed" ]; then
            mkdir "$directory/processed"
        fi

        # Insert code to run the following command on each file in $directory:
        #
        # convert $directory/$filename -resize 108x108^ -gravity center -extent 108x108 $directory/processed/$filename
    fi
done

UPDATE:

Here is the working script:

#!/bin/bash

command -v convert >/dev/null 2>&1 || {
    echo >&2 "Convert is not installed. Aborting.";
    exit 1;
}

directories_to_process=(
    "$HOME/Desktop/Album 1"
    "$HOME/Desktop/Album 2"
    "$HOME/Desktop/Album 3"
);

for directory in "${directories_to_process[@]}"
do
    [[ -d $directory ]] || continue

    mkdir -p "$directory/processed"

    for infile in "$directory"/*.jpg
    do
        outfile="$directory/processed/${infile##*/}"
        convert "$infile" \
                -resize '108x108^' \
                -gravity center \
                -extent 108x108 \
                "$outfile"
    done
done
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T16:36:09+00:00Added an answer on June 4, 2026 at 4:36 pm

    Add this in the commented-out area:

    for infile in "$directory"/*; do
      outfile="$directory/processed/${infile##*/}"
      convert "$infile" \
          -resize '108x108^' \
          -gravity center \
          -extent 108x108 \
          "$outfile"
    done
    

    A few other notes:

    • Instead of nesting a great deal of logic inside of if [ -d "$directory" ], consider putting [[ -d $directory ]] || continue at the top of the loop to reduce the nesting depth. (Unlike [ ], quoting is not needed inside [[ ]] in this case).
    • Instead of testing [ ! -d "$directory/processed" ] and using that to decide whether to create the directory, consider unconditionally running mkdir -p "$directory/processed", which will simply exit with a successful status if the directory already exists.
    • Consider replacing command -v convert with type convert, which is somewhat better known than the command -v syntax but will have the same effect.
    • You don’t need the declare -a when declaring an array variable outside of a function; simply directories_to_process=( ... ) will work.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.