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

  • Home
  • SEARCH
  • 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 8171795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:34:23+00:00 2026-06-06T21:34:23+00:00

I have written a small bash* script to process files in a directory. The

  • 0

I have written a small bash* script to process files in a directory. The files have a specific naming convention, which determines how the file is to be processed.

The filename starts with a geographic origin, and then contains a keyword in the filename that determines how it should be processed.

Here is the stripped down version of my script:

DATA_DIRECTORY=./data

array=( 'uk' 'usa' 'fra' 'ukr' )

for i in "${array[@]}"
do
    echo "#################################################"
    echo "Loop index : $i"
    echo "#################################################"

    # process baboon data for the current country
    for filename in $DATA_DIRECTORY/$i_*_baboon*.csv; do
       echo "Processing file: $filename in loop: $i"
    done

    # process whale data for the current country
    for filename in $DATA_DIRECTORY/$i_*_whale*.csv; do
       echo "Processing file: $filename in loop: $i"
    done

    # process lion data for the current country
    for filename in $DATA_DIRECTORY/$i_*_lion*.csv; do
       echo "Processing file: $filename in loop: $i"
    done

done

I am expecting the script above to match the files in the following way: A file named us_2007_deaths_caused_by_baboon.csv will be processed once – when the loop counter variable is ‘us’ and during the loop for filename in $DATA_DIRECTORY/$i_*_baboon*.csv;. However, the matching is not working as I expected, and each (nested for loop) is looping over all of the files in the directory – which is NOT what I want.

How may I pattern match the files based on the filenames?

*Note: I am using bash version 4.1.5 on Ubuntu 10.0.4

  • 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-06T21:34:25+00:00Added an answer on June 6, 2026 at 9:34 pm

    Write ${i}_ instead of $i_ because $i_ is “” (bash thinks that i_ is a variable here).

    For example:

    for filename in $DATA_DIRECTORY/${i}_*_baboon*.csv; do
       echo "Processing file: $filename in loop: $i"
    done
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a small bash script which needs an ssh tunnel to draw
I have written a small function, which uses ElementTree to parse xml file,but it
I have written a small bash script called isinFile.sh for checking if the first
I have written (tried to) this small bash script for searching through a range
I have written small bash script to try to get the last x characters
I have written a small script in Perl which I am executing on Windows
I have written a small GUI with qtdesigner and python which should display in
I have written a small test program in which I try to use the
I have written a small XML validator, that takes in an XML file and
i have written a small form application, which contains textbox only. I have enabled

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.