Skip to content

Instantly share code, notes, and snippets.

@oncomouse
Created April 30, 2017 14:56
Show Gist options
  • Save oncomouse/62d1c05c08933095ad0a171451642b56 to your computer and use it in GitHub Desktop.
Save oncomouse/62d1c05c08933095ad0a171451642b56 to your computer and use it in GitHub Desktop.
Script to fix permissions issues caused by a student IT worker running `chmod -R 755` in my OSX home directory
require 'shellwords'
# Dir.glob was acting weird w/ spaces in file names, so just used find:
IO.popen("find Old\\ Home/apilsch/ -type f") do |fp|
while not (output = fp.gets).nil?
# Full file path:
file = "/Users/apilsch/Desktop/#{output.strip}"
# Location of original file on disk:
disk_file = file.sub("Desktop/Old\ Home/apilsch/", "")
next if not File.exist? disk_file
# Similar to the problem w/ Dir.glob, File.stat was acting strange w/ spaces in file names, so just use commands:
old_mode = %x(/usr/bin/stat -f '%A' #{Shellwords.escape(file)}).to_i
new_mode = %x(/usr/bin/stat -f '%A' #{Shellwords.escape(disk_file)}).to_i
if old_mode != new_mode
puts "Changing #{disk_file} from #{new_mode} to #{old_mode}"
# chmod the file:
%x(/bin/chmod #{old_mode} #{Shellwords.escape(disk_file)})
else
puts "File #{disk_file} is fine"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment