copy mutilple files to mutiple folders


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers copy mutilple files to mutiple folders
# 1  
Old 01-10-2011
copy mutilple files to mutiple folders

Hi,

I just started to learn shell progamming and just can't get my head around the following problem.

I need to do the following:

I have a folder which contains 100+ subfolders. Inside these subfolders there is one folder named 'Morph' and several jpg's.
I need to copy all the files into each Morph folder

So:

MainFolder
|
__FolderA
____File_01
____File_02
____FolderMorph
__FolderB
____File_01
____File_02
____FolderMorph
__FolderC
____File_01
____File_02
____FolderMorph
|
etc..

I tried the following to do:
find . -depth 2 -type f -exec cp {} Morph/ \;

But this won't work.
Someone any idea?

thanks in advance

Martijn
# 2  
Old 01-10-2011
One way using "ls" rather than "find". It first lists directory entries two depths down then copies only the files to the named subdirectory. Script assumes that the subdirectory "Morph" exists and displays an error if it does not exist.
The key line which does the "cp" is prefixed by an "echo" for testing purposes so you can check what commands would be executed.

Code:
ls -1d /MainFolder/*/* 2>/dev/null | while read FILENAME
do
   if [ -f "${FILENAME}" ]
   then
        DIR=`dirname "${FILENAME}"`
        if [ -d "${DIR}/Morph" ]
        then
             # Remove echo when tested
             echo cp -p "${FILENAME}" "${DIR}/Morph"
        else
             echo "Directory missing: ${DIR}/Morph"
        fi
   fi
done

Btw. There is much variation in the "find" command. However there is no number parameter to the "-depth" parameter. Some versions of "find" have "-maxdepth" and "-mindepth" (mine doesn't).
Always post what Operating System and version you have and what Shell you are using.
# 3  
Old 01-10-2011
MySQL

hi Methyl,

may thanks!

will have a look at it.
next time I'll mention the OS & shell (which is osx 10.6.5 & bash)

Martijn
# 4  
Old 01-10-2011
Good luck.
Definitely mention if it is a MAC.
Some MACOS commands and functions are quite different from both mainstream unix and Linux (which are themselves different).
# 5  
Old 01-10-2011
Hi Methyl,

Got it working already! You saved me a lot of work.. moving up to 280.000 files...
It's my first post to this forum and usually I post to native MAC forums..
thanks once again!
Martijn
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to copy files/folders and show the files/folders?

Hi, So i know we use cp -r as a basic to copy folders/files. I would like this BUT i would like to show the output of the files being copied. With the amazing knowledge i have i have gone as far as this: 1) find source/* -exec cp -r {} target/ \; 2) for ObjectToBeCopied in `find... (6 Replies)
Discussion started by: Imre
6 Replies

2. HP-UX

Recursive copy of Folders with files

Dear All, I will appreciate any help received. Our system is running on hpux v1 My problem is as follows: We have many customer folders with name fd000100, fd000101 and so on e.g. (Testrun)(testsqa):/>ll /TESTrun/fd000100 total 48 drwxrwx--- 2 fq000100 test 96 Jun 27 2004... (17 Replies)
Discussion started by: mhbd
17 Replies

3. Shell Programming and Scripting

Linux Script to compare two folders and copy missing files

Hi, I need help in shell scripting. If someone can help me, that would be great! Problem. I want Linux Script to compare two folders and copy missing files. Description. I have two directories /dir1 /dir2 I need to copy all distinct/new/unique/missing files from /dir1 and that... (1 Reply)
Discussion started by: S.Praveen Kumar
1 Replies

4. Shell Programming and Scripting

Find files of type within folder copy multiple results to different folders

Ok question number two: I'd like to search a directory for multiple file types (rar, txt, deb) and depending on what's found, copy those files to folders named Rar, TextFiles, and Debs. I'm looking for speed here so the faster the script the better. I want it to be a function that I pass 1 argument... (4 Replies)
Discussion started by: DC Slick
4 Replies

5. Shell Programming and Scripting

Loop folders, delete files, copy new ones

Folks, I am hopeful that you may be able to help me out with writing a script that can be run nightly (as cron?) to loop through all subfolders within the "/media" directory, delete all of the files in each of them, and then copy in all of the files from the "/home//sansa" directory to each of... (6 Replies)
Discussion started by: acraig
6 Replies

6. Windows & DOS: Issues & Discussions

Windows mass copy files with same name in differnt folders

I have files existing with same names in the folders with date as display below c:\2010-09-10 <==== folder arr1.jpg arr2.jpg arr3.jpg arr4.jpg c:\2010-09-09 <==== folder arr1.jpg arr2.jpg c:\2010-09-08 <==== folder arr2.jpg arr3.jpg arr4.jpg ... (5 Replies)
Discussion started by: jville
5 Replies

7. Shell Programming and Scripting

I need script Copy permissions of files and folders from one server to another

Hi.. I have 2 servers with linux suse10. I made a mistake and on one of the servers changed with chmod the permission of root in directory /. In the other servers the permissions are correct Please i need a script, to change the permissions of one server 1, using the same permission of the... (11 Replies)
Discussion started by: ave-phoenix
11 Replies

8. Shell Programming and Scripting

Helppppppp I need script Copy permissions of files and folders from one server to another

Helpppppppppppppp Hi.. I have 2 servers with linux suse10. I made a mistake and on one of the servers changed with chmod the permission of root in directory /. In the other servers the permissions are correct Please i need a script, to change the permissions of one server 1, using... (1 Reply)
Discussion started by: ave-phoenix
1 Replies

9. Shell Programming and Scripting

copy some files from users home folders to my folder

i have users home directories in /home all the users have some files starting with character e and i want to copy all these files in a folder in my (root) home using a script i tried the script for i in m5 do cd m5 cp e1* /home/pc/exam cd .. done but get these... (3 Replies)
Discussion started by: pcrana
3 Replies

10. UNIX for Dummies Questions & Answers

copy all files and folders and cjange or remove ownership

So tried: cp -r -p test1/ user@machine:///srv/www/vhosts/domain.co.uk/httpdocs/backup/ but this didn't work either :( Anyone able to help with this? Many thanks Mr M (3 Replies)
Discussion started by: misterm
3 Replies
Login or Register to Ask a Question