I made a backup to an NTFS drive, and well, this backup really proved necessary. However, the NTFS drive messed up permissions. I’d like to restore them to normal w/o manually fixing each and every file.
One problem is that suddenly all my text files gained execute permissions, which is wrong ofc. So I tried:
sudo chmod -R a-x folder with restored backup/
But it is wrong as it removes the x permission from directories as well which makes them unreadable.
What is the correct command in this case?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
If you are fine with setting the execute permissions for everyone on all folders:
chmod -R -x+X *
The -x removes execute permissions for all
The +X will add execute permissions for all, but only for directories.
See below for a solution that uses find to really not touch folders as requested.
Method 2
Ok, I re-read the “chmod” man pages for Mac OS X, BSD, and Linux, and did a few experiments. Here is what I learned about symbolic modes. It can get complicated, but it’s worth understanding:
- The general form is clause[,clause…] where:
- clause := [ugoa][+-=][rwxXstugo]
- [ugoa] (who) (specify multiple) means set the permission for user, group, other, or all. If not specified, the default is ‘a’, but the umask is in effect.
- [+-=] (action) (specify one) means:
- + means add the specified permissions to the permissions already in effect
- – means remove the specified permissions from the permissions already in effect
- = means set the permissions to the specified permissions, clearing all others
- [rwxXstugo] (permission) (specify multiple of rwxXst OR one of ugo) sets the permissions for the specified user(s) as follows:
- r — read
- w — write
- x — execute/search
- X — execute/search iff directory OR any execute bit was already set.
- s — suid or sgid
- t — sticky
- u — copy user permission
- g — copy group permission
- o — copy other permission
So for example, a+x would make a file executable by everybody. a+X would make a file executable by everybody IF it had been executable by anybody.
a+x would make a directory searchable by everybody. a+X would also make a directory searchable by everybody.
The key difference between BSD and Linux is that with BSD, the determination is made based on the file’s permissions before chmod was executed. While with Linux, the determination is made immediately before the +X clause is executed.
So with BSD, the combination a-x,a+X would remove execute/search permission and then make a directory searchable by everybody, and make a file executable by everybody if it had originally been executable by anybody.
With Linux, a-x,a+X would remove execute/search permission and then make a directory searchable by everybody, while leaving a file executable by nobody.
Here’s a concrete example: on a BSD machine: a directory, an executable file, and a non-executable file:
drwxr-x--- 2 falk staff 68 Jul 19 18:01 fee/ -rwxr-x--- 1 falk staff 0 Jul 19 18:01 fie* -rw-r----- 1 falk staff 0 Jul 19 18:01 foe
Observe that both the directory and “fie” are executable/searchable by the user, but not by others.
Now we execute chmod a-x,a+X *. The first clause will strip the execute/search bit from all users for all files, but the second clause will add it back for both “fee” and “fie”. “fee” because it’s a directory, and “fie” because it had at least one executable bit to start with.
drwxr-x--x 2 falk staff 68 Jul 19 18:01 fee/ -rwxr-x--x 1 falk staff 0 Jul 19 18:01 fie* -rw-r----- 1 falk staff 0 Jul 19 18:01 foe
I had the same result executing chmod -x+X.
Conclusion: Jak Gibb’s solution will work on Linux, but for BSD, you would need to make two passes.
I didn’t test this on SVr4 or other Unix variants.
Method 3
One way to do it:
find backup -type f -exec chmod 0644 {} +
Method 4
find backup ! -type l ! -type d -exec chmod a-x {} +
Would remove execute permission to files that are not of type directory (as you requested) nor symbolic link (symlinks are generally always rwxrwxrwx, and chmod would affect the target of the symlink on them).
Note that:
find backup -type f -exec chmod a-x {} +
would only change the permission of regular files. That would exclude directories and symlinks, but also devices, named pipes, sockets and possibly others depending on the system (though in the OP’s case of a backup of a NTFS file system, those would be unlikely to be present).
Method 5
In zsh, use the glob qualifier . to match only regular files (not including symbolic links), and D to match dot files:
sudo chmod a-x folder with restored backup/**/*(D.)
If the command line is too long, you can use zargs:
zargs -- folder with restored backup/**/*(D.) -- sudo chmod a-x
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0