Take the 2-minute tour ×
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

doc001.jpg
doc002.jpg
...
doc010.jpg, 

Another folder (banana) also contains files

doc001.jpg   
...
doc150.jpg  

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

apple/doc001.jpg

becomes

banana/doc051.jpg 

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
add comment

1 Answer

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

    mkdir banana.new
    
  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 banana.new/; done
    for i in {10..50}; do cp banana/doc0$i.jpg banana.new/; done
    
  3. Get apple's files and rename them

    i=51; find apple/ -type f | sort | \
      while read n; do cp $n banana.new/doc0$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 banana.new/doc0$n.jpg; done
    for i in {90..99}; do \
     let n=$i+10; echo "cp banana/doc0$i.jpg banana.new/doc$n.jpg"; done
    for i in {100..150}; do \
     let n=$i+10; cp banana/doc$i.jpg banana.new/doc$n.jpg; done
    

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

share|improve this answer
add comment

Your Answer

 
discard

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.