Bash script to fix file permissions recursively

line 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

 

One Comment

  1. pingback from: gothi » Blog Archive » SwirlyMMS picture import fix

Got something to say?

 

Some other things that might interest you...

---