Debian – rsync failing to choose the correct target directory

debianrsync

I'm trying to write a short shell script which also handles copying a file called webcam.jpg into a date coded subdirectory. I'm using rsync for this as it automatically creates a directory if it doesn't already exist.

Everything works fine when I use:

rsync -a webcam.jpg ./$(date +%Y-%m-%d)/$(date +%Y-%m-%d_%H-%M).jpg

But when going one directory deeper, rsync starts throwing errors, and I really don't have a clue why. I'm typing:

rsync -a webcam.jpg ./saved_images/$(date +%Y-%m-%d)/$(date +%Y-%m-%d_%H-%M).jpg

and my output is:

rsync: change_dir#3 "/var/www/tmp/test//./saved_images/2013-07-07" failed: No such file or directory (2)
rsync error: errors selecting input/output files, dirs (code 3) at main.c(677) [Receiver=3.0.9]
rsync: connection unexpectedly closed (9 bytes received so far) [sender]
rsync error: error in rsync protocol data stream (code 12) at io.c(605) [sender=3.0.9]

I see the relevant error in the first line, but why is rsync trying to access this test//./saved_images directory in the first place? What's wrong with test/saved_images/?

Best Answer

Ok /var/www/tmp/test//./saved_images/2013-07-07 is the same as /var/www/tmp/test/saved_images/2013-07-07.

Double / are ignored you can type ls //// and it is the same as ls /.

The dot . is the same directory it is in. So ls /. shows the same output as ls / and so /var/www/tmp/test/. points to the directory /var/www/tmp/test/.

So rsync just takes the current directory it is in, in you case var/www/tmp/test/ (at least when your path starts with a .). Then its adds an extra / so it can make sure that the path it definitely has a / add the end. In the last step its adds the part you gave it, here ./saved_images/$(date +%Y-%m-%d)/$(date +%Y-%m-%d_%H-%M).jpg

The error you are seeing is that the directory /var/www/tmp/test/saved_images/ is not there and rsync will not create it, because it seams that rsync only creates one directory.

Edit

Maybe for your problem you should just use a script with

today_dir=$(date +%Y-%m-%d)
mkdir -p ./$today_dir/
cp webcam.jpg ./$today_dir/$(date +%Y-%m-%d_%H-%M).jpg
Related Question