Set bash
’s extglob
option with
shopt -s extglob
and use it like that to exclude these files:
sudo chown -R www-data:www-data !(file1.php)
sudo chown -R ubuntu:ubuntu !(file2.txt|file3.php)
This works with files and directories equally, but only in the current directory. If you need to exclude files in subdirectories, combine the above with the globstar
option (shopt -s globstar
):
sudo chown -R www-data:www-data **/!(file1.php)
This matches every file and directory except the ones called file1.php
.
Example run
$ touch {1..3}
$ shopt -s extglob
$ echo !(1)
2 3
$ echo !(1|2)
3
$ mkdir a
$ touch a/{1..3}
$ tree
.
├── 1
├── 2
├── 3
└── a
├── 1
├── 2
└── 3
$ shopt -s globstar
$ echo **/!(1|2)
3 a a/3
Further reading