In Terminal, type man rsync
to get its manual page. That way you get the one for the utility included in your macOS version -- they don't often change, but it does save you from wasting time when a google leads you to the page for the linux version instead...
The same works for most Unix utilities: man ditto
, man cp
, etc.
r
is "recursive" -- as you've found, you need that to get the sub-directories of the thing you are copying. R
is "use relative paths", which is what keeps your "parent" folder structure.
To do similar folder structuring in ditto
you'd usually set a sourceBase
and destinationBase
and create the "copy to" path from the file path by
- Getting the file path
- Chopping
sourceBase
of the front
- Adding
destinationBase
to the front
- Using that path
So if you wanted to copy ~/Documents/Projects/A/B/myfile.txt
to the (currently empty) ~/Dropbox/Project Backups/
to get ~/Dropbox/Project Backups/A/B/myfile.txt
you'd set sourceBase
to ~/Documents/Projects/
, destinationBase
to ~/Dropbox/Project Backups/
and then "Search thePath
for sourceBase
and Replace with destinationBase
", using the result in ditto
.
rsync
is obviously less work! So yes, use that if you don't care about Mac stuff like labels and other metadata.
This will crop up a lot in your shell scripts, so is worth remembering: ~
is not expanded when your path is in double-quotes. As you found, the KM solution is to expand the path before using it in the shell script.
Use the --stats
option, as @tiffle suggested above, and don't use -v
. If you "Save results to variable" you'll get similar to
Number of files: 5
Number of files transferred: 1
Total file size: 426405 bytes
Total transferred file size: 426405 bytes
Literal data: 426405 bytes
Matched data: 0 bytes
File list size: 133
File list generation time: 0.001 seconds
File list transfer time: 0.000 seconds
Total bytes sent: 426676
Total bytes received: 66
sent 426676 bytes received 66 bytes 853484.00 bytes/sec
total size is 426405 speedup is 1.00
...and you can use KM actions to extract the values you want, like
...to get the number of files transferred. If only want one value from the stats you can even get it directly in the shell script
rsync -rR --stats sourcePath destPath | egrep -o "transferred: (\d+)" | egrep -o "\d+"
...which will return just the number from the "Number of files transferred:" line -- you could add those up as you went through your "For Each" loop.