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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T06:45:05+00:00 2026-06-16T06:45:05+00:00

Now I have the rename script like this: cd /Users/KanZ/Desktop/Project/Test/ n=1 for file in

  • 0

Now I have the rename script like this:

cd /Users/KanZ/Desktop/Project/Test/ 
n=1
for file in *.jpg; do 
    echo $file
    prefix=M
    file_name=M$n.jpg 
    echo $file_name
    n=$(( $n+1 ))
    mv $file $file_name 
done 

The first if I run script, the all of jpg files will be M1.jpg M2.jpg M3.jpg but if I add the new file name A1.jpg to this directory and run script again. All of M1.jpg M2.jpg M3.jpg will be replaced by M4.jpg(before run script, this file named A1.jpg) because the first letter is A. It come before M. This is my problem. I would like to get M1 M2 M3 M4.jpg and every time if there are new files coming, this script will generate the continue name like M5.jpg M6.jpg . How is the code look like? Hope you understand that. Thank u for helping

  • 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-16T06:45:06+00:00Added an answer on June 16, 2026 at 6:45 am

    This little script will do it.

    cd /Users/KanZ/Desktop/Project/Test/ 
    n=$(ls -1 | grep '^M' -c)                    # find the number for M files 
    ls -1 *.jpg| grep -v '^M' | while read file  # for each of the non-M files
    do 
       n=$(($n+1));                       
       new_file=M$n.jpg                          # new name with next available M file name
       mv "$file" "$new_file"                        # rename with new file name
    done 
    

    See how it works.

    test/rename$ for x in 1 2 3; do echo A$x> A$x.jpg; done;
    test/rename$ ls
    A1.jpg  A2.jpg  A3.jpg
    test/rename$ cat ../rnm.sh 
    #!/bin/bash
    cd "$1"
    n=$(ls -1 | grep '^M' -c)
    nfiles=$(ls -1 *.jpg| grep -v '^M');
    for file in $nfiles;
    do 
       n=$(($n+1));
       new_file=M$n.jpg
       mv $file $new_file
    done 
    test/rename$ bash ../rnm.sh  ./
    test/rename$ ls
    M1.jpg  M2.jpg  M3.jpg
    test/rename$ for x in 1 2 ; do echo B$x> B$x.jpg; done;
    test/rename$ ls
    B1.jpg  B2.jpg  M1.jpg  M2.jpg  M3.jpg
    test/rename$ bash ../rnm.sh  ./
    test/rename$ ls
    M1.jpg  M2.jpg  M3.jpg  M4.jpg  M5.jpg
    test/rename$ for x in *.jpg; do echo $x; cat $x; done;
    M1.jpg
    A1
    M2.jpg
    A2
    M3.jpg
    A3
    M4.jpg
    B1
    M5.jpg
    B2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I now have a file uploader that goes on like this This is the
I have a working file rename java tool, now I want to add an
I have a lot of images (.png) file. Now i want to rename the
With help from you guys I now have a script that works like a
I have a file structure like this: 00000010 000000001.file1 000000001.file2 00000020 00000003.file1 00000003.file2 00000003.file3
Here is what I have so far in Windows: import os os.chdir(C:/Users/any/Desktop/test) for files
I have this script set up or rename my torrent folders, however it keeps
I created an Android project a few months ago and now have to automate
Say I have an HTML form like this to collect an email from a
I have a .sln solution file that references a .csproj project file that has

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.