Folder copy script in sh shell

Posted on 2006-05-16
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).


and after command:
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)
Question by:rebane
    LVL 45

    Expert Comment

    Hi rebane,

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

    LVL 1

    Author Comment

    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?!
    LVL 45

    Accepted Solution

    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
           cp -r $dirname $destination
    LVL 1

    Author Comment

    Thanks, i got it working! : )

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
    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…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    729 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now