Ee62f45441fbf8409dfd4adaa40a74aa

I'm planning to run this snippet in a cron job on the 1st and 16th of each month. The idea is to periodically remove all the old stuff from a shared folder on our LAN. Do you see any issues with the code?

#!/bin/bash

SRC="/export/shared /export/beancounters/"
DEST_BASE="/export/trash"

for S in $SRC 
do
  DEST=$DEST_BASE/$(basename $S)

  rm -rf ${DEST}/*

  while read F
  do

    DIR=$(dirname "$F"); DIR=${DIR#$S}
    if [[ ! -d $DIR ]]
    then
      mkdir -p \"${DEST}${DIR}\"
    fi

    mv \"$F\" \"${DEST}${DIR}\"

  done < <(find $S -type f -mtime +15 | sort)

  find $S -type d -empty -exec rmdir {} \;

done

Refactorings

No refactoring yet !

55502f40dc8b7c769880b10874abc9d0

ghostdog74.livejournal.com

January 8, 2010, January 08, 2010 13:15, permalink

No rating. Login to rate!

Since you are using bash, why not leave out basename and dirname and use bash's built in string methods.

#!/bin/bash
SRC="/export/shared /export/beancounters/"
DEST_BASE="/export/trash"

for S in $SRC
do
  #DEST=$DEST_BASE/$(basename $S)
  DEST="$DEST_BASE/${S##*/}" # added
  rm -rf ${DEST}/*

  while read -r F
  do

    #DIR=$(dirname "$F")
    DIR=${F%/*}  # added
    DIR=${DIR#$S}
    if [[ ! -d $DIR ]]
    then
      mkdir -p \"${DEST}${DIR}\"
    fi

    mv \"$F\" \"${DEST}${DIR}\"

  done < <(find $S -type f -mtime +15 | sort)

  find $S -type d -empty -exec rmdir {} \;
  # if you have GNU find
  # find $S -type d -empty -exec rmdir {} +;

done
88333677cd7928b29d05e1b32ae18dd2

clonecd510

May 9, 2011, May 09, 2011 00:30, permalink

No rating. Login to rate!

A curst cow has short horns.

Your refactoring





Format Copy from initial code

or Cancel