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
bash you can use
$ shopt -s extglob # to enable extglob $ cp !(b*) new_dir/
!(b*) exclude all
You can later disable
$ shopt -u extglob
Rsync handles this nicely.
Example copy all:
rsync -aP /folder1/* /folder/2
Example copy all with exclusion:
rsync -aP --exclude=x /folder1/* /folder2/
a: Similar to
cp -a, recursive, etc.
P: Shows progress, a nice feature of rsync.
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
^x means "all files except
You can also combine selection and de-selection patterns, e.g. to copy all wav files except those containing xyz, you can use:
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 (and not with names including spaces and/or linebreaks), this could be a way:
cp `ls | egrep -v '^excludename$'` destdir/.
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.
If you want to copy everything in a folder (including subfolders) to a particular sub-directory:
cp -R $(ls | grep -v '^subdir$') subdir/
Works with sh, bash, zsh (at least).
extglob is the best way so far I guess.
Another way is using
cp $(ls --ignore=x) subdir/