Does anyone have a tool or script that will recursively correct the file permissions on a directory?
On an Ubuntu Linux machine, a bunch of files were copied to a USB disk with full 777 permissions (user, group, other - read, write, execute) in error. I want to put them back in the user's directory corrected.
Directories should be 775 and all other files can be 664. All the files are images, documents or MP3s, so none of them need to be executable. If the directory bit is set then it needs execution, other wise it just needs user and group, read and write.
I figured it was worth checking if such a utility exists before hacking together a shell script :)
This should do the trick:
find can do the trick alone with -exec:
to prevent find from spawning a chmod for each entry:
(this effectively calls chmod once with the list of all files as parameters rather than one chmod per file)
This answer won't solve your problem, but someone might find it useful for a similar problem where files have less permission than they should do.
The magic is the X permission, rather than x. The chmod manpage describes it thus:
This isn't suitable in your case as your files have execute permission so, will match the second test.
I made a really simple bash script the other day because I needed to fix permissions. Why isn't there a formal utility to reset basic, non-root, file and folder permissions?
The script uses find to 755 all folders and 644 libraries. It then tests each file with readelf to see if it has a binary elf header. If not, it scans in the first two characters for shebang
#!
. It 755 those instances and 644 everything else after it checks to see if the file already has the matching permission.Special cases are handled with an exception like the
*.bak
for files to be ignored.In case that you're using ssh, it's good idea not to modify
~/.ssh
permissions.I made a script out of freiheit's solution, it adds basic argument checking.
An alternative, which does not exactly answer the original request, but is more likely what the OP intends, is to specify the permissions symbolically. E.g., I assume that OP wants to "remove write access for the public, and remove executable permission from all files".
This is possible if you use the symbolic permission representation.
Writing it out this way instead of explicitly setting "0664" you avoid accidentally allowing extra permissions to files that were previously locked down. E.g., if a file was 0770 it will not become 0664 by mistake.
To do the first requirement you can just use
chmod
:To do the second requirement:
Or to do both:
I use this script https://github.com/TheKito/Scripts/blob/master/FixHomesDirectoryPermissions.sh
I found a simplified C-shell solution. This might not be completely fool proof but should work for most directories. It assumes the Unix 'file' command works, and finds exec files to reset them back to exec permissions: