Copy recursively skipping directories with specific name

cpdirectoryrecursive

I have a directory with ~100 Gb. I need to copy this directory to other place skipping specific folders (there is a lot of them). The following is a wrong code to demonstrate my needs.

$ cp -r ~/directory_to_copy /path/to/copy --skip=foo --skip=bar

There is an example of result this command. Original directory tree is

~/directory_to_copy
  aaa
    foo
    doo
      bar
  bbb
    ccc
      ddd
        bar
      eee

Copied tree is

/path/to/copy/
  aaa
    doo
  bbb
    ccc
      ddd
      eee

How to write command for my purposes?

Best Answer

You want rsync:

rsync -va --exclude=foo --exclude=bar ~/directory_to_copy /path/to/copy 

--exclude is used to exclude unwanted files or directories.

-v makes rsync verbose (optional).

-a tells rsync to copy recursively and preserve file attributes. This is optional but, if you don't use -a, you likely want to use -r to copy recursively.

For more complex requirements, both exclude and include options can be specified. It is even possible to change the exclude/include settings from one directory to another by specifying the -F option and placing .rsync-filter files in various locations in the source directory hierarchy. man rsync has details.

Related Question