0

Let's say I have a project called my-project/ that lives in it's own directory and has the following file structure.

my-project/
.
├── src
│   ├── index.html
│   ├── main.js
│   ├── normalize.js
│   ├── routes
│   │   ├── index.js
│   │   └── Home
│   │       ├── index.js
│   │       └── assets
│   ├── static
│   ├── store
│   │   ├── createStore.js
│   │   └── reducers.js
│   └── styles
└── project.config.js

Now let's say I have a new project called my-new-project that also lives in it's own directory and has the same file structure as my-project but it contains an additional file called my-files-to-copy.txt

my-new-project/
.
├── src
│   ├── index.html
│   ├── main.js
│   ├── normalize.js
│   ├── routes
│   │   ├── index.js
│   │   └── Home
│   │       ├── index.js
│   │       └── assets
│   ├── static
│   ├── store
│   │   ├── createStore.js
│   │   └── reducers.js
│   └── styles
├── project.config.js
└── my-files-to-copy.txt # new file added to tree

my-new-project/ has the same file structure but different file contents than my-project/

Now let's say my-files-to-copy.txt contains a list of files I want to copy from my-project/ and write to the same path in my-new-project/ to overwrite the existing files in my-new-project/ at those locations.

my-files-to-copy.txt
src/main.js
src/routes/index.js
src/store/reducers.js
project.config.js

How can I accomplish this with a terminal/bash/shell command or script?

I think I might be able to do:

cp my-project/src/main.js my-new-project/src/main.js
cp my-project/src/routes/index.js my-new-project/src/routes/index.js
cp my-project/src/store/reducers.js my-new-project/src/store/reducers.js
cp my-project/project.config.js my-new-project/project.config.js

Maybe some type of rsync command will do?

But as the number of files scales, this method will become less efficient. I was looking for a more efficient solution that would allow me to leverage the file that contains the list of files (or at least a script) without having to write a separate command for each one.

1

This is what cpio is for: copying a list of files. I always find the "in" and "out directions confusing and am glad the GNU one has --create and --extract.

 cd your/source/dir
 cpio --create < my-files-to-copy.txt | (cd your/dest/dir && cpio --extract)

There are lots of options for cpio to govern things like preserving ownership / timestamps. The man page will guide you. One big gotcha to know about is cpio will not create directories unless you (a) copy them to the stream (eg in your list of files to copy) or (b) use the --make-directories option on the extract side.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.