?
Solved

Folder copy script in sh shell

Posted on 2006-05-16
4
Medium Priority
?
286 Views
Last Modified: 2012-06-21
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)
0
Comment
Question by:rebane
  • 2
  • 2
4 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 16692812
Hi rebane,

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

Cheers!
Sunnycoder
0
 
LVL 1

Author Comment

by:rebane
ID: 16692972
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?!
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 1000 total points
ID: 16693031
Try running
find $1 -name "found.txt" -type f | sed 's:\(.*\)/.*:\1:'
Does this give all the instances?

Next, replace cp command with echo $dirname
Are all instances printed properly?

Try this alternative loop and see if this works

export destination=$2

find $1 -name "found.txt" -type f | sed 's:\(.*\)/.*:\1:' | while read dirname
do
       cp -r $dirname $destination
done
0
 
LVL 1

Author Comment

by:rebane
ID: 16693110
Thanks, i got it working! : )
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question