30

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.

2 Answers 2

50

Use the -perm test to find in combination with -not:

find -type d -not -perm 775 -o -type f -not -perm 664
  • -perm 775 matches all files with permissions exactly equal to 775. -perm 664 does the same for 664.
  • -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 not 775 and ordinary files with permissions that are not 664.

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
2
  • Beautiful, thank you! I did not know that two different conditions could be set (type -d and type -f for instance) nor did I know about -perm.
    – dotancohen
    Jul 13, 2014 at 7:45
  • 1
    Maybe note that -not isn't a POSIX standard; they use ! instead. Feb 4, 2018 at 20:32
5

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

3
  • In fact the goal was not to change the files' permissions but rather to identify which files need snowflake permissions when uploading them to a production webserver. Thanks.
    – dotancohen
    Mar 22, 2018 at 7:15
  • "snowflake permissions"??
    – roaima
    Mar 2, 2020 at 12:21
  • 1
    Contemporary use of "snowflake" can be attributed to the 1996 movie Fight Club, used in the phrase "beautiful and unique snowflake". In the context of this post, it indicates those particular files which require, for whatever reason, atypical permissions. Identifying and understanding such files is critical so that a blanket chmod -R or the like doesn't cause a disaster. Strange permissions needed by strange programs are an absolute nightmare to troubleshoot. Aug 11, 2020 at 8:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.