Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

How do you exclude a folder when performing file operations i.e. cp etc.

I would currently use the wild card * to apply file operation to all, but I need to exclude one single folder.

The command I'm actually wanting to use is chown to change the owner of all the files in a directory but I need to exclude one sub directory.

share|improve this question
Cross-posted here: stackoverflow.com/questions/2065447/… –  Dennis Williamson Jan 14 '10 at 16:27

3 Answers 3

up vote 7 down vote accepted

Inverse Globbing:
You want an inverse match of a glob, I would do it like the following:

You can do an inverse match with a newer bash if you enable extended globbing. For example, to match everything that doesn't have foo or bar in the name:

shopt -s extglob
echo !(*foo*|*bar*)

Or just everything that doesn't have foo:

shopt -s extglob
echo !(*foo*)

You could also use find (this is the most robust option I think), and use ! to negate a match, and then run the command with xargs -0:

find . ! -iname 'foo' -print0 | xargs -0 echo

Just mv the folder somewhere else, do what you need to do, and put it back :-)

share|improve this answer

rsync -arv --exclude={files} {Destination}

share|improve this answer

rm $(ls -1 * | grep -v whatever)

share|improve this answer

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.