I actually tried an existing answer from: How to copy a file to multiple folders using the command line? where I used the command:

find folder -exec cp file.txt {} \;

I wanted to use this because I wanted to copy one file named 'file.txt' to a directory named 'folder' and all its sub-directories without mentioning all the directories in the command.

However, what this ended up doing was replacing the contents of every file in 'folder' and its subfolders with the contents of 'file.txt'. For example, if there was a 'foo.txt' inside folder, it would still retain the name 'foo.txt' but all of its contents would be replaced by that of 'file.txt'. This perplexed me and I tried the following interactive flag:

find folder -exec cp -i file.txt {} \;

and I got overwrite prompts for every single file in the directory 'folder' and all of its sub-directories, even though the names were all different.

Can someone please help me figure out what's going on and what the appropriate command should be?

1 Answer 1


You should restrict find to only listing directories:

find folder -type d -exec cp file.txt {} \;

Additionally, in future, when you're cp'ing to a directory where the directory name comes from some other command, use the -t option to make sure it actually is a directory:

find folder -type d -exec cp -t {} file.txt \;

From man cp:

-t, --target-directory=DIRECTORY
      copy all SOURCE arguments into DIRECTORY

So, if a file is provided as an argument to -t, cp will automatically complain.

Another answer to that question does use -type d.

  • Thank you, this works just right. Yes, I realise now that this answer was already available, however, it wasn't voted up as much. The one which you just corrected had the most up-votes for a solution I was looking for and also none of the comments pointed out this major flaw. I guess I made a wrong judgement call and should have done more research. Thanks for your help, I have a better understanding of find and cp now. Mar 10, 2016 at 2:44
  • @ShudiptoAmin you're welcome. It's my guess that both OP and answerers for that Q assumed no directory had files, only subdirectories (note, even OP - nux - had edited that answer). Frankly, I'm amazed no one pointed it out sooner, which is why I just edited it straight away.
    – muru
    Mar 10, 2016 at 2:53
  • yes that must be it. I suppose even if it was tested out by someone, they simply created empty directories and checked to see if the file was copied, which it would be. But its good to know that the answer to that question is now complete, thanks again! Mar 10, 2016 at 3:09

You must log in to answer this question.

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