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

June 3rd, 2011 mysurface Posted in dirname | Hits: 105781 | 15 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/hello.cc to your_project folder, but I want it to appear as your_project/cpp/hello.cc 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


filelist="myfilelist.txt"
while read line
do
# 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.
target="your_project"
while read line
do
  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.

15 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/hello.cc 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.
    Thanks

  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 true..love 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:

    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$target_dir”.
    ‘$target_dir’ is the source dir.

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

    @mysurface:

    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

Leave a Reply