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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:15:02+00:00 2026-06-04T01:15:02+00:00

I know this question has been asked, but I can’t find more than one

  • 0

I know this question has been asked, but I can’t find more than one solution, and it does not work for me. Essentially, I’m looking for a bash script that will take a file list that looks like this:

image1.jpg
image2.jpg
image3.jpg

And then make a copy of each one, but number it sequentially backwards. So, the sequence would have three new files created, being:

image4.jpg
image5.jpg
image6.jpg

And yet, image4.jpg would have been an untouched copy of image3.jpg, and image5.jpg an untouched copy of image2.jpg, and so on. I have already tried the solution outlined in this stackoverflow question with no luck. I am admittedly not very far down the bash scripting path, and if I take the chunk of code in the first listed answer and make a script, I always get “2: Syntax error: “(” unexpected” over and over. I’ve tried changing the syntax with the ( around a bit, but no success ever. So, either I am doing something wrong or there’s a better script around.

Sorry for not posting this earlier, but the code I’m using is:

image=( image*.jpg )  
MAX=${#image[*]}  
for i in ${image[*]}  
do  
   num=${i:5:3} # grab the digits  
   compliment=$(printf '%03d' $(echo $MAX-$num | bc))  
   ln $i copy_of_image$compliment.jpg  
done

And I’m taking this code and pasting it into a file with nano, and adding !#/bin/bash as the first line, then chmod +x script and executing in bash via sh script. Of course, in my test runs, I’m using files appropriately titled image1.jpg – but I was also wondering about a way to apply this script to a directory of jpegs, not necessarily titled image(integer).jpg – in my file keeping structure, most of these are a single word, followed by a number, then .jpg, and it would be nice to not have to rewrite the script for each use.

  • 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-04T01:15:03+00:00Added an answer on June 4, 2026 at 1:15 am

    Perhaps something like this. It will work well for something like script image*.jpg where the wildcard matches a set of files which match a regular pattern with monotonously increasing numbers of the same length, and less ideally with a less regular subset of the files in the current directory. It simply assumes that the last file’s digit index plus one through the total number of file names is the range of digits to loop over.

    #!/bin/sh
    
    # Extract number from final file name
    eval lastidx=\$$#
    tmp=${lastidx#*[!0-9][0-9]}
    lastidx=${lastidx#${lastidx%[0-9]$tmp}}
    tmp=${lastidx%[0-9][!0-9]*}
    lastidx=${lastidx%${lastidx#$tmp[0-9]}}
    
    num=$(expr $lastidx + $#)
    width=${#lastidx}
    
    for f; do
        pref=${f%%[0-9]*}
        suff=${f##*[0-9]}
        # Maybe show a warning if pref, suff, or width changed since the previous file
        printf "cp '$f' '$pref%0${width}i$suff'\\n" $num
        num=$(expr $num - 1)
    done |
    sh
    

    This is sh-compatible; the expr stuff and the substring extraction up front is ugly but Bourne-compatible. If you are fine with the built-in arithmetic and string manipulation constructs of Bash, converting to that form should be trivial.

    (To be explicit, ${var%foo} returns the value of $var with foo trimmed off the end, and ${var#foo} does similar trimming from the beginning of the value. Regular shell wildcard matching operators are available in the expression for what to trim. ${#var} returns the length of the value of $var.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this question has been asked/answered but I can't find it for the
I know this question has been asked many times before but I can't find
I know this question has been asked 100x but I can't seem to find
I know this question has been asked several times, but I can't quite seem
I know this question has been asked before, but no one has given any
I know this question has been asked before, but I tried eveything and can't
I know this question has been asked before, but this was one and a
I know that maybe this question has been asked before, but I can't seem
I know this question has been asked before numorous times but they seem to
I know this question has been asked before, but I ran into a problem.

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.