BASH copy all files except one

LinuxBashTerminalCopyCp

Linux Problem Overview


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?

Linux Solutions


Solution 1 - Linux

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

Solution 2 - Linux

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

Solution 3 - Linux

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

Solution 4 - Linux

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/

Solution 5 - Linux

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

Solution 6 - Linux

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/

Solution 7 - Linux

Below script worked for me

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

Solution 8 - Linux

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

Solution 9 - Linux

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 !

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJoe CannattiView Question on Stackoverflow
Solution 1 - LinuxJonView Answer on Stackoverflow
Solution 2 - LinuxmatjaView Answer on Stackoverflow
Solution 3 - LinuxJohn KugelmanView Answer on Stackoverflow
Solution 4 - LinuxnosView Answer on Stackoverflow
Solution 5 - LinuxWill HartungView Answer on Stackoverflow
Solution 6 - LinuxKai - Kazuya ItoView Answer on Stackoverflow
Solution 7 - LinuxmonofalView Answer on Stackoverflow
Solution 8 - LinuxIvan GrynenkoView Answer on Stackoverflow
Solution 9 - LinuxalexView Answer on Stackoverflow