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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:41:00+00:00 2026-06-13T10:41:00+00:00

I have a script where inotifywait is piped into a while loop that executes

  • 0

I have a script where inotifywait is piped into a while loop that executes the following logic.

     cp "$S3"/2/post2.png "$S3";
     mv "$S3"/1/post1.png "$S3"/2/post2.png;
     cp "$S3"/3/post3.png "$S3";
     mv "S3"/post2.png "$S3"/3/post3.png;

so forth and so on….. then at the end of the script…

     mv "$dir"/$file "$S3"/1/post1.png

That line represents a fresh post, the above is the rotation of older post.

I can can hand code the iterations all the way down to 100+, but I would like to program more efficiently and save time.

So, what’s some correct ways to loop 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-06-13T10:41:01+00:00Added an answer on June 13, 2026 at 10:41 am

    I think a better mechanism would list the directories in "$S3" in reverse numeric order, and arrange to process them like that. It isn’t clear if the 100 directories are all present or whether they need to be created. We’ll assume that directories 1..100 might exist, and directory N will always and only contain postN.png.

    I’m assuming that there are no spaces, newlines or other awkward characters in the file paths; this means that ls can be used without too much risk.

    for dirnum in $(cd "$S3"; ls */*.png | sed 's%/.*%%' | sort -nr)
    do
        next=$(($dirnum + 1))
        mv "$S3/$dirnum/post$dirnum.png" "$S3/$next/post$next.png"
    done
    

    The cd "$S3" means I don’t get a possibly long pathname included in the output; the ls */*.png lists the files that exist; the sed removes the file name and slash, leaving just a list of directory numbers containing files; and the sort puts the directories in reverse numeric order.

    The rest is straight-forward, given the assumption that the necessary directories already exist. It would not be hard to add [ -d "$S3/$next" ] || mkdir -p "$S3/$next" before moving the file. Clearly, after the loop you can use your final command:

    mv "$dir/$file" "$S3/1/post1.png"
    

    Note that I’ve enclosed complete names in double quotes; it generally leads to fewer nasty surprises if something acquires spaces unexpectedly.

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

Sidebar

Related Questions

I have script that add water mark (PNG, transparent) to image(JPG). Works fine with
I have script that reads remote file content and writes it to local server.
I need to have script.sh , that would create files f1.txt and f2.txt with
I have this script that run to fix my menu bar to the browser
I have an script interpreter that is spawn by a deamon and has to
I have a script that recursively loops through all the sub directories and compresses
I have a sh/bash script that needs to call a batch file with parameters
Say I have script, that does long polling on server to check if user
I currently have script that slides down a once the page has loaded. So
I have a bash script that processes some data using inotify-tools to know when

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.