Question

first post. Background- My wife is a photographer, she take a lot of pictures and saves them to a drive (mnt/STStorage) while she is editing them, but never cleans up afterward. I have a drive that i would like to move the folders to based on modified date.(/mnt/LTStorage). Need help with a script that i can add to a cron job to run once a day 30 1 * * * I would like for the script to..

  1. Check /mnt/STStorage/ root folders for last modified date & if older than 14 days, move that folder and everything in it to /mnt/LTStorage/ while keeping the same folder name.
  2. Then write what was moved to /mnt/STStorage/ so that we know what was moved and email me a log of folders moved.

OS CentOS 6.4

here is what i have, think this may work for now. Could be cleaner.

#/bin/bash
dt=$(date +%Y.%m.%d) 
From="/mnt/STStorage/"
To="/mnt/LTStorage/"

if [[ ! -d "$To" ]]; then
mkdir -p "$To"
fi

cd "$From"
for i in "$@"; do
find . -type d -mtime +14 -exec mv "{}" "$To" \; > "$From"/Moved."$dt".txt

uuencode "$From"/Moved."$dt".txt "$From"/Moved."$dt".txt | mail -s "Files Moved"
me@me.com
done

Then i will add this to crontab to run once a day.

Was it helpful?

Solution

You can use -exec along with find. Something like:

find /mnt/STStorage/ -type d -mtime +14 -exec mv {} /mnt/LTStorage/ \;
  • -type d will ensure only directories are moved.

Another option is to use xargs

 find /mnt/STStorage/ -type d -mtime +14 | xargs -I '{}' mv {} /mnt/LTStorage/

Update:

To add what is being moved, you can set the verbose mode option for mv

find /mnt/STStorage/ -type d -mtime +14 -exec mv -v {} /mnt/LTStorage/ \;

Since this will print everything on standard out. You can redirect it to a log file.

find /mnt/STStorage/ -type d -mtime +14 -exec mv {} /mnt/LTStorage/ \; > /mnt/STStorage/log.file

For emailing you can do something like -

uuencode /mnt/STStorage/log.file /mnt/STStorage/log.file | mail -s "this is my subject line" chip@email.com
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top