Loop folders, delete files, copy new ones

Folks,

I am hopeful that you may be able to help me out with writing a script that can be run nightly (as cron?) to loop through all subfolders within the "/media" directory, delete all of the files in each of them, and then copy in all of the files from the "/home/[user]/sansa" directory to each of the "/media" subfolders.

I have configured a Linux system as a charging station for about 50 Sansa Clip MP3 players with USB connectivity, which are being used for visitors to a museum attraction. Sometimes, visitors delete files from the players, or use the Sansa's recording feature to record sound files that other visitors need not access. The goal is to wipe the contents of the devices on a nightly basis, and retrieve them the next morning with freshly-written data.

Thanks for any assistance,

  • Adam

Something like this?

rm -r /media

cd /home/[user]/sansa
find * -print | cpio -pdmu /media

Instead of doing a massive delete of all the files just use rsync to prune the one's that are different from the source in /home/

 
rsync -a --delete \
--exclude /home/whateveryouwant \
--exclude /home/someotherfolder \
/home/ \
/media

Thanks, Franklin52.

I really like simusphere's approach. However, how do I get the script to loop through each of the subfolders inside the /media parent? For instance, device named USBDEV01 and USBDEV02 are both listed within the /media directory as /media/USBDEV01 and /media/USBDEV02. I want the rsync operation to run both inside USBDEV01 and USBDEV02 subfolders, plus any additional subfolders that may exist, depending on how many USB devices are connected on a given night.

More simply, is it possible to configure the script to do a loop rsync inside each subdirectory within /media, regardlss to how many folders are present, and what they are named?

Thanks for the help!

  • Adam

---------- Post updated at 11:09 AM ---------- Previous update was at 10:12 AM ----------

I managed to figure it out. The following is what I'm using successfully:

#!/bin/sh
cd /media
find /media/* -maxdepth 0 -print | while read directory
do
   echo "copying files to ${directory#"./"}"
   rsync -a --no-g --delete /home/PAMPLINPARK0/acraig/sansa/ "${directory#"./"}"
   sleep 5
done

Are these sansa devices physically labeled in a unique way? Is the data files on each one different? Just trying to understand the problem at hand. If the data is the same on each device then it should be a simple solution. If it is unique data on each device then you will need to create a unique identifying file (perhaps a hidden dot file) that would uniquely identify the device. I would use the ${username} with a number on the end.

.${username}01
.${username}02
.${username}03
.${username}04
etc...

The script would need to determine the source and destination directory for each device found in /media.

 
cd /media
for usbdevice in *
do
  ...
done

I don't have time to work on it right now but maybe some of the other gurus on here can hash it out for you.

simusphere, Thanks for your response. The script that I posted just before your last comment is what I ended up settling on. It successfully performs rsync operations on all of the devices mounted within the /media directory.

Ah, so you did have the simple version of what you were asking. So all the media on the usb drives is the same. Excellent!, glad you got it working the way you wanted.