cp
has no exclude option. You can either:
- Use
rsync
with --exclude
like wjazz suggest.
- Use your shell's special glob operation if it has one.
Both Bash and Zsh have special patterns to match everything except X, but they require enabling extended globbing first.
Bash
In an interactive shell, extended globbing is enabled by default on most distributions. However, in an interpreted script it's not, so first it has to be enabled with:
shopt -s extglob
Then, use the !()
syntax to match everything except what's inside the parentheses:
# "file.txt" is the file we want to exclude
cp !(file.txt) destination
# multiple files can be excluded by using the pipe alternation
cp !(file1.txt|file2.txt) destination
See bash(1)
manual page (section Pattern Matching) or (bash)Pattern Matching
info page, for more matching operators.
Zsh
Zsh doesn't enable extended globs by default in neither interactive nor interpreted shells. To enable it, use:
setopt EXTENDED_GLOB
The syntax in Zsh is to simply a prefix the pattern with ^
:
# "file.txt" is the file we want to exclude
cp ^file.txt destination
# multiple files can be excluded by using parentheses and pipe alternation
cp ^(file1.txt|file2.txt) destination
See zshexpn(1)
manual page, section FILENAME GENERATION for more glob operations.