0

I want to change file permissions and exclude a folder and it's contents (or only the contents is okay too).

It doesn't seem to work the same way as with chown - beause with chown I can change owner and exklude a folder like this:

sudo find . ! \( -path './images' -o -path './images/*' \)  -exec chown 33:33 {} \;

But when I try to change permissions with the same syntax:

sudo find . ! \( -path '/home/myuser/testdir/three/uploads/*' -o -path 
'/home/myuser/testdir/three/uploads' \) -type f -exec chmod 644 {} \;

That folder was not excluded, all files were affected. And it's not because of the added "-type f", that works with the chown command too.

So I'm wondering what I need to change to make it work with the chmod command?

1 Answer 1

2

This problem does not result from the chmod command but from your find expressions.

In your working example you tests like -path './images' that match the starting directory ., while in your non-working command you use absolute paths for -path with the same relative starting directory.

To see what files match your expressions, run

sudo find . ! \( -path '/home/myuser/testdir/three/uploads/*' -o -path 
'/home/myuser/testdir/three/uploads' \) -type f -print

I suggest to omit all exclude patterns first and then add exclude patterns that actually match the printed paths of the files.

In general, if you want to execute a command that will change something on your system, you should test the find command with -print first until you get the expected list of files before you add the -exec action.


Another option would be to use a matching start directory, e.g.

sudo find /home/myuser/testdir ! \( -path '/home/myuser/testdir/three/uploads/*' -o -path 
'/home/myuser/testdir/three/uploads' \) -type f -print

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .