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

  • Home
  • SEARCH
  • 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 8759737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:52:43+00:00 2026-06-13T14:52:43+00:00

I have this script below that will be daemonized and triggered possibly hundreds if

  • 0

I have this script below that will be daemonized and triggered possibly hundreds if not thousands of times by different users.

The script uses inotifywait to watch a folder for an upload and then move the uploaded file to its final destination for presentation, after rotating (backup/move) previous uploads. The code will be run against different upload folders that are created on the fly.

#!/bin/bash

db="$VAR1"/path/to/upload/folder/
s3="$VAR2"/path/to/remote/folder

inotifywait -m -r -e attrib "$db" |
while read dir ev file;
do

        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

        mv "$db"/"$file"  "$s3"/1/post1.png

done

What can I do to optimize it? Or should it be rewritten a faster programming language? Also, how can I test scripts under a certain amount of load?

  • 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-13T14:52:45+00:00Added an answer on June 13, 2026 at 2:52 pm

    This does not give identical behavior, but it avoids sorting:

    #!/bin/bash
    
    db="$VAR1"/path/to/upload/folder/
    s3="$VAR2"/path/to/remote/folder
    
    inotifywait -m -r -e attrib "$db" |
    while read dir ev file;
    do
        dirnum=1
        while test -e "$s3/$dirnum"; do : $(( dirnum += 1 )); done
        while test $dirnum -gt 0; do
            next=$(( dirnum + 1 ));    
            mkdir -p $s3/$next                   
            mv "$s3/$dirnum/post$dirnum.png" "$s3/$next/post$next.png"
            : $(( dirnum -= 1 ))
        done
        mv "$db/$file" "$s3"/1/post1.png
    done
    

    If you keep track of the highest number stored to $s3, you can avoid the first
    loop. Doing so is slightly more fragile if other processes are creating files
    in $s3, but in that case you have a race condition even in this simplistic solution.
    It would be a lot simpler to not rename the files, but put the first file uploaded in
    $s3/1, and the next in $s3/2. In that case the script can be written:

    #!/bin/bash
    
    db="$VAR1"/path/to/upload/folder/
    s3="$VAR2"/path/to/remote/folder
    dirnum=1
    while test -e "$s3/$dirnum"; do : $(( dirnum += 1 )); done
    inotifywait -m -r -e attrib "$db" |
    while read dir ev file;
    do
        mkdir -p "$s3"/$dirnum
        mv "$db/$file" "$s3"/$dirnum/post$dirnum.png
        : $(( dirnum += 1 ))
    done
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this script that displays all the users images which i will display
i have this script below that will resize the .content on window resize it
I have this script below where it scans users that are allowed to see
I have this script set up that echoes all relevant users in a database
I have a script below that uses $_SERVER['HTTP_USER_AGENT'] because get_browser() is not active on
I have this script that run to fix my menu bar to the browser
I have this script in the head of my index.html. I want different scripts
I am looking for some help. I have found this script that sort of
The script I have below is for users to upload a profile picture to
I'm trying to sum up all the values in this script below but not

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.