Link to home
Start Free TrialLog in
Avatar of rebane
rebane

asked on

Folder copy script in sh shell

I have to make a script which loops through folders and subfolders (given parameter 1) and if folder contains file "found.txt" then the folder has to be copied to another folder (given parameter 2).

ex.
/doc/t1/found.txt
/doc/t2/found.txt
/doc/t3/
/doc/t4/a1/
/doc/t4/a2/found.txt
/doc/t4/a3/

and after command:
sh mycopy.sh /doc /new

There would be:

/new/t1/ (with found.txt and other files inside)
/new/t2/ (with found.txt and other files inside)
/new/a2/ (with found.txt and other files inside)
Avatar of sunnycoder
sunnycoder
Flag of India image

Hi rebane,

for dirname in `find $1 -name "found.txt" -type f | sed 's:\(.*\)/.*:\1:'`
do
       cp -r $dirname $2
done

Cheers!
Sunnycoder
Avatar of rebane
rebane

ASKER

Hei sunnycoder! That was quick :P But i tried it and it seemed to copy only the first instance of dir where is found.txt inside. And i can't find a problem why it doesn't copy the others?!
ASKER CERTIFIED SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rebane

ASKER

Thanks, i got it working! : )