copy files and create the directories if it doesn’t exist

June 3rd, 2011 mysurface Posted in dirname | Hits: 182407 | 39 Comments »

It always have a need for this simple operation.

I wanna copy certain files to a destination, but I wanna preserved the sub directories structure, any cp option to set? For examples, I wanna copy my_project/cpp/ to your_project folder, but I want it to appear as your_project/cpp/ automatically. Especially I wanna do it in a batch of file.

By just using cp command, until today, I do not see such option for it.

You need additional commands, dirname and mkdir.

You may read a list of file from a file like this

while read line
# do your copy over here
done < $filelist

First you extract the directory name of the file you wanna copy, and then create the directory, lastly copy over the file.

filelist="myfilelist.txt" # assume your filename with relative path.
while read line
  target_dir=`dirname $line`
  mkdir -p $target_dir
  cp $line $target$target_dir
done < $filelist

P.S. The snapshot of the code above might not work for all case, it depends on your filename in your file list, but gives you and idea on how to copy files and create directories if it doesn’t exist.

39 Responses to “copy files and create the directories if it doesn’t exist”

  1. How about doing a cp -R my_project/* your_project/
    Should keep your directory structure and files.
    cheers, j.

  2. Johannes, that is for all files in my_project to your_project, if you have specific set of files to copy, you can’t use * :D

  3. Guess i need another cup of coffee. heh. :)

  4. cd my_project
    cp –parent cpp/ full/path/to/your_project

  5. Make the misstake will kill yourself.

  6. From the base directory you want to copy
    {filename} | cpio -pd {destination_directory}

    I use it a lot from svn status. Simply awesome.

  7. Hi,,

    I have try this but i think a lot of mistakes in it please keep checking then update it.

  8. How about using tar?

    cd my_project
    tar cf – –files-from filelist.txt | ( cd ../your_project && tar xfp -)

  9. mv is to move a director?

  10. It can be done in many ways. The script which is used earlier is good.

  11. cd is to move into directory

  12. this is so to see good tutorials like this one..thanks man for sharing such cool stuff..linux best

  13. I think there is something wrong with this bash script.
    “mkdir -p $target_dir” should be “mkdir -p $target$target_dir”. $target_dir is the source dir.

  14. Really trying to learn this stuff…


    Jack47 Says: June 27th, 2013 at 3:17 pm

    I think there is something wrong with this bash script.
    “mkdir -p $target_dir” should be “mkdir -p \
    ‘$target_dir’ is the source dir.

    Q1 – Would “mkdir -p $target/$target_dir” make a difference? Maybe I’m misreading.


    Q2 – Is there any chance wget works locally, to maintain intended dir structure?

    Q3 – Could the {filename} be copied to a (/tmp?) directory structure, verified, then put into place with “mv /tmp/tempdir/{dirstruct} {/path/to/intended/destination}”?

    Q4 – Am I misreading/misunderstanding?

    Thanks in advance, –tb01

    PS- to whomever, displayed time of last post showed 12hrs earlier than my time… (if important, pls pass to appropriate recipient, and TIA!)

  15. @tb01:

    correction – meant 12hrs LATER than my time. –tb01

  16. Thanks for aany other wonderful post. Where else may anybody get that kind of information in suchh an ideal manner of writing?
    I’ve a presentation subsequent week, and I am on the
    look for such info.

  17. If somne one wants too be updated with newest technolovies after that
    he mst be visit this website aand be up to date everyday.

  18. Great delivery. Outstanding arguments. Keep up the amazing work.

    Lookk into my weblog … marketing organizations (Denise)

  19. you are in point of fact a good webmaster. The site
    loading pace is incredible. It kind of feels that you are doing any distinctive trick.
    In addition, The contents are masterwork.
    you’ve done a fantastic job on this topic!

  20. Good blog post. I definitely love this website. Keep it up!

  21. Someone necessarily assist too make seriously posts I’d state.
    That iss thee very first time I frequented your
    web page and thus far? I amazed with the analysiis you made to create this
    actual poset extraordinary. Wonderful task!

  22. Just simply wished to emphasize I am grateful that i happened upon your web page!.

  23. Thanks a bunch! This is an astounding webpage!.

  24. Thanks, this was helpful for me.

  25. You’ve awesome knowlwdge listed here

  26. I appreciate checking your website. Regards!

  27. Few great suggestions when it comes to optimizing website speed. Thank you for another instrumental piece of knowledge!

  28. A Desentupidora em Campinas também realiza serviços de hidrojateamento, limpeza de fossas e também
    vídeo inspeção.

  29. Who do you want to gift for wholesale Daryl Johnston jerseys?
    Get value into the page: china jerseys

  30. Thank you for this post. It has been working.
    I would like to thank you for the efforts you have made in writing this article.Thanks. Would be waiting for more stuff from your side.

    Keep Posting.

  31. What is size of 28 Justin Bethel Jersey may have?
    Know more about here – 69 Jerel Worthy Jersey

  32. A calorie isn’t always a calorie. Get crafty.

  33. dirname isn’t the best way to find the directory part of a path (especially, if the filenames contain withspace). And it’s pretty slow.

    Better use the Parameter Expansion from bash:

    filelist=”myfilelist.txt” # assume your filenames with path.
    while read line
    mkdir -p ${line%/*}
    cp $line $target/{line%/*}
    done < $filelist

  34. Sorry. Should be:

    mkdir -p ${target}/${line%/*}

    of course…

  35. It is in point of fact a nice and helpful piece of information. I am glad that you shared this useful info with us.
    Please keep us up to date like this. Thank you for sharing.

  36. Back in the 90s, a sysadmin that really knew his stuff said this was quicker than cp. (I never checked then and things might have changed now-a-days.) Change to the directory you want to mirror. You assume exists, but the command will gracefully fail if it doesn’t.

    tar cf – . | ( cd && tar xvf – )

    This gets those hidden dot files, too. I’ve always used this as an example of, “If you understand this command, you have moved beyond beginner knowledge of the shell.”

  37. It seems text inside less than and greater than signs don’t get printed.

    “You assume *target_directory* exists…”

    tar cf – . | ( cd target_directory && tar xvf – )

    It also looks like en dashes get converted to em dashes, which may not work on the CLI.

  38. cuanto tiempo antes tomar viagra viagra theme song youtube

Leave a Reply