I'm writing a script to copy all files within a directory that start with A-L a-l or 0-9 to one destinations and all files starting is m-z M-Z to another destination.
I got it working with the following commands:
Code:
RUNAL="cp -R {} /destination/directory/a-l"
find source_parent_dir -type d -regex "/source/parent/directory/[a-lA-L0-9].*" -exec "$RUNAL" \;
This works perfectly. However I then want to delete the files that were copied. Originally I was running the find command once to copy and then again to delete the directories/files. The problem is that I'm going to set this up on a cron. In the unlikely situation the files copy, then in the time of when the copying starts to when the copying finishes a new files is saved to the directory then the second find command would delete a file that wasn't copied.
I then came up with the idea to run the -exec command with && and a rm -rf:
Code:
RUNAL="cp -R {} /destination/directory/a-l && rm -rf {}"
find source_parent_dir -type d -regex "/source/parent/directory/[a-lA-L0-9].*" -exec "$RUNAL" \;
The weird thing is when I run the cp && rm command by itself in the shell it works. When I add it to the -exec parameter it doesn't work.
Now my idea is to run the find command, save the results into an array, then process the array and copy the files and then process the array and delete the files.
could anyone here please tell me how to save the results of the find command in an array. This is what I've done but it doesn't work:
Code:
declare -a files_found
find /source/parent/directory -maxdepth 1 -type d -regex "^/source/parent/directory/[a-l-A-L0-9].*" -exec array[${#files_found[*]} "{}" \;