Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 12 down vote accepted

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 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 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.

share|improve this answer
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 '14 at 7:45

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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