How do I exclude a folder when performing file operations i.e. cp, mv, rm and chown etc. in Linux


Question

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.

1
11
6/1/2017 2:49:42 PM

Accepted Answer

If you're using bash and enable extglob via shopt -s extglob then you can use !(<pattern>) to exclude the given pattern.

18
1/14/2010 4:09:10 PM

find dir_to_start -name dir_to_exclude -prune -o -print0 | xargs -0 chown owner

find dir_to_start -not -name "file_to_exclude"  -print0 | xargs -0 chown owner

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon