Created
April 30, 2017 14:56
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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