129
votes

I would like to copy all files out of a dir except for one named Default.png. It seems that there are a number of ways to do this. What seems the most effective to you?

7
Why do you need it to skip that file, as opposed to just deleting it after copying it? Does it exist in the target directory already?Lasse V. Karlsen
Yes a file with the same name is already living in the target dir.Joe Cannatti
@LasseV.Karlsen: Or you could want to save the time of copying it, if it's a large file. I'm interested in this but excluding a directory rather than a file.Nikana Reklawyks

7 Answers

170
votes

Should be as follows:

cp -r !(Default.png) /dest

If copying to a folder nested in the current folder (called example in the case below) you need to omit that directory also:

cp -r !(Default.png|example) /example
84
votes

rsync has been my cp/scp replacement for a long time:

rsync -av from/ to/ --exclude=Default.png

-a, --archive               archive mode; equals -rlptgoD (no -H,-A,-X)
-v, --verbose               increase verbosity
61
votes

Simple, if src/ only contains files:

find src/ ! -name Default.png -exec cp -t dest/ {} +

If src/ has sub-directories, this omits them, but does copy files inside of them:

find src/ -type f ! -name Default.png -exec cp -t dest/ {} +

If src/ has sub-directories, this does not recurse into them:

find src/ -type f -maxdepth 1 ! -name Default.png -exec cp -t dest/ {} +
2
votes
cp `ls | grep -v Default.png` destdir
2
votes

I'd just do:

cp srcdir/* destdir/ ; rm destdir/Default.png

unless the files are big. Otherwise use e.g.

find srcdir -type f/ |grep -v Default.png$ |xargs -ILIST cp LIST destdir/
-1
votes
# chattr +i /files_to_exclude
# cp source destination
# chattr -i /files_to_exclude
-3
votes

use the shell's expansion parameter with regex

cp /<path>/[^not_to_copy_file]* .

Everything will be copied except for the not_to_copy_file

-- if something is wrong with this. please Specify !