To start out I am very new to this so please forgive me if this is extremely basic or I sound overly uninformed.

I would like to check all the files in a specific directory on my VPS to ensure that files are 644 and directories are 755. FTP is out of the question because there are far too many files/directories. SSH seems the most logical choice, but before i go chmod permissions I'd like to see what I'll be changing. Is there any way to find and display files/directories that do not meet this criteria?

4 Answers
4

As a refinement to David Spillett's answer, how about using a single find command? I think it's a bit cleaner than all the pipes, and calls to grep. Furthemore, it cuts out a bit of the edge cases where this might blow up.

Thank you, this is very helpful as well. Is the exclamation point the the part that reverses this? Could I do a find. f -perm 777 to find them as well?
–
Bms85smBMay 19 '11 at 18:02

Yeah, the ! is basically not. Using find . -type f -perm 777 would only return those files that are 777, so if you're sure that's the only alternative, it would definitely work.
–
Christopher KarelMay 19 '11 at 18:08

Its not the only alternative however I was using the "find all but" to narrow down the results so i could make the judgement call myself. I'm a little concerned that batch chmod is going to create some serious issues (i'm working on a live site). I generated a tarball with permissions included (or so I hope), but i'm still nervous.
–
Bms85smBMay 19 '11 at 18:19

Now just out of curiosity, could this be refined further to find the permissions of only public while group and owner remain irrelevant?
–
Bms85smBMay 19 '11 at 18:32

I understand the hesitation to using find with -exec. It might help if you replace the chmod with ls. That will just show you what it's going to do. Or perhaps by preceding with echo, and writing the chmod commands to a file. And the more you use it, the more comfortable it will be. (Play around in test!) As for your second comment, yes, you should be able to use -perm -o=rwx to match any rwx 'other' permissions.
–
Christopher KarelMay 19 '11 at 18:40

"find all files in current directory, pass to "ls -l" to list their detail, and scan the output of ls for all the lines not (the -v option to grep negates the match) containing "rw-r--r--" and for directories:

find . -type d | xargs ls -l | grep -v drw-r--r--

If you want to scan another directory replace "." with it, such as:

find /path/to/directory -type f | xargs ls -l | grep -v rw-r--r--

and if some of the file/directory names might contain spaces use the "use 0-delimited strings" options in find and xargs like so to avoid errors:

find . -type f -print0 | xargs -0 ls -l | grep -v rw-r--r--

Also, you can remove the -v option from grep to find files/directories that do match, if you want to list these as a sanity check.

Using find will scan subdirectories too by default. To scan just one directory and not its children, you could just pipe the output of ls -l through grep instead of using find and xargs.

When you do run chmod remember that you can specify the -v option so it will list what it does and doesn't change as it performs the operation - this can be useful for reassuring yourself that you've given the right command.

Thank you for your prompt response however the directory in question has many sub directories with more child directories and 10,000+ files. This is why I was hoping to just display files that weren't set to 644 and directories that aren't set to 755.
–
Bms85smBMay 19 '11 at 14:16

find and grep are your friends in that case, as you can see in the newer answer from David.
–
HyppyMay 19 '11 at 14:21