DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Chmodding And Ruby

03.23.2009
| 5119 views |
  • submit to reddit
        Recently, I switched from a Powerbook to a Macbook, and to copy my files from one to the other, I used a pen drive. Since my pen drive has a FAT file system, it treats everything as being executable. This, however, is not the case on a UNIX-like file system like OS X. In order to save myself the hassle of manually chmodding thousands of files, I wrote this Ruby script:

#!/usr/bin/env ruby
require 'find'
require 'fileutils'

# Append to/remove from this list as necessary
Plain = ['php', 'txt', 'jpg', 'jpeg', 'gif', 'png', 'html', 'pdf', 'css', 'mp3', 'zip', 'tar.gz', 'tar', 'htm', 'psd', 'ai', 'ptg', 'cgi.pl', 'sphp', 'svn-base', 'default'].collect do |ext|
    ".#{ext}"
end.freeze

# Append to/remove from this list as necessary
Executable = ['pl', 'rb', 'cgi', 'sh'].collect do |ext|
    ".#{ext}"
end.freeze
$num_chmodded = 0

def chmod_and_puts( cmd )
    puts cmd
    system cmd
    $num_chmodded += 1
end

def chmodder( start_dir )
    num_skipped = 0
    num_plain = 0
    num_executable = 0
   
    Find.find( start_dir ) do |path|
        unless File.symlink?( path )
            if FileTest.file? path
                if is_plain? path
                    chmod_and_puts "chmod 644 \"#{path}\""
                    num_plain += 1
                elsif is_executable? path
                    chmod_and_puts "chmod 755 \"#{path}\""
                    num_executable += 1
                else
                    num_skipped += 1
                end
            elsif File.directory? path
                chmod_and_puts "chmod 755 \"#{path}\""
                num_executable += 1
            else
                num_skipped += 1
            end
        else
            num_skipped += 1
        end
    end
   
    puts "----------------------------------------------"
    puts "Chmodded #{$num_chmodded} total:"
    puts "\tExecutable: #{num_executable}"
    puts "\tPlain: #{num_plain}"
    puts "Skipped #{num_skipped} files/directories/symlinks"
end

def get_extension( path )
    File.extname( path ).downcase
end

def is_executable?( path )
    extension = get_extension( path )
   
    if Executable.include? extension
        true
    else
        false
    end
end

def is_plain?( path )
    extension = get_extension( path )
   
    if Plain.include? extension
        true
    else
        false
    end
end

chmodder( '.' )

This goes through all the files and directories within the directory from which you run the script and chmods non-executable files to 644 (read+write on User, read on Group and Other) and executable files to 755 (read+write+execute on User, read+execute on Group and Other).