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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:28:37+00:00 2026-06-13T17:28:37+00:00

This could easily be any lang…but I’m using bash for now. I’m looking for

  • 0

This could easily be any lang…but I’m using bash for now. I’m looking for the understanding behind this that can be replicated across langs.

so…

I’m copying a file up the directory tree and than replacing that file with another.

     cp "$DIR"/folder2/file2 "$DIR";
     mv "$DIR"/folder1/file1 "$DIR"/folder2/file2;

Then I take the copied file and move it into another folder..starting the same dance.

     cp "$DIR"/folder3/file3 "$DIR";
     mv "$DIR"/file2 "$DIR"/folder3/file3

I want to do this like 100 times+.

what’s the most elegant way?

Elegant, in the sense of brief and understandable by someone else.

  • 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-13T17:28:38+00:00Added an answer on June 13, 2026 at 5:28 pm

    You’re trying to:

    1. Backup file B.
    2. Rename (or move) file A as file B.
    3. Backup file C.
    4. Rename backup of file B as file C.
    5. Backup file D.
    6. Rename backup of file C as file D.

    and so on.

    It’s actually easier to do it backwards such that you only need to backup the last file.

    last=100
    mv "$DIR/folder$last/file$last" "$DIR"
    for i in $(seq $last -1 2); do
      mv "$DIR/folder$((i-1))/file$((i-1))" "$DIR/folder$i/file$i"
    done 
    

    Note:

    • seq $last -1 2 generates a sequence of numbers from $last up to 2 by increments of -1.
    • I’m using seq instead of brace expansion because the latter doesn’t expand numbers when you have {$last .. 1}
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I suppose this could apply to any dynamic language, but the one I'm using
This could easily be done using code, but I wondered if it could be
I remember I could easily do this in PHP but can't seem to find
I generally work with dataframes and could easily do this for a data frame
This could be a very basic Javascript I know but I just can't get
This could be an amazingly dumb question, but .... I don't think a standard
I think this could be a very easy question for you. But I have
This question could easily take multiple paths, so I will hit the more specific
Alright, what I'm looking for is something that could generate a graphical tree-style map
This could quite possibly be the dumbest question ever asked. Our client wants us

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.