Bash script to fix file permissions recursively

 
Tags: , , ,

I love the redhat implementation of cron, simply drop a shell script into /etc/cron.daily/ and your script will be executed every day (by default at 4am).

Recently I’ve been having a small problem with mediatomb, further investigation lead me to a “Inotify thread caught exception” error which can be fixed by recursively resetting your file permissions.

What I have done to fix the issue is save the following code as /etc/cron.daily/fix_mt.sh and ‘jobs a gooden’ :cool:

#!/bin/bash
# Load up your mediatomb directories...
MediaTombDirectories[0]="/home/me/Videos"
MediaTombDirectories[1]="/home/me/Music"
# add more if needbe
# MediaTombDirectories[2]="/home/me/files"

# Setup find correctly.
export IFS=$'\n'

# Loop through our array.
for x in ${MediaTombDirectories[@]}
	do
		# Find all directories & subdirectories
		for i in $(find $x -type d)
			do
				# Fix Permissions
				chmod -c 775 $i
				chown -c me:user $i
			done

		# Find all Files
		for i in $(find $x -type f)
			do
				# Fix Permissions
				chmod -c 664 $i
				chown -c me:user $i
			done
	done


Quick tip:
You (or root) will get e-mailed every time this executes, you can make the script more verbos by changing all “-c” to “-v“, or make it silent by removing the “-c” altogether.

Enjoy!

nick

 

2 Responses to “Bash script to fix file permissions recursively”

  1. [...] Based on an example Bash script by linickx.com [...]

  2. mmatczuk says:

    Your script is buggy – it does not handle file names with spaces.

    try something like this
    find ~/ -type f -exec chmod 600 ‘{}’ \;

    or you can always use find … -print0 | xargs -0

Leave a Reply

 

Some other things that might interest you...

---