DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Chmodding And Ruby

Snippets Manager user avatar by
Snippets Manager
·
Mar. 23, 09 · · Code Snippet
Like (0)
Save
Tweet
452 Views

Join the DZone community and get the full member experience.

Join For Free
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).

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • What Is URL Rewriting? | Java Servlets
  • Checklist for API Verification
  • How to Submit a Post to DZone
  • 12 Kick-Ass Software Prototyping and Mockup Tools

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo