Sponsored Content
Top Forums Shell Programming and Scripting copying random Jpg files to different folder Post 302570154 by Corona688 on Wednesday 2nd of November 2011 01:19:58 PM
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:
 

9 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

9. 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
All times are GMT -4. The time now is 01:52 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy