Wednesday, May 16th, 2007
Unix and its chain-commands-through-pipes always comes through. Recently I had a need to do some recursive permission changes. The permissions needed to be changed carefully, though: I needed files to have permissions of 664 (-rw-rw-r–) and directories to have permissions of 2775 (drwxrwsr-x). (The “2″ sets the sticky bit for the group.) Anyway, the find command allows you to find directories and files easily with the -type flag. And, as usual, xargs helps out a ton.
So, changing permissions for all directories below the current directory:
$ find . -type d | xargs chmod 2775
Changing permissions for all files below the current directory:
$ find . -type f | xargs chmod 664
The ‘xargs’ command is invaluable. It appends the incoming data from the pipe (in this case, the line-by-line results from the ‘find’ command) to the end of the command. So the above commands get translated to:
chmod 664 ./some/file/or/directory
…for every file and/or directory. Obviously you should change the permissions to whatever is appropriate in your situation.
