Sign up ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a folder (apple) containing files


Another folder (banana) also contains files


How can I insert the contents of apple into banana in the middle while renaming them? So that the banana folder has it's existing files up to doc050.jpg then




and onward, and the previous banana/doc051.jpg through banana/doc150.jpg get renamed banana/doc060.jpg through banana/doc160.jpg?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

This is not the most elegant of solutions but it's easy to understand and should work:

  1. Make a new banana folder

  2. Copy the 1st 50 files over (this is slightly harder than it need be because of the leading 0s):

    for i in {1..9}; do cp banana/doc00$i.jpg; done
    for i in {10..50}; do cp banana/doc0$i.jpg; done
  3. Get apple's files and rename them

    i=51; find apple/ -type f | sort | \
      while read n; do cp $n$i.jpg; let i++; done 
  4. Get the rest of banana's files and rename them

    for i in {51..89}; do \
      let n=$i+10; cp banana/doc0$i.jpg$n.jpg; done
    for i in {90..99}; do \
     let n=$i+10; echo "cp banana/doc0$i.jpg$n.jpg"; done
    for i in {100..150}; do \
     let n=$i+10; cp banana/doc$i.jpg$n.jpg; done

At this point, should be exactly as you want it. Make sure that it is and then rename it to banana.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.