0

I am very new to this but making progress every day.

I am a bit stuk now...

What I want to do is the following:

Copy folder /test to another folder called /test

But what I would like to do to is when every time I execute the "copy" command, I would like it to check if the /test folder already exists, if so increase a number in de folder name, like /test1, /test2 etc etc.. and start over if for example test10 is reached.

What is the best method to do this?

4
  • Maybe logrotate?
    – Jean-Marie
    Feb 18, 2018 at 13:03
  • Simply make a bash / python script. Feb 18, 2018 at 13:44
  • 1
    @Jean-Marie Either that or use a proper versioning tool like git. Sounds like an XY problem to me.
    – PerlDuck
    Feb 18, 2018 at 15:11
  • logrotate does not do what is asked, as it would copy test(x) to test(x+1), while Björn asks for more and more copies of test. However I suspect that this is really meant?
    – muclux
    Feb 18, 2018 at 15:13

1 Answer 1

1

Assuming you have a folder named test, write a shell script copytest with the following contents:

#!/bin/bash

i=1
while true
do
    if [ -d test$i ]
    then
        i=$((i+1))
    else
        cp -r test test$i
        break
    fi
    if [ $i -eq 11 ]
    then
        rm -r test{1..10}
        cp -r test test1
        break
    fi
done

Now make it executable with chmod u+x copytest and each time you call ./copytest you'll get a new copy named test<n>.

Explanation:

  • [ -d test$i ] checks whether there is already a folder test$i
  • break exits the otherwise infinite loop initiated by while true.
  • The eleventh call will delete all copies and begin again with test1.

This does what you asked for in your question (create more and more copies of test). If you want the copies to "move on" at each call (i.e. test10 is discarded, test9 becomes test10, etc.) then you should take a look at the logrotate command as suggested by @Jean-Marie in the first comment.

5
  • This is exactly what i was looking for, also understanding the syntaxis in the script, may I thank you for your answer!, Also thanks for the others for thinking with me. I realy love this comunity.
    – Björn
    Feb 18, 2018 at 15:44
  • @Björn If this is exactly what you asked for you may mark it as the accepted answer. I added one break when i=11.
    – muclux
    Feb 18, 2018 at 15:55
  • Ok thanks for pointing me to this.. I have run the script several times.. i have adjusted it to my needs but something strange is happening. After the 11th folder copy.. I getting a error message after the 10th copy, and there are folders created beyond 11. 'code' #!/bin/bash i=1 while true do if [ -d map/test$i ] then i=$((i+1)) else cp -r test map/test$i break fi if [ $i -eq 11 ] then rm -r map/test{1..10} cp -r map/test test1 fi 'done'
    – Björn
    Feb 18, 2018 at 16:00
  • That's why I added the missing break when the value of 11 is reached in the loop. See my corrected answer above.
    – muclux
    Feb 18, 2018 at 16:02
  • ahh ok, I see now.. it is working now. thanks!
    – Björn
    Feb 18, 2018 at 16:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .