2

I use the following command to copy a folder (named 'dist') containing a react project to my frontend folder:

cd apps/react-app
sudo cp -R dist ../server/frontend/client

The first time I run the 'cp'-command, the content of the 'dist'-folder gets copied into the 'client'-folder. I would expect that when I run this command again, the cp-command would overwrite the files in the 'client'-folder. This doesn't happen, instead, the 'cp'-command now creates a 'dist'-folder inside the 'client'-folder en puts all the new files in there. The 3th, 4th, ... time I run the 'cp'-command, it overwrites all the files in the 'dist'-folder, but the files from the first 'cp'-command are still in the 'client'-folder also.

How can I cp the files from the 'dist'-folder to the 'client'-folder, and whenever I run this command again, it overwrites (instead of creating a new folder)?

2
  • 1
    Does the client directory already exist before all of this?
    – DonHolgo
    Apr 27, 2021 at 7:52
  • Thanks. I hadn't realized this in 25 years. Apr 27, 2021 at 11:39

1 Answer 1

0

Yes, cp overwrites. It is idempotent.

The first time I run the 'cp'-command, the content of the 'dist'-folder gets copied into the 'client'-folder.

Are you sure? Normally, the folder itself should be copied (including its content), and indeed that is want you see in the next runs.

How can I cp the files from the 'dist'-folder to the 'client'-folder,

With

cp -r dist/* client
12
  • 1
    cp is not naturally idempotent. You have to use cp -r dist/. client for it be so Apr 26, 2021 at 23:59
  • Actually you can't copy a directory without -r. Apr 27, 2021 at 0:21
  • Can you show an example? The original poster seems to have one but I doubt it. Apr 27, 2021 at 0:23
  • The OP's example is valid Apr 27, 2021 at 5:03
  • Yes, the content of the 'dist' folder is copied into the 'client' folder the first time, not the folder itself. The second time, a 'dist' folder shows up with all that content again.
    – Sam Leurs
    Apr 27, 2021 at 10:29

Not the answer you're looking for? Browse other questions tagged .