Search This Blog

Thursday, October 28, 2010

SUSE 11 - Script

Search for MP3 files in the home directories

On server1, create a shell script (bash) called /root/bin/findmp3 to search for MP3 files in the users' home directories. The script has to perform the following tasks:


a) Create a directory /quarantine if it does not exist.

b) For each user on your system with the home directory in /home, create a corresponding directory in /quarantine (e.g. /quarantine/student).

c) Search for filenames ending in .mp3 (case insensitive) in all home directories. If matching filenames are found, move the files to a directory /quarantine//YYYY-MM-DD, where YYYY-MM-DD is the current date (e.g. /quarantine/student/2009-04-21).

d) Set the permissions of the /quarantine directory and all subdirectories that only root has access. The owner and group of all files in the subdirectories have to be kept (e.g. student as the owner and users (e.g. student as the owner and users as the group).

e) Create a file containing all user names whose quarantine directory contains files moved there today (i.e., the day the script has been run).
Example: When the script runs and moves MP3 files from user student to his quarantine directory today, the user name student has to be in the file. If no files are found for the user manager today, the user name manager must not be in the file.

f) Mail the list of user names to root@server1, the subject of the mail has to be "New MP3 files found". Remove the file with the user names after sending it via email.

Hint: To test your script, use the touch command as normal user to create files such as title1.mp3, title2.mP3 in the user's home directory or a subdirectory of his home directory.

Find MP3 Files Script

#!/bin/bash
if [ ! -d /quarantine ]
then
mkdir /quarantine
fi

chmod 700 /quarantine
cd /quarantine
touch users

declare -a usersArray
usersArray=( `ls -dl /home/* | awk '{print $8}' | sed 's/\/home\///'` )
usersCount=${#usersArray[*]}

for i in `seq $usersCount`
do
user=${usersArray[i-1]}
if [ ! -d $user ]
then
mkdir $user
fi

today=`date +%F`
userDir=$user/$today
if [ ! -d $userDir ]
then
mkdir $userDir
fi

find /home/$user -type f -iname '*.mp3' -exec mv {} $userDir \;
chmod 700 $user
chmod 700 $userDir

ls -l $userDir | awk '{print $3}' >> users
done

uniq users | sed '/^$/d' > mp3users
if [ -s mp3users ]
then
mail root -a mp3users -s "New MP3 files found" <~
rm users mp3users
fi

No comments:

Post a Comment