Writing data into existing home folders as part of an installation package

I have been in a few scenarios where we need to deploy a package and put a file into the home folder of each user on the target system. Of course if we are using Casper we can just create a DMG package and use the “fill existing users” option. The below script gives you a way of doing this if you don’t have Casper deploying the package.

We start off by deploying the app / software via the package as normal. In this example we put the file we want to end up in the users home folders in /tmp. We then use a while loop to copy the file into each users home folder (excluding the Shared and Guest directories):


     # Sets the file name as a variable

counter=`ls /Users | grep -v Shared | grep -v Guest | grep -v .localized | grep -v .DS_Store | grep -c "[A-z 0-9]"`
     # Outputs the number of folders in the /Users directory, excluding the Shared & Guest directories

# Loop start

     while [ $counter -ne 0 ]
               targetFolder=`ls /Users | grep -v Shared | grep -v Guest | grep -v .localized | grep -v .DS_Store | grep "[A-z 0-9]" | head -$counter | tail -1`
                    # Gets the target folder name (there’s probably a better way to do this but it works!)

               cp $fileName /Users/$targetFolder/Desktop/
                    # Copies the file into place

               chown $targetFolder /Users/$targetFolder/Library/Preferences/ByHost/com.apple.QuickTime.$uuid.plist
                    # Set the correct owner on the file

               counter=$(( $counter - 1 ))
                    # Reduces the counter by 1

exit 0

5 Replies to "Writing data into existing home folders as part of an installation package"

  • Graham Gilbert

    What about users that don’t have their home directory in /Users? It would be better to use dscl to get each user’s home directory, or even better still, copy the files on login using a tool like outset or scriptrunner – then you catch all existing and future users.

  • I agree, this is a basic fix for a specific scenario. DSCL would be a good option as this script assumes (or only catches) home folders in /Users. I thought I’d share it in case it helps someone out!

  • Just to clean the script up a little, you could use:

    targetFolder=`ls /Users | grep -v -E ‘grep -v ‘Shared|Guest|.localized|.DS_Store’ | grep “[A-z 0-9]” | head -$counter | tail -1`

    instead of

    targetFolder=`ls /Users | grep -v Shared | grep -v Guest | grep -v .localized | grep -v .DS_Store | grep “[A-z 0-9]” | head -$counter | tail -1`

    The -E option lets you put multiple search patterns into one command so it makes it a bit shorter.

  • Oops…

    * it should read:

    targetFolder=`ls /Users | grep -v -E ‘Shared|Guest|.localized|.DS_Store’ | grep “[A-z 0-9]” | head -$counter | tail -1`
  • Bryan Vines

    I agree with David Acland with regard to using DSCL to determine users’ home directories.

    You can get a list of users this way, filtering out root, daemon, nobody, Guest, and system users whose names begin with an underscore:
    dscl . list /users | grep -v “^_” | grep -v “^root” | grep -v “^daemon” | grep -v “^nobody” | grep -v “^Guest”

    Then you can loop through the list of usernames, asking DSCL for the home directory:
    dscl . read /users/$userName/ NFSHomeDirectory

    You’ll want to slice the “NFSHomeDirectory: ” text from that command’s result to get the path to that particular user’s home.

Leave a Reply

Your email address will not be published. Required fields are marked *