I am moving a website from one server to another and Git does not store metadata such as file permissions. I need to find the directories and files that are not 775 / 664 respectively.
Right now, I’m using this cobbled-together contraption:
$ find . -type d -exec ls -la {} ; | grep ^d | grep -v ^drwxrwxr-x
$ find . -type f -exec ls -la {} ; | grep -v ^d | grep -v ^-rw-rw-r-- | grep -v '.git'
Though this works, I feel it is rather hacky. Is there a better way to do this, perhaps a canonical way, or should I just be hacky?
This is running on a recent Ubuntu version with GNU tools under Bash.
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
Use the -perm test to find in combination with -not:
find -type d -not -perm 775 -o -type f -not -perm 664
-perm 775matches all files with permissions exactly equal to775.-perm 664does the same for664.-not(boolean NOT) negates the test that follows, so it matches exactly the opposite of what it would have: in this case, all those files that don’t have the correct permissions.-o(boolean OR) combines two sets of tests together, matching when either of them do: it has the lowest precedence, so it divides our tests into two distinct groups. You can also use parentheses to be more explicit. Here we match directories with permissions that are not775and ordinary files with permissions that are not664.
If you wanted two separate commands for directories and files, just cut it in half at -o and use each half separately:
find -type f -not -perm 664 find -type d -not -perm 775
Method 2
I have no idea what your code was trying to achieve.
Normally the reason for finding Files and Directories with incorrect permissions is for the purpose of changing them.
So this is what I am successfully using in Ubuntu 16.04
find ! -perm 775 -type d -exec chmod 775 {} ;
find ! -perm 664 -type f -exec chmod 664 {} ;
This demonstrates the ! symbol being escaped
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