Saturday, May 4, 2024
97
rated 0 times [  97] [ 0]  / answers: 1 / hits: 192817  / 2 Years ago, sat, june 4, 2022, 8:24:00

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


More From » command-line

 Answers
3

Use rsync:


rsync -av --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.


-av means archive mode and verbose.


--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


[#29908] Monday, June 6, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
hergy

Total Points: 64
Total Questions: 115
Total Answers: 109

Location: Saint Helena
Member since Tue, Nov 3, 2020
4 Years ago
;