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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T09:05:17+00:00 2026-05-19T09:05:17+00:00

I’m using a for-loop similar to this one to truncate all filenames in a

  • 0

I’m using a for-loop similar to this one to truncate all filenames in a folder to 16 characters:

for i in *; do
    Shortname=${i:0:16}     # Let's assume I don't need the extension
    mv "$i" "$Shortname"
done

The problem is: Whenever two filenames have the same first 16 characters, the later one will overwrite the previous one (on OS X mv behaves that way).

How can I check if a file with the name “Shortname” already exists, and if so, replace the last character of “Shortname” with a number. Then check again if a file with that name exists, and if so, try a higher number. And so on. If it arrives at number 9 and so far all names have been taken, it should replace the last TWO characters of the “Shortname” with “10” and check if that file already exists.

Example: Let’s say I have a directory with the following files in it:

MyTerriblyLongLongFirstFile.jpg
MyTerriblyLongLongSecondFile.jpg
MyTerriblyLongLongThirdFile.jpg
...
MyTerriblyLongLongFourteenthFile.jpg
...
MyTerriblyLongLongOneHundredSixtySeventhFile.jpg
...
MyTerriblyLongLongFiveMillionthFile.jpg

Note that the first 16 letters are the same for all files. After running the script, I would like them to be renamed to this:

MyTerriblyLongL1.jpg
MyTerriblyLongL2.jpg
MyTerriblyLongL3.jpg
...
MyTerriblyLong14.jpg
...
MyTerriblyLon167.jpg
...
MyTerribl5000000.jpg

It doesn’t matter if “MyTerriblyLongLongFourteenthFile.jpg” is renamed to “MyTerriblyLong14.jpg”, that depends on alphabetical sorting. It’s just important that they each get a unique name.

What is the best way to do this?

  • 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-19T09:05:17+00:00Added an answer on May 19, 2026 at 9:05 am

    Try this on test files first. The usual method of testing by using echo instead of mv won’t tell you much since the potential name collisions wouldn’t be created.

    #!/bin/bash
    num=1
    length=16
    for file in M*.jpg
    do
        newname=$file
        until [[ ! -f $newname ]]
        do
            (( sublen = length - ${#num} ))
            printf -v newname '%.*s%d' "$sublen" "$file" "$num"
            (( num++ ))
        done
        mv "$file" "$newname"
    done
    

    Testing:

    $ touch MyTerriblyLongLongFilenames{a..k}.jpg
    $ touch MyTerriblyLongL3
    $ ls M*
    MyTerriblyLongL3                  MyTerriblyLongLongFilenamesf.jpg
    MyTerriblyLongLongFilenamesa.jpg  MyTerriblyLongLongFilenamesg.jpg
    MyTerriblyLongLongFilenamesb.jpg  MyTerriblyLongLongFilenamesh.jpg
    MyTerriblyLongLongFilenamesc.jpg  MyTerriblyLongLongFilenamesi.jpg
    MyTerriblyLongLongFilenamesd.jpg  MyTerriblyLongLongFilenamesj.jpg
    MyTerriblyLongLongFilenamese.jpg  MyTerriblyLongLongFilenamesk.jpg
    $ ./nocollide
    $ ls M*
    MyTerriblyLong10  MyTerriblyLongL1  MyTerriblyLongL4  MyTerriblyLongL7
    MyTerriblyLong11  MyTerriblyLongL2  MyTerriblyLongL5  MyTerriblyLongL8
    MyTerriblyLong12  MyTerriblyLongL3  MyTerriblyLongL6  MyTerriblyLongL9
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm making a simple page using Google Maps API 3. My first. One marker
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text

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.