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条回答
等我变得足够好
2楼-- · 2019-01-07 02:13

Expanding on mvds’s comment, this works for me

cd dotfiles
tar -c --exclude .git --exclude README . | tar -x -C ~/dotfiles2
查看更多
【Aperson】
3楼-- · 2019-01-07 02:16

The easiest way I found, where you can copy all the files excluding files and folders just by adding their names in the parentheses:

shopt -s extglob
cp -r !(Filename1 | FoldernameX | Filename2) Dest/
查看更多
淡お忘
4楼-- · 2019-01-07 02:17

Why use rsync when you can do:

find . -type f -not -iname '*/not-from-here/*' -exec cp '{}' '/dest/{}' ';'

This assumes the target directory structure being the same as the source's.

查看更多
Juvenile、少年°
5楼-- · 2019-01-07 02:18

I use a "do while" loop to read the output of the find command. In this example, I am matching (rather than excluding) certain patterns since there are a more limited number of pattern matches that I want than that I don't want. You could reverse the logic with a -not in front of the -iname flags:

    find . -type f -iname "*.flac" -o -print0 -iname "*.mp3" -print0 -o -iname "*.wav" -print0 -o -iname "*.aac" -print0 -o -iname "*.wma" -print0 | while read -d $'\0' file; do cp -ruv "$file" "/media/wd/network_sync/music/$file"; done

I use the above to copy all music type files that are newer on my server than the files on a Western Digital TV Live Hub that I have mounted at /media/wd. I use the above because I have a lot of DVD files, mpegs, etc. that I want to exclude AND because for some reason rsync looks like it is copying, but after I look at the wd device, the files are not there despite no errors during the rsync with this command:

    rsync -av --progress --exclude=*.VOB --exclude=*.avi --exclude=*.mkv --exclude=*.ts --exclude=*.mpg --exclude=*.iso --exclude=*ar --exclude=*.vob --exclude=*.BUP --exclude=*.cdi --exclude=*.ISO --exclude=*.shn --exclude=*.MPG --exclude=*.AVI --exclude=*.DAT --exclude=*.img --exclude=*.nrg --exclude=*.cdr --exclude=*.bin --exclude=*.MOV --exclude=*.goutputs* --exclude=*.flv --exclude=*.mov --exclude=*.m2ts --exclude=*.cdg --exclude=*.IFO --exclude=*.asf --exclude=*.ite /media/2TB\ Data/data/music/* /media/wd/network_sync/music/
查看更多
家丑人穷心不美
6楼-- · 2019-01-07 02:20

Just move it temporally into a hidden directory (and rename it after, if wanted).

mkdir .hiddendir
cp * .hiddendir -R
mv .hiddendir realdirname
查看更多
登录 后发表回答