Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to write a bash script that will rsync only a specific subset of folders. I am trying to figure out a more slick so that I can just add a variables such as FOLDER1='name of folder in home directory' and then

rsync -arvz --delete /home/emka/$FOLDER1/ /home/emka/Desktop/Mount/$FOLDER1

Currently I have FOLDER1 through FOLDER13, but I do not want to have the above line thirteen times.

Could someone give me a push on how to do this?

share|improve this question

migrated from Nov 22 '11 at 1:07

This question came from our site for professional and enthusiast programmers.

Not a general answer to your question: But in this particular example you could also just use wildcards: rsync -arvz --delete /home/emka/{folder1,folder2,folder[3456]} /home/emka/Desktop/Mount/. Note the nested wildcards. And note how the trailing slashes/ had to change. – cfi Nov 21 '11 at 8:09

Bash allows you to use arrays and loops.

Define each path as an element of an array, then loop through the array.

share|improve this answer
Thanks! I didn't know this. I'm quite new to bash. – EMKA Nov 21 '11 at 5:28
Shell scripting is dang blasted fun! You'll like it more and more the more you use it. – Christopher Nov 21 '11 at 7:06
there is no need to loop through the array in such simple case where one rsync command can do all work – Cougar Nov 22 '11 at 7:41

Some people here don’t seem to have read the original question properly, but as one suggested: you can (and should) use arrays. If you really have the folder names stored in the variables FOLDER1, FOLDER2, etc., and want to keep it that way, you can use the following:

for i in {1..13}; do
   eval "FOLDER=\$FOLDER$i"
   rsync -arvz --delete "/home/emka/$FOLDER/" "/home/emka/Desktop/Mount/$FOLDER"

Using quotation marks is a good habit, and in this case they are required, since you have spaces in the directory names.

share|improve this answer
If you do rsync dir1 dir2/ then dir1 contents will be in dir2/dir1 anyway. You do not need to use "for" command for that as long $FOLDER name is the same on both side. {1..13} is brace expansion that is evaluated before variable name i.e. $FOLDER{1..13} will be list of $FOLDER1, $FOLDER2, … $FOLDER13 anyway – Cougar Nov 22 '11 at 7:25

Bash 3.0+ supports range arrays.

for i in {1..13}
    rsync -arvz --delete /home/emka/FOLDER${i}/ /home/emka/Desktop/Mount/FOLDER${i}
share|improve this answer
ach. I read it as meaning you had 13 folders called Folder1 - Folder 13. Carry on! – ZenGyro Nov 21 '11 at 11:28

Using Bash brace expansion you can just do this:

rsync -arvz --delete /home/emka/$FOLDER{1..13} /home/emka/Desktop/Mount/

If you want to use arrays (as some people suggest and which is good idea) then it is still possible to use one rsync without loop as long as folder names in array do not end with slash.

cd /home/emka
rsync -arvz --delete ${folderarray[@]} /home/emka/Desktop/Mount/

Put "echo" in the beginning of this line to see what it is going to do before first use :-)

share|improve this answer

You must log in to answer this question.