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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:40:36+00:00 2026-06-01T23:40:36+00:00

My problem is that I have too many files in single directory. I cannot

  • 0

My problem is that I have too many files in single directory. I cannot “ls” the directory, cos is too large. I need to move all files in better directory structure.

I’m using the last 3 digits from ID as folders in reverse way.

For example ID 2018972 will gotta go in /2/7/9/img_2018972.jpg.

I’ve created the directories, but now I need help with bash script. I know the IDs, there are in range 1,300,000 - 2,000,000. But I can’t handle regular expressions.

I wan’t to move all files like this:

/images/folder/img_2018972.jpg -> /images/2/7/9/img_2018972.jpg

I will appreciate any help on this subject. Thanks!

  • 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-01T23:40:39+00:00Added an answer on June 1, 2026 at 11:40 pm

    EDIT: after explainations in comments the following assumptions exists:

    • filenames are in the form of img_<id>.jpg or img_<id>_<size>.jpg
    • the new dir is the reverse order of the three last digits of the id

    using Bash:

    for file in /images/folder/*.jpg; do 
        fname="${file%.*}"      # remove extension and _<size>
        [[ "$fname" =~ img_[0-9]+_[0-9]+$ ]] && fname="${fname%_*}"
    
        last0="${fname: -1:1}"  # last letter/digit
        last1="${fname: -2:1}"  # last but one letter/digit
        last2="${fname: -3:1}"  # last but two letter/digit
    
        newdir="/images/$last0/$last1/$last2"
        # optionally check if the new dir exists, if not create it
        [[ -d "$newdir" ]] || mkdir -p "$newdir"
    
        mv "$file" "$newdir"
    done
    

    if * can’t handle it (although I think * in a for loop has no limits),
    use find as suggested by @Michał Kosmulski in the comments

    while read -r; do 
        fname="${REPLY%.*}"     # remove extension and _<size>
        [[ "$fname" =~ img_[0-9]+_[0-9]+$ ]] && fname="${fname%_*}"
    
        last0="${fname: -1:1}"  # last letter/digit
        last1="${fname: -2:1}"  # last but one letter/digit
        last2="${fname: -3:1}"  # last but two letter/digit
    
        newdir="/images/$last0/$last1/$last2"
        # optionally check if the new dir exists, if not create it
        [[ -d "$newdir" ]] || mkdir -p "$newdir"
    
        mv "$REPLY" "$newdir"
    done < <(find /images/folder/ -maxdepth 1 -type f -name "*.jpg")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the problem that my view controller class has too many delegates and
We have the problem that we have the open project files in our SVN
I have several classes that need to load some properties files, and I was
I have a folder with far too many files in, and I want to
Problem: to combine PATHs with filenames, such that I can easily source many files.
I frequently need to make many replacements within files. To solve this problem, I
The problem that I have is somehow very specific. I have to implement a
I have encountered a problem that I have not come accross yet when setting
I have a problem that I have not faced before: It seems that the
i have bought a template that have built in contact form problem is that

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.