Fork me on GitHub

Project Notes

sync_folder

Comparing methods for synchronizing local folders - cp, rsync.

:arrow_forward: return to the Catalog

Notes

Problem: need to make or update a copy of a folder to another local folder. This may be for deployment purposes or just some disk management.

Recursive Copy

On *nix-type systems, the cp -R makes a recursive copy.

Advantages:

  • simple
  • built-in shell command (no dependencies)

Disadvantages:

  • does not help making a true mirror (i.e. deletions do not propagate)
  • inefficient - copies everything even if redundant

Example:

$ ./make_example_source.sh
Making a fresh copy of ./original
$ ./use_recursive_copy.sh
Making a copy of ./original in ./copy

Using rsync

Most *nix-type systems will have an rsync program available (or it can be installed).

Advantages:

  • can do a proper mirror, including propagating deletions
  • efficient - only copies differences

Disadvantages:

  • may not be natively available
  • a little more unusual - need to read the man page

Example:

$ ./make_example_source.sh
Making a fresh copy of ./original
$ ./use_rsync.sh
Performing pristine copy of ./original/ to ./copy
building file list ... done
created directory ./copy
./
1.txt
2.txt
sub1/
sub1/1.txt
sub1/2.txt
sub2/
sub2/1.txt
sub2/2.txt

sent 512 bytes  received 170 bytes  1364.00 bytes/sec
total size is 32  speedup is 0.05
Adding 3.txt to source and removing 1.txt from dest
And update with sync..
building file list ... done
1.txt
3.txt

sent 305 bytes  received 64 bytes  738.00 bytes/sec
total size is 34  speedup is 0.09

Credits and References

About LCK#23 Bash
Project Source on GitHub Return to the Project Catalog

This page is a web-friendly rendering of my project notes shared in the LittleCodingKata GitHub repository.

LittleCodingKata is my collection of programming exercises, research and code toys broadly spanning things that relate to programming and software development (languages, frameworks and tools).

These range from the trivial to the complex and serious. Many are inspired by existing work and I'll note credits and references where applicable. The focus is quite scattered, as I variously work on things new and important in the moment, or go back to revisit things from the past.

This is primarily a personal collection for my own edification and learning, but anyone who stumbles by is welcome to borrow, steal or reference the work here. And if you spot errors or issues I'd really appreciate some feedback - create an issue, send me an email or even send a pull-request.