Change ownership of home directory

Joined
Feb 1, 2018
Messages
2
Reaction score
0
Points
1
In a shell script (running as a launch daemon, thus as root), I'm trying to transfer ownership of a home directory from a local user to a network user.

Stripped of all the logging and debugging code, here's what the script does:

----
# $sourcedir contains the old directory name (from the old, local user)
# $userdir contains the new directory name (from the new, network, user)
# $tgtgu contains the user:group specifier for the network user (like 3953253:20)

# Remove any conflicting old directory
rm -R $userdir || true

# Rename the old directory to the new name
mv -f "$sourcedir" "$userdir"

# Change the ownership
chown -R $tgtgu \"$userdir\"
----

The 'chown' statement claims that '$userdir' does not exist at that point. Part of the debugging code confirms that the directory does, in fact, exist.

Does anyone know why I would not be able to change the owner of the newly-renamed directory, or why chown would report that it does not exist?
 
Joined
Oct 16, 2010
Messages
17,494
Reaction score
1,541
Points
113
Location
Brentwood Bay, BC, Canada
Your Mac's Specs
2011 27" iMac, 1TB(partitioned) SSD, 20GB, OS X 10.11.6 El Capitan
Change ownership of home directory


Just a thought and without knocking any programmers that might be here, but I think you might get a better response at one of the Mac programmers sites or forums.




- Patrick
======
 
OP
E
Joined
Feb 1, 2018
Messages
2
Reaction score
0
Points
1
You may be right; my impression was that most of those were more concerned with Obj-C and Swift than shell scripting. I'll give it a try.
 

chscag

Well-known member
Staff member
Admin
Joined
Jan 23, 2008
Messages
65,248
Reaction score
1,833
Points
113
Location
Keller, Texas
Your Mac's Specs
2017 27" iMac, 10.5" iPad Pro, iPhone 8, iPhone 11, iPhone 12 Mini, Numerous iPods, Monterey
Not helpful Patrick. We have several folks who are members and know programming and lets not forget one of our Staff Moderators is professional programmer.
 
Joined
Oct 16, 2010
Messages
17,494
Reaction score
1,541
Points
113
Location
Brentwood Bay, BC, Canada
Your Mac's Specs
2011 27" iMac, 1TB(partitioned) SSD, 20GB, OS X 10.11.6 El Capitan
Not helpful Patrick. We have several folks who are members and know programming and lets not forget one of our Staff Moderators is professional programmer.


Well Charles, I'm sure the OP will gladly welcome any useful programmer's comments or input if and when they might reply.

Personally, I wasn't even sure what a 'shell script" was until I checked:
https://developer.apple.com/library...ShellScripting/Introduction/Introduction.html

And it seems to be a bit more than any HyperCard language I dare say, which was about the last time I did any "programming". :Smirk:




- Patrick
======
 

Raz0rEdge

Well-known member
Staff member
Moderator
Joined
Jul 17, 2009
Messages
15,745
Reaction score
2,071
Points
113
Location
MA
Your Mac's Specs
2022 Mac Studio M1 Max, 2023 M2 MBA
Assuming this is a BASH shell script, do you have appropriate checks around the various functions? That is, I would do the chown in the following way
Code:
if [ -e $userdir ]; then
    chown -R $tgtgu $userdir
else
    echo "$userdir doesn't exist"
fi

This should ensure that the folder exists before you try to change ownership. I assume you have error checking in place but have provided a streamlined version of your commands for input.

Also, I'm confused by your use of quotes and escaped quotes in the script since those are variables to commands and not being printed, so not sure why you are using those.
 
Joined
Apr 16, 2016
Messages
1,096
Reaction score
51
Points
48
Location
CT
Your Mac's Specs
MacBook Air Mid-2012 / iMac Retina 5K Late-2014
Ownership of a file or directory is based on the UID of the user. All UID's are "local" to the machine, even for network users.

So, the first thing you need to do is to understand how your network users will be representing themselves to the local machine. For example: When I share directories on my Linux machines, I use SAMBA. And, as part of SAMBA, I "map" usernames in the SAMBA software to usernames on the local machine. The local username has an associated UID with it, and THAT'S the UID that I have to ensure owns their home directory.

Also, it's pretty important to ensure that UID's stay consistent across various machines on a network so that you always use the same UID no matter where you are. That way, if drives are moved around or sharing changes, the numbers remain the same and so does the ownership.

When changing ownership, you use the CLI utility "chown". It supports a username as a parameter, but can also take the UID directly. Use either "chown rsmith" or "chown 1029" or whatever the name or UID is.

You should also be looking to do a recursive ownership change all the way down the tree, so the -R parameter probably makes sense. If default group membership will be different, then you should look to whether or not changing that would be necessary.

If you're purely transferring ownership of the directory structure from old id to new id, there is no need to copy or to delete. If the username is changing, consider updating the record for the user so that things like ~/<user> will work properly.
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top