views:

1264

answers:

5

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

+8  A: 

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
Jon
This produces-bash: !: event not found
Joe Cannatti
Are you copying the files to a folder nested within the folder your copying from?
Jon
This requires `shopt -s extglob` to work, if it has been disabled.
Barry Kelly
I don't see why that would be an issue ordinarily, but thanks for pointing it out...
Jon
Works great now. I did not have glob enabled.
Joe Cannatti
+2  A: 

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/

nos
grep -v is your friend
George Jempty
+4  A: 

Simple, if src/ only contains files:

find src/ ! -name Default.png -exec cp {} 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 {} dest/ +

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

find src/ -type f -maxdepth 1 ! -name Default.png -exec cp {} dest/ +
John Kugelman
This turns out to be the best for me because it would appear that OS X ships without glob enabled.
Joe Cannatti
+1  A: 

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

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

matja
A: 
cp `ls | grep -v Default.png` destdir
Will Hartung
This is more likely to produce errors than do what you intend.
Dennis Williamson