osx - Setting a user's home directory on Mac OS X (Server) from the command line

  • dnolen

    How can I set a user's home directory on Mac OS X Server using the command line?

  • Answers
  • kiamlaluno

    As you asked how to do it from the command line, I take you can open a SSH session for the Mac OS X server, or you can open a terminal window directly from the server.

    If this is the case, then execute the following command: sudo dscl . -change /Users/<username> NFSHomeDirectory <old-path> <new-path>; replace <username>, <old-path>, and <new-path> with, respectively, the user name, the old home directory, and the new home directory. The command -change requires the old value of the key being changed; if you don't know the old value, then you cannot do anything.

    If you are not using any SSH session, or you cannot open a terminal window in the Mac OS X, but you can still access the server from another Mac, then the command is a little different. The dot after sudo dscl is the datasource is described in the (dscl(1) Mac OS X Manual Page) as the following:

    dscl operates on a datasource specified on the command line. This may be a node name or a Mac OS X Server (10.2 or later) host specified by DNS hostname or IP address. Node names may be absolute paths beginning with a slash ("/"), or relative domain paths beginning with a dot (".") character, which specifies the local domain, or "..", specifying the local domain's parent. If the hostname or IP address form is used then the user must specify the -u option and either the -P of -p options to specify an administrative user and password on the remote host to authenticate with to the remote host. The exception to this is if "localhost" is specified. Passing passwords on the command line is inherently insecure and can cause password exposure. For better security do not provide the password as part of the command and you will be securely prompted.


  • Seasoned Advice (cooking)

    Warning: Experiment with this on an unimportant account or machine first; I have not tested this.

    It can be done. Something like this:

    sudo dscl . -change /Users/$USERNAME NFSHomeDirectory $OLDPATH $NEWPATH  

    Then, of course, to actually move their home directory folder to its new path, if you haven't done so already, use the following:

    sudo mv $OLDPATH $NEWPATH  


    • dscl is the "Directory Services Command Line" utility
    • Don't worry that the key in question is called NFSHomeDirectory; that's a leftover from the heritage of the schema coming from LDAP

    By the way, for this kind of thing, the Mac OS X Server Administration Guides (freely downloadable PDFs) are your friends, especially, in this case, the User Mangement one. It includes both the GUI and command-line ways to do many user management tasks. I believe the Open Directory Administration guide explains the schema.

  • Related Question

    osx - How do I get the size of a Linux or Mac OS X directory from the command-line?
  • Daryl Spitzer

    What command do I use to find the size of all the files (recursively) in a Linux or Mac OS X directory?

  • Related Answers
  • wch

    The BSD version of du used in OS X reports size with 512-byte blocks -- the sizes are essentially rounded up to the next 512-byte value. This tells you the space on disk, which is larger than the amount of data. If you have a lot of small files, the difference can be large.

    Here's an example.

    This is the value with regular du. It's in 512-byte blocks:

    $ du -s
    248   .

    The -h flag results in a more readable number, in kilobytes. As expected, it's half the number of 512-byte blocks:

    $ du -hs
    124K  .

    Finally, you can use find and awk to give you the sum of actual bytes in the files. This is kind of slow, but it works:

    $ find . -type f -exec ls -l {} \; | awk '{sum += $5} END {print sum}'

    This value matches exactly the number reported by Finder's Get Info window. (There are no weird forks or xattrs in this set of files.) It's significantly smaller than the value reported by du.

    Here's how it works: it gets a list of all the files, and passes them to ls -l; then awk is used to count up the bytes. The -type f flag is there so that only files (and not directories) get sent to ls. Without that flag, it'll also send directory names to ls, and each file will be listed twice : once as an individual file, and once as an item in the directory.

    The GNU version of du can give values in actual bytes instead of blocks. It's unfortunate that the BSD version of du is not as flexible.

  • Community

    Show the size of a single file

    du -h path_to_a_file

    Show the size of the contents of a directory, each sub-directory, and each individual file:

    du -h path_to_a_directory

    Show the size of the contents of a directory:

    du -sh path_to_a_directory

  • Acid

    du - tells the disk use not the file size.

    find . -type f -print0 | xargs -0 stat -f%z | awk '{b+=$1} END {print b}'

    above terminal code (im on osx 10.6) offers for me the best result and is waaay faster than "find ... -exec"

    a quick benchmark

    time find . -type f -print0 | xargs -0 stat -f'%z' | awk '{b+=$1} END {print b}'
    real    0m0.086s
    user    0m0.029s
    sys 0m0.073s
    time find . -type f -exec ls -l {} \; | awk '{sum += $5} END {print sum}'
    real    0m18.515s
    user    0m2.929s
    sys 0m9.339s
  • malte

    I combined all your approuches and combined it with a human readable output the result is:

    find $1 -type f -print0 | xargs -0 stat -f'%z' | awk '{b+=$1} END {print b}' | awk '{ sum=$1 ; hum[1024**3]="Gb";hum[1024**2]="Mb";hum[1024]="Kb"; for (x=1024**3; x>=1024; x/=1024){ if (sum>=x) { printf "%.2f %s\n",sum/x,hum[x];break } }}'

    Link to the gist: https://gist.github.com/mlegenhausen/9365461