copying random Jpg files to different folder

Hi,

I have 200 pictures in a folder and I would like move 10 random pictures every week to given folder automatically.

I have this server on 1and1.com.

So I tried the following using Bash script for manual copy and paste for testing

 
#!/bin/bash
mapfile -t -n 3 files < <(find /homepages/mylocation/location/htdocs/release/randompics/ -type f | sort -R)
cp --backup=numbered "${files[@]}" /homepages/mylocation/mylocation/htdocs/release/pictures/ 

when execute this script on my linux machine I get:

NOTE: 'mylocation' is just my reference. I have changed it for privacy issue.

I am not sure what is wrong here? Can you somebody help me out?

Awaiting your response!

Thanks!

Apparently your sort doesn't have -R.

I think -R is a GNU/Linux thing.

Try shuf.

do you have any syntax for it?

BTW: In my previous post is my code correct?

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

I got the mapfile through manuals..

I used the code syntax given by you I got the following:

What could be the issue? Guess Shuf is not supported.

I was searching the internet for this somebody has tried this:

 
ls | while read x; do echo "`expr $RANDOM % 1000`:$x"; done \ | sort -n| sed 's/[0-9]*://' | head -15
 

The above listed out random 15 files.

But do you know how to use the above code to copy and paste the file to a location?

'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. :slight_smile: 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.

Hey!

Thanks for the reply!

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.

prompt> crontab -e

and type the below:

49 11 * * 3 /bin/chmod +x /kunden/homepages/mylocation/mylocation/htdocs/release/sites/default/files/randompics/move.sh
50 11 * * 3 /bin/bash /kunden/homepages/mylocation/mylocation/htdocs/release/sites/default/files/randompics/move.sh

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.

Do you know what could be wrong?

Thanks!

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!

When I run the script manually it is working just fine but it is not the same on cronjob.

Also I am not able to access any log files to figure out the issue.

Anyways, here is the code:

 
#!/bin/bash
 
templocation=/kunden/homepages/mylocation/mylocation/htdocs/release/sites/default/files/randompics/tmp/
copylocation=/kunden/homepages/mylocation/mylocation/htdocs/release/sites/default/files/banner/

rm /kunden/homepages/mylocation/mylocation/htdocs/release/sites/default/files/banner/*.jpg
FILENAME=$(ls *.jpg | while read x; do echo "`expr $RANDOM % 1000`:$x"; done \
     | sort -n| sed 's/[0-9]*://' | head -10)

cp -f $FILENAME $templocation
cd $templocation
i=0
for i in `ls *.jpg`
do
x=`expr $x + 1`
mv $i $x.jpg
done
mv *.jpg $copylocation
 
# EMAIL
# The subject
SUBJECT="***** Random Pictures Changed TODAY! *****"
# Who to send the email to
EMAIL="myemail@gmail.com"
# Email text/message
EMAILMESSAGE="/kunden/homepages/mylocation/mylocation/htdocs/tmp/tmp.txt"
echo "This is a weekly change - Every Wednesday 16:00 GMT" > $EMAILMESSAGE
echo " " >> $EMAILMESSAGE
echo "The following pictures have been copied:" >> $EMAILMESSAGE
echo " " >> $EMAILMESSAGE
echo $FILENAME >> $EMAILMESSAGE
 
# send an email using /bin/mail
/usr/bin/mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE

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).

EDIT: Actually, it's probably this

FILENAME=$(ls *.jpg ...

From cron you won't be in the right directory.

1 Like

That's a useless use of ls * and dangerous use of backticks.

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
1 Like

Corona688,

The script that you gave randomly renames the existing *.jpg file and doesn't move to the required location.

I want randomly move the 10 jpg file and then rename it in series 1 To 10.

BTW: All are JPG files and I don't have any other formats.

Thanks!

Read closer.

if ! cp "$LINE" "/path/to/dest/${X}.jpg"

It copies, not renames, directly to the destination as a filename from 0 through 9. Then, only after successfully copying, it deletes the original.

Other than the difference between 0-9 and 1-10 -- which is a trivial fix -- it fills your requirements.

I changed FILENAME=$(ls *.jpg to Full location.

And ran the cronjob still the same. :frowning:

---------- Post updated at 01:42 PM ---------- Previous update was at 01:36 PM ----------

Actually My bad.. I gave wrong location. It is working.. This is solved the issue. Thanks a lot mate!! :slight_smile: