Hawaiian Scuba Dude

Q: Automatically removing trash 5 days old

Hi

I did a secure empty trash yesterday which I have not done in a few years and there were over 1.5  million files to permanently erase.   This is dangerous to keep this much trash and it takes way too long to clean up.  I would prefer regular maintenance . . . to securly and automatically empty the trash regularly and still maintain the benefit being able to prevent accidental deletion of recent work.  The requirement therefore would be to perhaps during off hours, once a day or upon startup automtically secure empty trash only items that have aged in the trash at least x days.

 

Can this be done quickly and simply with applescript or a cron job?

Posted on Jul 12, 2011 12:58 AM

Close

Q: Automatically removing trash 5 days old

  • All replies
  • Helpful answers

Previous Page 2 of 3 last Next
  • by Hawaiian Scuba Dude,

    Hawaiian Scuba Dude Hawaiian Scuba Dude Jun 10, 2012 2:35 AM in response to Frank Caggiano
    Level 1 (5 points)
    Jun 10, 2012 2:35 AM in response to Frank Caggiano

    The point of the post was the explanation of a records/documentation retention policy.

  • by Monsignor Paolo,

    Monsignor Paolo Monsignor Paolo Jun 9, 2013 10:00 AM in response to twtwtw
    Level 1 (10 points)
    Mac OS X
    Jun 9, 2013 10:00 AM in response to twtwtw

    It not works for me and I can't understand where is the error.

    The script works well, if I run it in Applescript editor, so I think, the problem is in .plist file created by me or somewhere else.

    What I did:

    I made dated_deleter.scpt file with content written above and placed it in ~/Library/Scripts folder. It works correct after manual execution, there are new tags in Spotlight field files properties in Trash folder.

    Then I made user.trash.dated.plist file with content:

     

     

    <?xml version="1.0" encoding="UTF-8"?>

    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">

    <plist version="1.0">

    <dict>

              <key>Label</key>

              <string>user.trash.dated</string>

              <key>ProgramArguments</key>

              <array>

                        <string>osascript</string>

                        <string>/Users/user1/Library/Scripts/dated_deleter.scpt</string>

              </array>

              <key>StartCalendarInterval</key>

              <dict>

                        <key>Hour</key>

                        <integer>0</integer>

                        <key>Minute</key>

                        <integer>0</integer>

              </dict>

    </dict>

    </plist>

     

     

    and placed it in ~/Library/LaunchAgents folder.

    Next I entered launchctl load ~/Library/LaunchAgents/user.trash.dated.plist in Terminal, launchctl list command shows user.trash.dated.plist is in list, status 1.

    And… nothing. No new tags, the applescript is not executing.

    Could somebody explain me, what's wrong?

  • by mm256,

    mm256 mm256 Jul 16, 2013 1:10 AM in response to Hawaiian Scuba Dude
    Level 1 (0 points)
    Jul 16, 2013 1:10 AM in response to Hawaiian Scuba Dude

    Is there an App for it? Looks like it's too messy playing with Apple Script.

  • by Hawaiian Scuba Dude,

    Hawaiian Scuba Dude Hawaiian Scuba Dude Jul 16, 2013 5:36 PM in response to mm256
    Level 1 (5 points)
    Jul 16, 2013 5:36 PM in response to mm256

    Personally, I did not like the idea of combining UNIX/POSIX scripts with AppleScript.  To your point, . . . kind of messy.  I implemented a similar algorithm as the AppleScript example in korn shell with the terminal app

     

    #!/bin/ksh

    today=`date '+%Y%m%d'`

     

    delay=7

    #

    echo

    echo  Locate any data in the users' .trash directory or disk's .trashes directory.  If previously scheduled to be removed, move it to a directory for mass removal. else schedule a date + 1 week it should be removed permanently.

    echo

     

     

    find /Volumes/*/.trashes/* /Volumes/*/.trashes/.[0-z]* /Users/*/.Trash/* -prune -exec ls -1d {} \; 2>/dev/null | grep -v trashInfo | grep -v deleteNow | while read trashFile

    do

      trashDir=`dirname "${trashFile}"`

      base=`basename "${trashFile}"`

      trashInfo="${trashDir}/.${base}.trashInfo"

      echo -n "looking at ${trashFile}: "

      du -sh "${trashFile}" | awk '{print $1}'

      if [ -r "${trashInfo}" ]

      then

        dateToSRM=`awk '{print $NF}' "${trashInfo}"`

        if [ $dateToSRM -lt $today ]

        then

          if  [ ! -d "${trashDir}"/.deleteNow ]

          then

            mkdir -p "${trashDir}"/.deleteNow

          fi

          mv "${trashFile}" "${trashDir}/.deleteNow"

          mv "${trashInfo}" "${trashDir}/.deleteNow"

        fi 

      else

        echo "date to secureRemove: " `date -v+${delay}d '+%Y%m%d'` > "${trashInfo}"

      fi

    done

     

    echo

    echo locate any trashinfo files without actual trash file and move it to the pending removal directory

    echo

     

    find /Volumes/*/.trashes/* /Volumes/*/.trashes/.[0-z]* /Users/*/.Trash/* -name '*.trashInfo' -prune -exec ls -1 {} \; 2>/dev/null | grep -v deleteNow | while read trashInfo

    do

      base=`basename "${trashInfo}" .trashInfo | sed s/^\.//`

      trashDir=`dirname "${trashInfo}"`

      if ! [ -r "${base}" ] > /dev/null 2>&1

      then

        mv "${trashInfo}" "${trashDir}/.deleteNow"

      fi

    done

     

    echo

    echo remove anything in the pending removal directory

    echo

     

    du -sk /Volumes/*/.trashes/.deleteNow/* /Users/*/.Trash/.deleteNow/* | sort -nr | uniq| while read size realTrash

    do

      echo -n "looking at ${realTrash}: "

      du -sh "${realTrash}" | awk '{print $1}'

      chmod -R 777 "${realTrash}"

      srm -rfmz "${realTrash}" 2>/dev/null &

    done

    numFiles=`find /Volumes/*/.trashes/.deleteNow/* /Users/*/.Trash/.deleteNow/* -prune -exec ls -1d {} \; 2>/dev/null | wc -l`

    while [ $numFiles -gt 0 ]

    do

      echo

      echo

      du -sk /Volumes/*/.trashes/.deleteNow/* /Users/*/.Trash/.deleteNow/* 2>&1 | grep -v "No such file" | sort -nr | head -5 | awk '{printf "%2.2f MB %s\n",$1/1024,$2}'

      sleep 60

      numFiles=`find /Volumes/*/.trashes/.deleteNow/* /Users/*/.Trash/.deleteNow/* -prune -exec ls -1d {} \; 2>/dev/null | wc -l`

    done

  • by Hawaiian Scuba Dude,

    Hawaiian Scuba Dude Hawaiian Scuba Dude Jul 16, 2013 5:47 PM in response to Linc Davis
    Level 1 (5 points)
    Jul 16, 2013 5:47 PM in response to Linc Davis

    Linc Davis wrote:

     

    Any hopes of including it in the OS?

     

    I sure hope not. The Trash is not for temporary file storage. It's a safeguard against accidental deletion. You should never put anything in the Trash unless you intend to delete it immediately.

    To  your point, trash is a safeguard against accidental deletion.  Nothing should be put in Trash unless you intend to delete it.  But ultimately by nature of a safeguard it does keep stuff temporarily.  Think of it as a utility the same as paying a trash collector to come to your home and pick up your trash once a week.

  • by Hawaiian Scuba Dude,

    Hawaiian Scuba Dude Hawaiian Scuba Dude Jul 16, 2013 5:51 PM in response to championshipdigital
    Level 1 (5 points)
    Jul 16, 2013 5:51 PM in response to championshipdigital

    championshipdigital wrote:

     

    At the risk of oversimplifying things, why not just empty the trash yourself each Monday morning/Friday afternoon?

    Do you drive to the city dump to remove your trash from your house?  No.  You have a utility that does it for you.

  • by Hawaiian Scuba Dude,

    Hawaiian Scuba Dude Hawaiian Scuba Dude Jul 16, 2013 6:00 PM in response to Frank Caggiano
    Level 1 (5 points)
    Jul 16, 2013 6:00 PM in response to Frank Caggiano

    Frank Caggiano wrote:

     

    If that's your requirements then you should be sweeping your documents directory (or entire HD) and removing the files that are past the time you want to keep them. Moving a file into the trash and expecting it to be there a week later is foolish.

     

    Not quite the point of the thread.  There is some level of expectation when deleting a file, it should never be available anymore period.  Just like taking out the trash at home.  On Monday mornings, I know it is no longer available.

  • by Hawaiian Scuba Dude,

    Hawaiian Scuba Dude Hawaiian Scuba Dude Jul 16, 2013 6:06 PM in response to Hawaiian Scuba Dude
    Level 1 (5 points)
    Jul 16, 2013 6:06 PM in response to Hawaiian Scuba Dude

    Hawaiian Scuba Dude wrote:

     

    numFiles=`find /Volumes/*/.trashes/.deleteNow/* /Users/*/.Trash/.deleteNow/* -prune -exec ls -1d {} \; 2>/dev/null | wc -l`

    while [ $numFiles -gt 0 ]

    do

      echo

      echo

      du -sk /Volumes/*/.trashes/.deleteNow/* /Users/*/.Trash/.deleteNow/* 2>&1 | grep -v "No such file" | sort -nr | head -5 | awk '{printf "%2.2f MB %s\n",$1/1024,$2}'

      sleep 60

      numFiles=`find /Volumes/*/.trashes/.deleteNow/* /Users/*/.Trash/.deleteNow/* -prune -exec ls -1d {} \; 2>/dev/null | wc -l`

    done

    This section is for monitoring the progress if there is a large amount of trash

  • by twtwtw,

    twtwtw twtwtw Jul 16, 2013 7:26 PM in response to mm256
    Level 5 (4,935 points)
    Jul 16, 2013 7:26 PM in response to mm256

    now (a year later) I could write a simpler script, but the central problem still remains: Neither applescript nor unix knows when a file was moved into the trash. even stat (which can tell when files were moved into any other folder) doesn't know that.  If you want deletions based on the age of the file or the last modification date, fine, but to get the date the file was added to the trash is more complex.

  • by Monsignor Paolo,

    Monsignor Paolo Monsignor Paolo Jul 17, 2013 1:37 AM in response to Hawaiian Scuba Dude
    Level 1 (10 points)
    Mac OS X
    Jul 17, 2013 1:37 AM in response to Hawaiian Scuba Dude

    I found where was the problem.

    Just executed:

    osascript /Users/user1/Library/Scripts/dated_deleter.scpt

    and got:

    /Users/user1/Library/Scripts/dated_deleter.scpt: execution error: This script contains uncompiled changes and cannot be run. (-2700)

    Сonclusion: compile script in AppleScript editor by pressing "Compile" after you change it and before saving.

  • by Tony T1,

    Tony T1 Tony T1 Jul 17, 2013 5:35 AM in response to Hawaiian Scuba Dude
    Level 6 (9,249 points)
    Mac OS X
    Jul 17, 2013 5:35 AM in response to Hawaiian Scuba Dude

    Just use find and launchd (or cron):

     

    /usr/bin/find $HOME/.Trash -mindepth 1 -prune -not -newerct '7 days ago' -exec /bin/rm -Rf {} \;

  • by Mark Jalbert,

    Mark Jalbert Mark Jalbert Jul 17, 2013 7:23 AM in response to Tony T1
    Level 5 (4,649 points)
    Jul 17, 2013 7:23 AM in response to Tony T1

    Hey Tony,

    If you want to empty the trash on other volumes then->

     

     /usr/bin/find /Users/*/.Trash /Volumes/*/.Trashes/*/ ..................
    

    This would have to be run as the root user.

     

    Message was edited by: Mark Jalbert -added wild card

  • by twtwtw,

    twtwtw twtwtw Jul 17, 2013 11:20 AM in response to Tony T1
    Level 5 (4,935 points)
    Jul 17, 2013 11:20 AM in response to Tony T1

    Tony T1 wrote:

     

    Just use find and launchd (or cron):

     

    /usr/bin/find $HOME/.Trash -mindepth 1 -prune -not -newerct '7 days ago' -exec /bin/rm -Rf {} \;

     

    N.B. From testing yesterday, it seems that (on 10.8.4) the -newerct option will not compare the date/time the item was moved into the trash, but instead uses the date/time that the item was moved into its previous folder.  I don't know if that's a bug, a feature, or an omission, but test it out yourself.  That can produce unexpected behavior: if the item was moved into its original folder a month ago it will be deleted immediately, because the system will think it's been in the trash for a month.

     

    Of course, if that's the behavior your looking for, all's well.  But it seems wrong to me.

  • by Tony T1,

    Tony T1 Tony T1 Jul 17, 2013 3:53 PM in response to twtwtw
    Level 6 (9,249 points)
    Mac OS X
    Jul 17, 2013 3:53 PM in response to twtwtw

    Thanks for pointing that out.  Not the behavior that I was looking for.  I agree that this seems wrong. 

  • by Hawaiian Scuba Dude,

    Hawaiian Scuba Dude Hawaiian Scuba Dude Jul 18, 2013 4:33 AM in response to twtwtw
    Level 1 (5 points)
    Jul 18, 2013 4:33 AM in response to twtwtw

    Twtwtw, you are right about being more complex. :0  To get around it, the .trashinfo file that is created as part of the script is the trashed date.  It is intended to be a file  attribute.  To support the case of manually removing a file from trash, If it is not paired with the file, the .trashinfo file  is removed

Previous Page 2 of 3 last Next