Is there a way to use command 'cp' to copy a directory and exclude certain files/sub-directories within it?


Use rsync:

rsync -avr --exclude='path1/to/exclude' --exclude='path2/to/exclude' source destination

Note that using source and source/ are different. A trailing slash means to copy the contents of the folder source into destination. Without the trailing slash, it means copy the folder source into destination.

Alternatively, if you have lots of directories (or files) to exclude, you can use --exclude-from=FILE, where FILE is the name of a file containing files or directories to exclude.

--exclude may also contain wildcards, such as --exclude=*/.svn*

Copied From: https://stackoverflow.com/a/2194500/749232

If you want to use cp itself:

find . -type f -not -iname '*/not-from-here/*' -exec cp '{}' '/dest/{}' ';'

This assumes the target directory structure is the same as the source's.

Copied From: https://stackoverflow.com/a/4586025/749232

| improve this answer | |

Late into the game but here is a very different solution using plain bash and cp: you can use a global file specification while having some files ignored.

Assume the directory contains the files:

$ ls *
listed1  listed2  listed3  listed4  unlisted1  unlisted2  unlisted3

Using the GLOBIGNORE variable:

$ export GLOBIGNORE='unlisted*'
$ ls *
listed1  listed2  listed3  listed4

Or with more specific exclusions:

$ export GLOBIGNORE='unlisted1:unlisted2'
$ ls *
listed1  listed2  listed3  listed4  unlisted3

Or using negative matches:

$ ls !(unlisted*)
listed1  listed2  listed3  listed4

This also supports several unmatched patterns:

$ ls !(unlisted1|unlisted2)
listed1  listed2  listed3  listed4  unlisted3
| improve this answer | |
  • 2
    For negative matches you need to have extended globs enabled: shopt -s extglob, also you don't need to export GLOBIGNORE: it's supposed to modify the behaviour of the current shell, most child programs wouldn't care about it. – muru Jun 13 '16 at 22:14
  • BTW, to turn off extended glob, run shopt -u extglob. – Rockallite Dec 10 '19 at 0:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.