View Single Post
Old Nov 1st, 2007, 1:46 PM   #1
acwbrat
Newbie
 
Join Date: Oct 2007
Posts: 17
Rep Power: 0 acwbrat is on a distinguished road
Unhappy Bash Script Deletion help

I want to change some features on a particular code. This code must delete files that haven't been used in the directory.

Here's my code that I saw an example of:

#!/bin/sh

#< Delete an entry from the address book

_ADDRESS_BOOK=$HOME/a_book/a_book
_TEMP_FILE=$HOME/tmp/a_book.tmp.$$
_ENTIRE_FILE=$HOME/tmp/a_full.tmp.$$

# As a : appears in every line, this has the effect
# of numbering the lines in the fashion we want.
grep -in ":" ${_ADDRESS_BOOK} > ${_ENTIRE_FILE}

echo "Enter a searchterm for your deletion:"
read _SEARCHTERM

grep -in "${_SEARCHTERM}" ${_ADDRESS_BOOK} > ${_TEMP_FILE}

_count=$(wc -l < ${_TEMP_FILE})
echo "${_count} match(es) found!"

while read line
do
  _record_number=$(echo $line | awk -F: '{print $1}')
  _records="${_records} ${_record_number}"
done < ${_TEMP_FILE}

echo ""

for record in ${_records}
do
   _current_rec=$(grep "^${record}:" ${_TEMP_FILE})
   _firstname=$(echo ${_current_rec} | awk -F: '{print $2}')
   _surname=$(echo ${_current_rec} | awk -F: '{print $3}')
   _address1=$(echo ${_current_rec} | awk -F: '{print $4}')
   echo "===================================================="
   echo "${_firstname} ${_surname} of"
   echo "${_address1}"
   echo "DO YOU WANT TO DELETE THIS RECORD? [y/n]"
   read response
   case $response in
     [yY]|[yY][eE][sS])  
	grep -v "^$record:" ${_ENTIRE_FILE} > ${_ENTIRE_FILE}_v
    cp -f ${_ENTIRE_FILE}_v ${_ENTIRE_FILE}
	cut -d':' -f 2-11 ${_ENTIRE_FILE}_v > ${_ADDRESS_BOOK}
	echo "ENTRY DELETED!"
	;;
     *) ;;
   esac
done

rm -rf ${_TEMP_FILE}
rm -rf ${_ENTIRE_FILE}
rm -rf ${_ENTIRE_FILE}_v

Here's another example:

#!/bin/bash
#< Script to remove old quarantine and virus mails, plus cocoon logs from web/mail servers

ECHO="/bin/echo"
FIND="/usr/bin/find"
RM="/bin/rm"
SLEEP="/usr/bin/sleep"
TR="/usr/bin/tr"
XARGS="/usr/bin/xargs"

TARGET[0]="/www/webapps/cocoon/WEB-INF/logs"
TARGET[1]="/var/amavis/quarantine"
TARGET[2]="/var/amavis/virusmails"

${ECHO} "WARNING: This script will remove all files in the following directories"
${ECHO} "$( ${ECHO} ${TARGET[@]} | ${TR} ' ' '\n' )"
${ECHO} "that haven't been modified for more than 7 days"
${ECHO} "Sleeping for 5 seconds - CTRL-C if you need to!"
${SLEEP} 5

for DIRECTORY in ${TARGET[@]}; do
   ${ECHO} "--> Performing prune in ${DIRECTORY}"
   ${FIND} ${DIRECTORY} -type f -mtime +7 | ${XARGS} ${RM} 2>/dev/null
   ${ECHO} "--> Prune in ${DIRECTORY} complete"
done

exit 0
acwbrat is offline   Reply With Quote