Have you ever needed a copy of a directory but didn't want all of the subfolders included in the copy?  Maybe you're about to make a massive change to a codebase and want a complete backup as a security blanket 😂 but don't want to backup node_modules with it. Well, you can't use the cp command to accomplish that.

Fortunately, rsync has you covered!  Normally, rsync is used for syncing files between different computers, but it works great locally as well.

rsync -av original-project backup --exclude node_modules
  • The -a part tells rsync to pretty much copy everything and recursively.
  • v tells it to be verbose. I like seeing what's happening while the copy is being made. You can skip that part if you like.
  • original-project is the name of the directory to be copied.
  • backup is the name of the copy. It can be anything you like.
  • --exclude node_modules tells rsync to ignore that child directory. It will not be included in your copy.

Pretty neat right?  If there are additional directories to be excluded, just add them like this:

rsync -av original copy --exculude a --exclude b --exclude c

There are many more options to rsync.  If you'd like to explore more of them, just enter this on the command line: man rsync