1

so i have this startup script i run after re OSing my server:

  • Installs drivers
  • Mounts Zpool
  • Adds users and smb users and SMB shares.

The only things that survives the new OS is my Zpool of which most files are owned by bob lets say and some files in my docker volume folder are owned by root , because sensitive stuff.

In my script i sudo chown -R all the files in my Zpool BUT i found out i don't want to do that because of the root-owned files and dir. of my docker volumes which i want to keep the ownership intact.

So what I really want is to change the ownership of the files owned by my example "bob" user from what it was previously to what it is now, without changing my files owned by "root" in this specific folder.

  • How can i chown all the files in my Zpool and skip entirely a specific folder inside?
  • Do i even need to chown all the files if my username doesn't change between new installs of the same OS?
  • What about different OS?

Thanks guys

2
  • Are you asking how to change the ownership only for files owned by "old" bob, leaving all others untouched? Commented Feb 2, 2022 at 9:31
  • 1
    yeah that would do it
    – nope none
    Commented Feb 2, 2022 at 11:34

3 Answers 3

2
sudo find /zpool -path /zpool/some/dir/to/exclude -prune -o \
  -exec chown -h someuser:somegroup {} +

With GNU chown, you can also chown only the files that are owned by a given user:group with:

sudo chown -Rh --from=user:group someuser:somegroup /zpool

(or --from=user for files owned by user regardless of group or --from=:group for files group-owned by group regardless of user).

The standard equivalent would be:

sudo find /zpool -user user -group group \
  -exec chown -h someuser:some group {} +
3
  • thanks a lot guy. there really is a lot of stuff built in to usual apps. damn I didn't know this stuff. But now that i read all your response, i also understand that i dont even need to do all that stuff if i just keep the same uid between new OSs. but still. imma try some of of those commands on my test pi.
    – nope none
    Commented Feb 2, 2022 at 11:43
  • @nopenone remember that you can check functionality for a particular tool in the man pages (man chown, for example) Commented Feb 2, 2022 at 11:51
  • Yes thanks. just gotta think outside the box to combine the functions.
    – nope none
    Commented Feb 4, 2022 at 0:31
1

you can try the -prune operator in find:

It sounds a bit scary, but what it does is to prune find's processing

again, if your files are in /zpool, this should work

find /zpool -user root -prune -o -exec chown user:group {} \;

This should change all your files that are not owned by root to owned by user:group. Just to make sure, you can run

find /zpool -user root -prune -o -exec echo {} ;\

to see that you get the files you expect.

The -o is find's or operator that in this case works as "if the previous does not match does what follows"

You ask two different questions, if you want to change owner of everything except for what is owned by root, the find command should work. But you also mentions everything except what is in a given subdirectory, in that case you would do a

find /zpool -name dir_with_secret_stuff -prune -o -exec chown user:group {} ;

The username on files is really not connected to the username, but to the numeric user id, so if you have a disk with files owned by user 1000 with name bob, ls -l will list them as owned by bob. If you mount that disk on a system where user 1000 is named nobob, ls -l will list them as owned by nobob, and if you mnount it on a system where there is no user 1000, they will just show up as owned by user 1000. So if your username and userid stays the same, the apperant file ownerships will stay the same.

0

If the folder can be any place within your directory hierarchy, I think you would have to operate on all your files and directories individually.

Say your Zpool-thing is mounted on /zpool and the directory you want to exclude is /zpool/subdirectory/secret stuff, then maybe something like this could work?

for item in "$(find /zpool -print | grep -v '/zpool/sudirectory/secret stuff')"; do
    sudo chown user:group "$item"
done

Where you substitute user and group with the user and group you want to use, of course :-)

The find bin -print | grep -v '/zpool/sudirectory/secret stuff'will give you a list of all files and folders in '/zpool', except the ones in the "secret stuff" directory, and then you just chown all the items in the list.

And with respect to the username question; it's the user ID (UID) that matters. Check with id. If the UID doesn't change you're good.

1

You must log in to answer this question.

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