What does chmod -u do

chmodpermissions

By accident I ran chmod -u filename and it removed all of the permissions I had on filename.

The man page does not reference a -u option. Experimenting I was able to conclude that it removes not all permissions, but just read and execute access, leaving write access intact.

So what does this do exactly?


My conclusion above is wrong, I now think that what it does is remove the permissions that the owner has, from all categories.


I think the behavior is analogous to a=u, only it is - instead of = and a can be dropped just as it can with, for instance, a+x.

Best Answer

This is not an option, but a standard (but uncommon) way of specifying the permissions. It means to remove (-) the permissions associated with the file owner (u), for all users (no preceding u, g, or o). This is documented in the man page.

GNU chmod's man page documents this as:

The format of a symbolic mode is [ugoa...][[-+=][perms...]...], where perms is either zero or more letters from the set rwxXst, or a single letter from the set ugo

and later

Instead of one or more of these letters, you can specify exactly one of the letters ugo: the permissions granted to the user who owns the file (u), the permissions granted to other users who are members of the file's group (g), and the permissions granted to users that are in neither of the two preceding categories (o)

So -u means to remove (-) whatever permissions are currently enabled for the owner (u) for everybody (equivalently to a-u, except honouring the current umask). While that's not often going to be very useful, the analogous chmod +u will sometimes be, to copy the permissions from the owner to others when operating recursively, for example.


It's also documented in POSIX, but more obscurely defined: the permission specification is broadly who[+-=]perms (or a number), and the effect of those are further specified:

The permcopy symbols u, g, and o shall represent the current permissions associated with the user, group, and other parts of the file mode bits, respectively. For the remainder of this section, perm refers to the non-terminals perm and permcopy in the grammar.

and then

-

... If who is not specified, the file mode bits represented by perm for the owner, group, and other permissions, except for those with corresponding bits in the file mode creation mask of the invoking process, shall be cleared.

Related Question