Commit e660aff5 authored by kpetersn's avatar kpetersn
Browse files

Initial commit of takeOwnership.sh

parents
#!/bin/bash
SCRIPT_NAME=$0
USER_NAME=$1
DIRECTORY=$2
FILE_PATTERN=$3
NUM_ARGS=$#
SUFFIX="-DELETE-ME-SOON"
#!echo "${SCRIPT_NAME}"
#!echo "${USER_NAME}"
#!echo "${DIRECTORY}"
#!echo "${FILE_PATTERN}"
#!echo "${NUM_ARGS}"
if [ ${NUM_ARGS} -ne 3 ]
then
echo "Usage: takeOwnership.sh <username> <directory> <file_pattern>"
else
# Make sure the directory exists
if [ ! -d ${DIRECTORY} ]
then
echo "Error: ${DIRECTORY} doesn't exist"
else
if [ ! -w ${DIRECTORY} ]
then
echo "Error: ${DIRECTORY} isn't writable"
else
FILE_LIST=`find ${DIRECTORY} -user ${USER_NAME} -name "${FILE_PATTERN}"`
#!echo ${FILE_LIST}
for filepath in ${FILE_LIST}
do
#echo ${filepath}
# filedir can differ from DIRECTORY if the file resides in a subdirectory
filedir=$(dirname ${filepath})
#!echo $filedir
filename=$(basename ${filepath})
#!echo $filename
# Try to rename the file
mv ${filedir}/${filename} ${filedir}/${filename}${SUFFIX}
retval=$?
if [ $retval -ne 0 ]
then
echo "Error: unable to rename ${filedir}/${filename}"
else
# Create a copy of the file with the new user (preserve mode in the future?)
cp --preserve=timestamps ${filedir}/${filename}${SUFFIX} ${filedir}/${filename}
retval=$?
if [ $retval -ne 0 ]
then
echo "Error: unable to copy ${filename}${SUFFIX} to ${filename} in ${filedir} (THIS SHOULD NEVER HAPPEN)"
else
# Delete the temp file if the move and copy were both successful (this will often fail)
rm -f ${filedir}/${filename}${SUFFIX}
fi
fi
done
fi
fi
fi
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment