Joe Cannatti
Joe Cannatti

Reputation: 5079

BASH copy all files except one

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?

Upvotes: 153

Views: 154408

Answers (9)

monofal
monofal

Reputation: 1968

Below script worked for me

cp -r `ls -A | grep -v 'skip folder/file name'` destination

Upvotes: 1

Jan, 2022 Update:

This is the easiest way(It's not complicated).

First, make "temp" folder:

mkdir temp

Second, copy all files from your original folder to "temp" folder:

"-R" flag can copy exactly all files including "Symbolic Links"

cp -R originalFolder/. temp/

Third, remove "Default.png" from "temp" folder:

rm temp/Default.png

Finally, copy all files from "temp" folder to your destination folder:

cp -R temp/. destinationFolder/

In addition, this is the shortest way without "temp" folder:

cp -R originalFolder/!(Default.png) destinationFolder/

Upvotes: 1

alex
alex

Reputation: 1

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 !

Upvotes: -3

John Kugelman
John Kugelman

Reputation: 361595

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/ {} +

Upvotes: 69

matja
matja

Reputation: 4169

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

Upvotes: 101

nos
nos

Reputation: 229088

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/

Upvotes: 2

Ivan Grynenko
Ivan Grynenko

Reputation: 1

# chattr +i /files_to_exclude
# cp source destination
# chattr -i /files_to_exclude

Upvotes: -1

Will Hartung
Will Hartung

Reputation: 118611

cp `ls | grep -v Default.png` destdir

Upvotes: 4

Jonathan Holloway
Jonathan Holloway

Reputation: 63672

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

Upvotes: 199

Related Questions