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
# 8  
Old 11-02-2011
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!
# 9  
Old 11-02-2011
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:

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

# 10  
Old 11-02-2011
Quote:
Originally Posted by raamkum
When I run the script manually it is working just fine but it is not the same on cronjob.
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
Code:
FILENAME=$(ls *.jpg ...

From cron you won't be in the right directory.
This User Gave Thanks to CarloM For This Post:
# 11  
Old 11-02-2011
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:

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

This User Gave Thanks to Corona688 For This Post:
# 12  
Old 11-02-2011
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!

Last edited by raamkum; 11-02-2011 at 03:53 PM..
# 13  
Old 11-02-2011
Quote:
Originally Posted by raamkum
Corona688,

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

Code:
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.
# 14  
Old 11-02-2011

Quote:
Originally Posted by CarloM
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
Code:
FILENAME=$(ls *.jpg ...

From cron you won't be in the right directory.
I changed FILENAME=$(ls *.jpg to Full location.

And ran the cronjob still the same. Smilie

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

Quote:
Originally Posted by CarloM
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
Code:
FILENAME=$(ls *.jpg ...

From cron you won't be in the right directory.
Actually My bad.. I gave wrong location. It is working.. This is solved the issue. Thanks a lot mate!! Smilie
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