copying random Jpg files to different folder


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting copying random Jpg files to different folder
# 1  
Old 11-01-2011
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

Code:
 
#!/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:


Quote:
sort: invalid option -- R
Try `sort --help' for more information.
cp: missing destination file operand after `/homepages/mylocation/mylocation/htdocs/release/sites/default/files/banner/'
Try `cp --help' for more information.
(uiserver):u35495245:~ > sort: invalid option -- R
Try `sort --help' for more information.
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!
# 2  
Old 11-01-2011
Apparently your sort doesn't have -R.

I think -R is a GNU/Linux thing.

Try shuf.
# 3  
Old 11-01-2011
do you have any syntax for it?

BTW: In my previous post is my code correct?
# 4  
Old 11-01-2011
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:

Code:
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

# 5  
Old 11-01-2011
I got the mapfile through manuals..

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


Quote:
./move2.sh: line 1: shuf: command not found
What could be the issue? Guess Shuf is not supported.

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

Code:
 
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?

Last edited by raamkum; 11-01-2011 at 04:10 PM..
# 6  
Old 11-02-2011
'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. Smilie It will be slow though, since the shell has to process each and every filename found, bar none, before a result can happen. Smilie

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.
# 7  
Old 11-02-2011
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!
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copying files from various folders to similar folder structure in another location

Hi, I need to write a script the has to copy the files from folders and subfolders to the same folder structure located in another location. Ex: mainfolder1 file1,file2,file3 subfolder1(file1,etc) subfolder2(file1,etc) to another folder location of same folder structure. rsync is not... (7 Replies)
Discussion started by: Raji Perumal
7 Replies

2. Shell Programming and Scripting

choosing a random folder in a folder

I need my script to choose a random folder within a folder and set that folders name as a variable. I can list all folders within a folder using #!/bin/bash dir='/folder' file=`/bin/ls -1 -l "$dir" | egrep '^d' | sort --random-sort | head -1` path=`readlink --canonicalize "$dir/$file"`... (6 Replies)
Discussion started by: digitalviking
6 Replies

3. UNIX for Dummies Questions & Answers

Deleting all but JPG files

I would like to delete all files but keep all the .JPG files. How can this be accomplished? Thanks in advance (8 Replies)
Discussion started by: Xterra
8 Replies

4. Shell Programming and Scripting

Bash folder manipulation - selecting/copying specified files

Bash/scripting newbie here - I feel this might be a trivial problem, but I'm not sure how to tackle it. I've got a folder of a year's worth of files, with some random number of files generated every day of the year (but at least one per day). I'm writing a script to automatically grab the file with... (6 Replies)
Discussion started by: WildGooseChased
6 Replies

5. OS X (Apple)

Automated command ; extracting files from folders and copying them into a single folder

Hello everyone, I'm running Mac OS X Leopard (10.5.8) and I want to use the Terminal to help automate this tedious and laborious command for me: I need to extract all of the .m4p files in my "iTunes Music" folder which reside in folders of the artist, and then subfolders for the albums and... (2 Replies)
Discussion started by: qcom
2 Replies

6. Shell Programming and Scripting

Rename all ".JPG" files to ".jpg" under all subfolders...

Hi, Dear all: One question ! ^_^ I'm using bash under Ubuntu 9.10. My question is not to rename all ".JPG" files to ".jpg" in a single folder, but to rename all ".JPG" files to ".jpg" in all subfolders. To rename all ".JPG" to ".jpg" in a single folder, for x in *.JPG; do mv "$x"... (7 Replies)
Discussion started by: jiapei100
7 Replies

7. Homework & Coursework Questions

Copying Folder, C program

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Copying all the normal files from 1 folder to another on a unix platform. I pass in 1 or 2 arguments, the folder... (0 Replies)
Discussion started by: MW99
0 Replies

8. Shell Programming and Scripting

mkdir: copying a folder's permissions

Hi, I wanted to know how to create a folder using mkdir and then have it copy the permissions from another specified folder on the system. For example if I did this: mkdir /Volumes/USBSTICK/System How can I make it copy the permissions from the /System folder ? Thanks (4 Replies)
Discussion started by: pcwiz
4 Replies

9. Shell Programming and Scripting

script for Finding files in a folder and copying to another folder

Hi all, I have a folder '/samplefolder' in which i have some files like data0.txt, data1.txt and data2.txt. I have to search the folder for existence of the file data0.txt first and if found have to copy it to some other file; next i have to search the folder for existence of file... (5 Replies)
Discussion started by: satish2712
5 Replies
Login or Register to Ask a Question