Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

So, you can use the * as a wild card for all files when using cp within context of a directory. Is there a way to copy all files except x file?

share|improve this question

4 Answers 4

up vote 22 down vote accepted

In bash you can use extglob:

 $ shopt -s extglob  # to enable extglob
 $ cp !(b*) new_dir/

where !(b*) exclude all b* files.

You can later disable extglob with

 $ shopt -u extglob
share|improve this answer
Do you know if there's something equivalent for the tcsh shell? –  Levon Jun 27 '12 at 1:33
Unfortunately I don't. Seems like find is the only way in tcsh: find . -maxdepth 1 ! -name "exclude*" -exec cp -t destination {} \+ –  rush Jun 27 '12 at 5:25

Rsync handles this nicely.

Example copy all: rsync -aP /folder1/* /folder/2

Example copy all with exclusion: rsync -aP --exclude=x /folder1/* /folder2/

The -aP switch:

a: Similar to cp -a, recursive, etc. P: Shows progress, a nice feature of rsync.

share|improve this answer
And see this answer for a short guide to common rsync exclusion lists. –  Gilles Jun 26 '12 at 22:27

This isn't a feature of cp, it's a feature of your shell (it expands the * to mean all non-dot files), so the answer depends on which shell you're using. For example, zsh supports this syntax:

$ cp ^x /path/to/destination

Where ^x means "all files except x"

You can also combine selection and de-selection patterns, e.g. to copy all wav files except those containing xyz, you can use:

cp *.wav~*xyz*
share|improve this answer
Any comments on bash? –  hydroparadise Jun 26 '12 at 19:59
@hydroparadise I don't know much about bash, but this answer seems to cover it –  Michael Mrozek Jun 26 '12 at 20:00
What about tcsh? Anyone know? –  Levon Jun 27 '12 at 1:32

Could also be done in plain old (portable/compatible) bourne shell in a variety of ways with standard tools in a lot less elegant ways than using advanced shell globbing or commands with built-in-exclusion options.

If there are not too many files, this could be a way:

cp `ls | egrep -v '^excludename$'` destdir/.

Sure, bash and GNU tools are great and powerful, but they're still not always available. If you intend to put it in a portable script, I would recommend find as in the comment by Rush.

share|improve this answer
I find that the last part of your answer just distracts from the topic at hand. Besides, "Unix" isn't the gold standard anymore (if it ever were). It just isn't that relevant if something is "Unix" or not anymore, despite the title of this site being "Unix and Linux". –  Alexander Jun 27 '12 at 8:44
OK. I moved the comment to here instead: Unix is not GNU. I agree that the "unixness" of things is not very interesting, but I still believe in portability and knowing a bit about your history. –  MattBianco Jun 27 '12 at 8:55
Fully agree with you there. –  Alexander Jun 27 '12 at 8:57

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.