5

I have moved contents of another drive into my home directory, but the permissions are not what they should be. First, I changed ownership by entering:

  ~$ sudo chown -R USERME /home/USERHOME/

Which I think works now.

But now, I wanted to simply change all of the files and sub-directories into the default permissions, with files being read+write, and directories being r+w+x. I have not found a recursive way of handling this. I have run:

  ~$ sudo chmod 755 -R /home/USERHOME/

To make everything available to me. However, files appear as executables, and this is not what I want. There are plenty more sub-directories and files so I am looking for a simple recursive solution.

Basically, I think I am looking for a recursive way to reapply the default umask parameter to a whole folder with (I think) chmod.


Update:

I have posted a solution thanks to @SkyDan and "changing chmod for files but not directories"

1
  • You do not need the sudo for chmod, you now own the files. Jun 10, 2014 at 12:26

4 Answers 4

6

chmod can do it, you don't need find.

Use symbolic mode and capital X.

chmod -R u=rwX,og=rX directory

alternately to avoid repetition, and make easier to edit. We can made it action orientated, instead of role orientated.

chmod -R a=rX,u+w directory

The capital X tells chmod to apply x to directories, (and if it already has it, if you do for example go+X).

Manual extract:

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. Multiple symbolic modes can be given, separated by commas.

A combination of the letters ugoa controls which users' access to the file will be changed: the user who owns it (u), other users in the file's group (g), other users not in the file's group (o), or all users (a). If none of these are given, the effect is as if a were given, but bits that are set in the umask are not affected.

The operator + causes the selected file mode bits to be added to the existing file mode bits of each file; - causes them to be removed; and = causes them to be added and causes unmentioned bits to be removed except that a directory's unmentioned set user and group ID bits are not affected.

The letters rwxXst select file mode bits for the affected users: read (r), write (w), execute (or search for directories) (x), execute/search only if the file is a directory or already has execute permission for some user (X), set user or group ID on execution (s), restricted deletion flag or sticky bit (t). 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).

2
  • Ok, I tried your answer "chmod -R u=rwX,og=rX /mydirectory" But unfortunately it does not recognize this, it is giving me "chmod: invalid mode: ‘u=rwX,’"
    – briank
    Jun 10, 2014 at 14:06
  • I just been playing with it, I managed to reproduce your error by putting a space after the ,. Don't add a space, the mode has to be a single argument, as typed in the answer. Jun 10, 2014 at 16:18
2

Files and directories usually need different file mode bits, while chmod -R usually applies the same mode to all entries.

To differentiate between files and directories, find is a useful utility.

Change the mode of regular files:

# Finds all regular files and passes them to chmod
find /path/to/dir -type f | xargs chmod u=rw,g=r,o=r

Change the mode of directories:

# Directories require the execution bit to be accessible
find /path/to/dir -type d | xargs chmod u=rwx,g=rx,o=rx

The find utility is itself recursive, so no need to use chmod with the -R flag.

1
  • This assumes that the "normal" was predictable, but in the general case, you can't be sure. For example, if you messed up permissions in /usr you need to painstakingly decide for each file individually what its correct permissions are. A common problem is losing the SUID bit on individual entries which need to have it.
    – tripleee
    Apr 28, 2017 at 12:12
0

If you want to have separate permissions for each file/dir etc you need to find first those items and then do something on them Example:

 find . -type d -name '*spec_dir*' | xargs chmod 755 

So above will search for all subdirectories from current one and then will set permissions as drwxr-xr-x to all found which contains in its name spec_dir.

If you need to add/remove permission without reseting them then use g+/-p format. Example,add only execute permission for other group: o+x

chmod o+x file or in above command context ... | xargs chmod o+x

And same you could do for your files find . -type f -name 'my_files*' ... etc.

0

Update

Thanks to the help of @SkyDan and this unix question here I have found a solution.

I first did

~$ find . -type d -print0 | xargs -0 chmod 755

To change all directories into read, write, executables for the user. Then I did

~$ find . -type f -print0 | xargs -0 chmod 644

To make all of the files read/write for the user.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .