I’ve been experimenting with my latest Raspberry Pi and Sense HAT for a while and having got everything working I just wanted to copy over some files from my desktop.
Since the Raspberry Pi had SSH installed I could easily just use ‘scp’ to copy over the files in a single folder but in this case I wanted to copy files in multiple folders and replicate the directory structure. It didn’t look like this was going to be possible using ‘scp’ so I had to resort to searching to see if I could find an answer. After a while I found a couple of links, one of which showed me how to use ‘rsync’ to replicate a folder structure (and all the python files), and the other showed me how to use ‘rsync’ with ‘scp’ to copy files to a remote machine, so all I had to do was combine these two methods to give me what I wanted.
When using ‘rsync’ be careful to make sure you get any trailing forward slashes in the right place, otherwise the results may not be quite what you expect. For example to copy everything from ‘~/documents/letters/’ to ‘~/archive/letters/’ you could use either of the following commands:
Both commands do the same thing but since ‘rsync’ will create the destination folder if it doesn’t exist the difference between them is that in the first case ‘~/archive/’ folder will be created if necessary and in the second case it must already exist.
The command above won’t work as it will actually copy everything in the folder ‘~/archive/letters/’ to ‘~/archive/letters/letters/’.
Tip – To see what files and folders would be copied without actually running the command use ‘-n’ to perform a dry run and ‘-v’ to show what files or folders would be copied. In the case of the command above this would have shown that is was not going to work, since the first thing that gets copied is the ‘letters/’ folder itself, and not the first file in that folder…
To selectively copy files and folders using ‘rsync’ you can use filters to include and exclude certain files. The following example uses a filter to include all folders and combines this with a second filter to exclude all files in order to replicate the folder structure from the source folder to the destination folder:
Note – You can’t exclude all the files before including the folders as an '*' by itself will match every file and folder, but by including every folder first using '*/' we get the result we want.
Adding additional filters allows you include other files so since I wanted to copy all the python and shell scripts as well I needed to add two additional filters before the final filter that tells ‘rsync’ not to copy any files.
This will only copy all the python and shell scripts but it includes all the folders, so I added an additional filter exclude hidden folders.
Note – Since '*/' will match all folders even f they are hidden you need to put the filter to exclude hidden folders before the one that includes all folders.
You can also include or exclude files using ‘--include’ and ‘--exclude’.
To get ‘rsync’ to copy files to a remote system using ‘ssh’ you need to specify the remote shell.
If you wanted to copy everything from ‘~/documents/letters/’ to ‘~/archive/letters/’ in the another user’s home folder on a remote machine with an IP address of 192.168.0.2 you would need the following command:
So finally to copy all the folders and sub-folders from one user’s home folder to another user’s home folder on a remote machine including any Python or shell scripts I needed to combine the remote shell with the appropriate filters:
I have added an additional filter to exclude any folder that matches '*/*/*/'. This will exclude any folder more than two levels deep which will limit the depth of the recursive copy.
Raspberry Pi is a trademark of the Raspberry Pi Foundation