Write a bash script to protect a directory. Your script should notify a user of
ID: 3815370 • Letter: W
Question
Write a bash script to protect a directory. Your script should notify a user of the following: New file was created Permission changes Ownership changes File size changes Modification date changes Your script should have two operation modes, protect and check. Use command line flags, -p and -c to decide which mode to operate in. In protect mode, your script should create a file called .protect inside the directory you want to watch. You will use this file to check for any changes made as listed above. In check mode, your script should report any of the changes listed aboveExplanation / Answer
#!/bin/bash
ls -l /usr/local/dir | grep username > /tmp/watchfile
while true; do
sleep 10
ls -l /usr/local/dir | grep username > /tmp/watchfile2
diff -q /tmp/watchfile /tmp/watchfile2 > /dev/null
if [ $? -ne 0 ] ; then
echo "New File was created"
exit ;
fi
cp /tmp/watchfile2 /tmp/watchfile
done
### Set initial time of file
LTIME=`stat -c %Z /path/to/directory/file.txt`
SIZE=$(du -B 1 /path/to/directory | cut -f 1 -d " ")
CHECK=$(du -sb /data/sflow_log | cut -f1)
while true
do
ATIME=`stat -c %Z /path/to/directory/file.txt`
if [[ "$ATIME" != "$LTIME" ]]
then
echo "RUN COMMNAD"
LTIME=$ATIME
fi
sleep 5
done
if [ -n "$(find . -user "$username" -print -prune -o -prune)" ]; then
echo "The current directory is owned by $username."
fi
if [ -n "$(find . -user "$(id -u)" -print -prune -o -prune)" ]; then
echo "The current directory is owned by the current user."
fi
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.