递归复制文件夹,排除一些文件夹
我正在尝试编写一个简单的bash脚本,它将将包括隐藏文件和文件夹的文件夹的全部内容复制到另一个文件夹中,但我想排除某些特定的文件夹。 我怎么能做到这一点?
使用rsync:
rsync -av --exclude='path1/to/exclude' --exclude='path2/to/exclude' source destination
请注意,使用source
和source/
是不同的。 尾部斜线表示将文件夹source
的内容复制到destination
。 如果没有结尾斜杠,则表示将文件夹source
复制到destination
。
或者,如果要排除很多目录(或文件),可以使用--exclude-from=FILE
,其中FILE
是包含要排除的文件或目录的文件的名称。
--exclude
也可能包含通配符,例如--exclude=*/.svn*
使用tar和管道。
cd /source_directory
tar cf - --exclude=dir_to_exclude . | (cd /destination && tar xvf - )
你甚至可以通过ssh使用这种技术。
您可以使用-prune
选项来find
。
来自man find
一个例子man find
:
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.链接地址: http://www.djcxy.com/p/78141.html