I have no idea what mapfile is, so can't even guess.
You use shuf like you'd use sort -R. Also, see man shuf.
How I'd do it:
find /homepages/mylocation/location/htdocs/release/randompics/ -type f | shuf |
for ((N=0; N<10; N++))
do
read LINE || break;
mv "$LINE" "/path/to/dest"
done
'command not found' generally means 'command not found'.
That's a clever way to do it -- append a number to the filename, so sort puts them into a random order. It will be slow though, since the shell has to process each and every filename found, bar none, before a result can happen. :wall:
It works nearly the exact same way I showed you -- read filenames one by one, then move them, except you have to strip the number off.
I figured out the way to get the random file working... with the code that could list out the files. And everything works just fine when I manually run the script with.
prompt> chmod +x move.sh
prompt> ./move.sh
I files are randomily selected, renamed and moved to the location.
But I have another issue:
When I do the same using cronjob for the shell script.
I couldn't get the file moved but the existing file got deleted as per the code and also sent me an email too...but it didn't do the main job moving random file to given location.
You don't have to chmod +x every single time you run a script. Once you've done so once, it sticks. It's stored as part of the file, like the filename.
As for why it doesn't move the files, I can't possibly tell without seeing what's in the script!
That's usually an indication that you're relying on an environment variable or similar that's only set in your shell's interactive login script (.bash_profile or whatever).
You're assuming your cron script starts in the right directory. This is probably not true.
You're assuming that all the files you find are .jpg files -- is this true? no jpeg? no gif? no png?
You don't need to use \ to extend lines when you can just put a pipe on the end, and continue the other end of the pipe on the next line.
You've done no error checking, which allows your program to delete files without copying them.
How about this:
cd /path/to/files/
ls *.jpg |
while read x; do echo "`expr $RANDOM % 1000`:$x"; done |
sort -n| sed 's/[0-9]*://' |
for X in 0 1 2 3 4 5 6 7 8 9
do
read LINE || break
if ! cp "$LINE" "/path/to/dest/${X}.jpg"
then
echo "Couldn't copy $LINE" >&2
break
fi
rm "$LINE"
done