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 3222110
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:57:12+00:00 2026-05-17T15:57:12+00:00

Bash scripting does my head in. I have searched for regex assignment, but not

  • 0

Bash scripting does my head in. I have searched for regex assignment, but not really finding answers I understand.

I have files in a directory. I need to loop through the files and check if they fit certain criteria. File names under a certain sequence need to have their sequence increased. Those over a certain sequence need to trigger an alert.

I have pseudo code and need help turning it into correct bash syntax:

#!/bin/sh

function check_file()
{
    # example file name "LOG_20101031144515_001.csv"
    filename=$1

    # attempt to get the sequence (ex. 001) part of file

    # if sequence is greater than 003, then raise alert

    # else change file name to next sequence (ex. LOG_20101031144515_002.csv)
}

for i in `ls -Ar`; do check_file $i; done;

If PHP were an option, I could do the following:

function check_file($file){
    //example file 'LOG_20101031144515_001.csv';

    $parts = explode('.',$file);
    preg_match('/\d{3}$/', $parts[0], $matches);
    if ($matches){
        $sequence = $matches[0];
        $seq      = intval($sequence);

        if ($seq > 3){
            // do some code to fire alert (ex. email webmaster)
        }
        else{
            // rename file with new sequence
            $new_seq  = sprintf('%03d', ++$seq);
            $new_file = str_replace("_$sequence", "_$new_seq", $file);
            rename($file, $new_file);
        }
    }
}

So long story short, I’m hoping someone can help port the PHP check_file function to the bash equivalent.

Thank you

  • 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-05-17T15:57:13+00:00Added an answer on May 17, 2026 at 3:57 pm

    First of all, your question is tagged [bash], but your shebang is #!/bin/sh. I’m going to assume Bash.

    #!/bin/bash
    function check_file()
    {
        # example file name "LOG_20101031144515_001.csv"
        filename=$1
    
        # attempt to get the sequence (ex. 001) part of file
    
        seq=${filename%.csv}
        seq=${seq##*_}
    
        # if sequence is greater than 003, then raise alert
    
        if (( 10#$seq > 3 ))
        then
            echo "Alert!"
        else
            # else change file name to next sequence (ex. LOG_20101031144515_002.csv)
            printf -v newseq "%03d" $((seq + 1))
            echo "${filename/$seq/$newseq}" # or you could set a variable or do a mv
        fi
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am switching to GNU emacs for my python and bash scripting. I really
New to bash scripting, I'm writing a script to copy my TV shows accross
I am trying to lean Bash scripting from cygwin. When i manually type the
I have folders with the naming convention: yearmonthday_jobcode_descriptor ie 20101007_GR1234_Whatsit I'm attempting to write
Do lines in a bash script execute sequentially? I can't see any reason why
A project I'm working on currently involves using a disparate set of technologies, including
Actually in my last question I was trying to use the pipe command. I
I'm writing a script to extract some useful data about a series of chemical
My research group uses a Wordpress blog. Frequently I want post snippets or even

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.