How to check file permissions via SSH
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 {} \;
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.