0

Suppose I have files data_1.mat, data_2.mat, ..., data_n.mat. How to I rsync those files to another folder in one line of code?

The only way I know is do it in a for loop:

for i in 1..n
do 
rsync 'data_$i' \dest\ 
done

This is not convenient if I am transferring data from the server as it needs to establish a new ssh every time in the for loop.

2 Answers 2

1

You can use a shell wildcard to match the set of files in question:

cp -p data_*.mat /dest/

You can use rsync if you insist but based on your description and use it may be the wrong tool:

rsync -a data_*.mat /dest/

In both cases the shell (not the command) expands data_*.mat to a list of all files/directories that match, leaving it unchanged if there are no matches. (The * wildcard matches zero or more characters; ? would match precisely one character.)

If your 1..n is a subset of the possible matches it's a little harder but not much. Some shells (including bash) allow a brace expansion for a fixed range: {1..27} for example. Plug that in to the pattern and you have another solution:

cp -p data_{1..27}.mat /dest/

Remember that when you are using wildcard patterns, it's the shell that expands them, so there's no reason why you can't drop them into a command that starts with echo just to see what's going to happen:

echo data_?.mat
echo rsync -a data_*.mat /dest/
echo cp -p data_{1..27}.mat /dest/
1

You can use command lke this to generate in advance the list of all required files:

seq -f "data_%.0f.mat" 1 11

and this will generate:

data_1.mat
data_2.mat
data_3.mat
    ...
data_11.mat

Then you can use

rsync –files-from=/path/of/file/with/above_list .....

You must log in to answer this question.

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