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?

Thanks in advance for any insight.


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.

find . -type f ! -perm 644 should find every file that doesn't have -rw-r--r-- permissions. For directories, you can use find . -type d ! -perm 755.

Once you're happy with what you're seeing, you can even combine the chmod into the same command:

find . -type f ! -perm 644 -exec chmod 644 {} \;
find . -type d ! -perm 755 -exec chmod 755 {} \;

  • 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? – Bms85smB May 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 Karel May 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. – Bms85smB May 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? – Bms85smB May 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 Karel May 19 '11 at 18:40
find . -type f | xargs ls -l | grep -v rw-r--r--

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

  • Clever solution. I like it. – Safado May 19 '11 at 14:21
  • In crazy rare cases this may fail on files with names like rw-r-r--.jpg. "find . -type f | xargs ls -l | awk ' { if($1!="rw-r--r--") print $0} '" may work better. (i think!) – Sirex May 19 '11 at 14:25
  • Or just use egrep and $rw-r--r-- Whatever works :p – Sirex May 19 '11 at 14:27
  • Thank you very much, This is exactly what I needed and easy to understand! – Bms85smB May 19 '11 at 14:32
  • 2
    find supports the -perm flag as well, find . -type f -perm 0644 – HampusLi May 19 '11 at 16:17

The following command should give you numerical permissions for all files in the specified directory

 stat -c "%a %n" /path/to/files/*
  • 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. – Bms85smB May 19 '11 at 14:16
  • find and grep are your friends in that case, as you can see in the newer answer from David. – Hyppy May 19 '11 at 14:21

See if the first answer to this works for you: View permissions @stackoverflow

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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