How to use 'cp' command to exclude a speci

2019-01-07 01:28发布

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?

标签: linux cp
17条回答
Evening l夕情丶
2楼-- · 2019-01-07 02:02
cp -r `ls -A | grep -v "c"` $HOME/
查看更多
Animai°情兽
3楼-- · 2019-01-07 02:05

rsync

rsync -r --verbose --exclude 'exclude_pattern' ./* /to/where/

and first try it with -n option to see what is going to be copied

查看更多
▲ chillily
4楼-- · 2019-01-07 02:06

This is a modification of Linus Kleen's answer. His answer didn't work for me because there would be a . added in front of the file path which cp doesn't like (the path would look like source/.destination/file).

This command worked for me:

find . -type f -not -path '*/exlude-path/*' -exec cp --parents '{}' '/destination/' \;

the --parents command preserves the directory structure.

查看更多
劫难
5楼-- · 2019-01-07 02:07

I assume you're using bash or dash. Would this work?

shopt -s extglob  # sets extended pattern matching options in the bash shell
cp $(ls -laR !(subdir/file1|file2|subdir2/file3)) destination

Doing an ls excluding the files you don't want, and using that as the first argument for cp

查看更多
太酷不给撩
6楼-- · 2019-01-07 02:08

One way would be to use find -0 with -prune to find all the files and directories you want to move, then use xargs -0 to copy each one.

查看更多
我只想做你的唯一
7楼-- · 2019-01-07 02:09

It's relative to the source directory.
This will exclude the directory source/.git from being copied.

rsync -r --exclude '.git' source target
查看更多
登录 后发表回答