How to use 'cp' command to exclude a specific directory?

I want to copy all files in a directory except some files in a specific sub-directory. I have noticed that 'cp' command didn't have a --exclude option. So, how can I achieve this?


rsync is fast and easy:

rsync -av --progress sourcefolder /destinationfolder --exclude thefoldertoexclude

You can use --exclude multiples times.

Also you can add -n for dry run to see what will be copied before performing real operation, and if everything is ok, remove -n from command line.


Well, if exclusion of certain filename patterns had to be performed by every unix-ish file utility (like cp, mv, rm, tar, rsync, scp, ...), an immense duplication of effort would occur. Instead, such things can be done as part of globbing, ie by your shell.

bash

Link to manual, search for extglob .

Example:

$ shopt -s extglob
$ echo images/*
images/004.bmp images/033.jpg images/1276338351183.jpg images/2252.png
$ echo images/!(*.jpg)
images/004.bmp images/2252.png

So you just put a pattern inside !() , and it negates the match. The pattern can be arbitrarily complex, starting from enumeration of individual paths (as Vanwaril shows in another answer): !(filename1|path2|etc3) , to regex-like things with stars and character classes. Refer to the manpage for details.

zsh

Link to manual, section "filename generation" .

You can do setopt KSH_GLOB and use bash-like patterns. Or,

% setopt EXTENDED_GLOB
% echo images/*
images/004.bmp images/033.jpg images/1276338351183.jpg images/2252.png
% echo images/*~*.jpg
images/004.bmp images/2252.png

So x~y matches pattern x , but excludes pattern y . Once again, for full details refer to manpage.


fishnew!

The fish shell has a much prettier answer to this:


                        链接地址: http://www.djcxy.com/p/13690.html
                        

上一篇: 令人惊叹的音频引擎如何将滤波器应用于麦克风输入

下一篇: 如何使用'cp'命令排除特定目录?