There's no magic bullet here. The permissions carry information which is not always redundant.
If you'd done this in a system directory, your system would be in a very bad state, because you'd have to worry about setuid and setgid bits, and about files that are not supposed to be world-readable, and about files that are supposed to be group- or world-writable.
In a per-user directory, you have to worry about files that aren't supposed to be world-readable. No one can help you there.
As for executability, a good rule of thumb would be to make everything that doesn't look like it could be executed, be nonexecutable. The kernel can execute scripts whose first two bytes are #!
, ELF binaries whose first four bytes are \x7fELF
where \x7f
is the byte with the value 12, and a few rarer file types (a.out, anything registered with binfmt_misc
). Hence the following command should restore your permissions to a reasonable state (assumes bash 4 or zsh, otherwise use find
to traverse the directory tree; warning, typed directly into the browser):
for x in **/*; do
if ! [ -f "$x" ]; then continue; fi # skip all but regular files
case $(head -c 4 "$x") in
"#!"??) :;; # skip script
"\x7fELF") :;; # skip ELF executable
*) chmod a-x "$x";;
esac
done
Note that there is a simple way to back up and restore permissions of a directory tree, on Linux and possibly other unices with ACL support:
getfacl -R >saved-permissions
setfacl --restore=saved-permissions
/
or another directory? – gvkv Aug 27 '10 at 19:09/
, a directory completely owned by me. – Larry Wang Aug 27 '10 at 19:12#!
at the start anyways. – Larry Wang Aug 27 '10 at 22:40