Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Bash / Shell Scripting (http://www.programmingforums.org/forum26.html)
-   -   Bash Script Deletion help (http://www.programmingforums.org/showthread.php?t=14319)

acwbrat Nov 1st, 2007 1:46 PM

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



All times are GMT -5. The time now is 12:25 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC