I am trying to write a simple bash script that will copy the entire contents of a folder including hidden files and folders into another folder, but I want to exclude certain specific folders. How could I achieve this?
Join Stack Overflow to learn, share knowledge, and build your career.
|
Use rsync:
Note that using Alternatively, if you have lots of directories (or files) to exclude, you can use
|
|||||||||||||||||||||
|
|
Use tar along with a pipe.
You can even use this technique across ssh. |
|||||||||||||||||||||
|
|
You can use An example from
cd /source-dir
find . -name .snapshot -prune -o \( \! -name *~ -print0 \)|
cpio -pmd0 /dest-dir
This command copies the contents of /source-dir to /dest-dir, but omits
files and directories named .snapshot (and anything in them). It also
omits files or directories whose name ends in ~, but not their con‐
tents. The construct -prune -o \( ... -print0 \) is quite common. The
idea here is that the expression before -prune matches things which are
to be pruned. However, the -prune action itself returns true, so the
following -o ensures that the right hand side is evaluated only for
those directories which didn't get pruned (the contents of the pruned
directories are not even visited, so their contents are irrelevant).
The expression on the right hand side of the -o is in parentheses only
for clarity. It emphasises that the -print0 action takes place only
for things that didn't have -prune applied to them. Because the
default `and' condition between tests binds more tightly than -o, this
is the default anyway, but the parentheses help to show what is going
on.
|
|||||
|
|
Similar to Jeff's idea (untested):
|
|||
|
|
|
you can use tar, with --exclude option , and then untar it in destination. eg
see the man page of tar for more info |
|||
|
|
Untested... |
|||
|
|
|
inspired by @SteveLazaridis's answer, which would fail, here is a POSIX shell function - just copy and paste into a file named
Example usage
|
||||
|
|
cp -R !(dir1|dir2) path/to/destination– Boris D. Teoharov Aug 14 '14 at 18:02!(dir1|dir2)pattern requiresextglobto be turned on (shopt -s extglobto turn it on). – Boris D. Teoharov Aug 20 '14 at 19:07