cp command to exclude certain files from being copied
Is there a way to use the cp command to copy a directory and exclude certain files/sub-directories within it?
4 Answers
Use rsync:
rsync -av --exclude='path1/to/exclude' --exclude='path2/to/exclude' source destinationNote 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.
-av means archive mode and verbose.
--exclude may also contain wildcards, such as --exclude=*/.svn*.
Copied From:
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:
1Late 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 unlisted3Using the GLOBIGNORE variable:
$ export GLOBIGNORE='unlisted*'
$ ls *
listed1 listed2 listed3 listed4Or with more specific exclusions:
$ export GLOBIGNORE='unlisted1:unlisted2'
$ ls *
listed1 listed2 listed3 listed4 unlisted3Or using negative matches:
$ ls !(unlisted*)
listed1 listed2 listed3 listed4This also supports several unmatched patterns:
$ ls !(unlisted1|unlisted2)
listed1 listed2 listed3 listed4 unlisted3 2 Quick Start
Run:
rsync -av --exclude='path1/in/source' --exclude='path2/in/source' [source]/ [destination]Notes
-avrwill create a new directory named[destination].sourceandsource/create different results:source— copy the contents of source into destination.source/— copy the folder source into destination.
- To exclude many files:
--exclude-from=FILE—FILEis the name of a file containing other files or directories to exclude.
--excludemay also contain wildcards:- e.g.
--exclude=*/.svn*
- e.g.
Modified from:
Example
Starting folder structure:
.
├── destination
└── source ├── fileToCopy.rtf └── fileToExclude.rtfRun:
rsync -av --exclude='fileToCopy.rtf' source/ destinationEnding folder structure:
.
├── destination
│ └── fileToExclude.rtf
└── source ├── fileToCopy.rtf └── fileToExclude.rtf 1 You can use cp with the ! character.
For example, to exclude the file or files file.txt, test.jpg and the directory nodir, while copying all others from the source directory to the destination directory, you may run:
cp source/!(file.txt|test.jpg|nodir) destinationIf you have, for example, a file structure as this one:
.
├── destination
└── source ├── file.rtf ├── file.txt ├── test.jpg ├── yes | └── test.jpg └── nodir └── other.jpgThe result after running the above command is:
.
└── source (not modified)
└── destination ├── file.rtf └── yes └── test.jpg Here is an article with information on rsync and cp with exclude: How to cp copy and exclude internal files or directories (equivalent to rsync –exclude)
More in general
"Zoraya ter Beek, age 29, just died by assisted suicide in the Netherlands. She was physically healthy, but psychologically depressed. It's an abomination that an entire society would actively facilitate, even encourage, someone ending their own life because they had no hope. Th…"