MacOS – How to change a directory name

filesystemmacosterminal

My home directory was not where it was supposed to be. I was able to fix the directory location. Then needed to fix the permission of home directory with

$ cd /Users/
$ sudo chown -R hook1:staff /Users/hook1
chown: /Users/hook1: no file or directory
$ ls -F
Shared/    chris/      hook1???/

Home directory incorrectly appears as hook1???/ not hook1/
Is there a reason for this?

Best Answer

If what you posted was literally what the directory name is, then this should work:

cd /Users
mv hook1\?\?\? hook1

The problem, as fd0 points out, is that you have invisible/unprintable characters in your directory name. Use basic, non-destructive shell commands to isolate the directory name using wildcards, then rename it. For example, try this:

cd /Users
ls -ld h*

That should result in exactly one directory being listed. If it does not, continue to add letters before the * in order to create a wildcard mask that produces exactly one result:

ls -ld ho*     # produces 3 results
ls -ld hoo*    # produces 1 result

If you get to a point where you go from more than one result to zero results, then you've stumbled upon another invisible character. Back up one letter, add a question mark then re-add the letter.

ls -ld ho*     # produces 3 results
ls -ld hoo*    # produces 0 results
ls -ld ho?o*   # produces 1 result

If you still have problems, play around with more * and ? until you have something that produces exactly one result:

ls -ld ho*     # produces 3 results
ls -ld hoo*    # produces 0 results
ls -ld ho?o*   # produces 0 results
ls -ld ho*o*   # produces 1 result

Once you have that, then use that in your mv command:

mv ho*o* hook1

If none of this works, please post the results of you ls -ld experiments.