6

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?

1
  • Do you have access to GNU tar? Commented Aug 5, 2015 at 21:00

2 Answers 2

10

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.

3
  • 1
    Actually, -a has the same effect as a bunch of other flags, one of which is -r(--recursive) which is not optional here. Commented Aug 6, 2015 at 6:26
  • 1
    @MatthiasUrlichs Good point. Answer updated with info on -r. Commented Aug 6, 2015 at 6:46
  • 1
    you can upvote comments, you know ;-) Commented Aug 6, 2015 at 11:02
1

you can use find for that

find -depth ! -wholename '*foo*' ! -wholename '*bar*' -exec cp --parents '{}' /target/dir/ \; 

note that you will a) need -depth to make sure you first find the directories that need to be omitted and b) use the --parents option to cp to create the full path while copying.

This will however also skip empty folders as -r in cp cannot be used as then ALL filed would be copied once find comes to ./firstDIR .

2
  • 1
    Why not just path, wholename is less portable too.. Commented Aug 6, 2015 at 9:17
  • might as well work, the ! could also be avoided with -prune like in ... -path '*foo*' -o -path '*bar*' -prune -o -exec ... Commented Aug 6, 2015 at 9:24

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.