1

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.

chip
  • 13
  • 1
  • 6
  • You can add the verbose mode option to `mv`. Do this - `find . -type d -mtime +14 -exec mv -v "{}" "$To" \;` – jaypal singh Jun 11 '13 at 17:18
  • how could i write the output of -v to a log and save to /mnt/STStorage and also email it? i tested -v and it prints out the file and where it was moved on the terminal – chip Jun 11 '13 at 17:22
  • added that in the answer. I am not suggesting this is the best approach. But atleast you have something to play around with and customize it according to your needs like appending datestamp to log files etc. – jaypal singh Jun 11 '13 at 17:23

1 Answers1

1

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
jaypal singh
  • 74,723
  • 23
  • 102
  • 147
  • Thank you, that got me started on the right path. have another questions after my updated code above. – chip Jun 11 '13 at 17:19
  • Thank you, i have it working correctly so far, just have to add the email to it. Could i use something like && at the end of saving the log file? – chip Jun 11 '13 at 19:25
  • After you have created the log file, add this command on the next line `uuencode "$From"/Moved."$dt".txt "$From"/Moved."$dt".txt | mail -s "Whatever Subject" chip@email.com` – jaypal singh Jun 11 '13 at 19:33