Linux ‘find’ example: How to copy one file to many directories

Did you ever need to take one file on a Linux or Unix system and copy it to a whole bunch of other directories? I had this problem recently when I changed some of the header files on this website. I had a file named header.html, and I needed to copy it to a bunch of subdirectories.

Using Unix, Linux, or Cygwin this turns out to be easy. To solve the problem, I used the Linux find command in combination with the cp command. Once I figured out the right syntax, I was able to copy one file to nearly 500 directories in just a few seconds.

My Linux find & copy example

Here’s an example that shows how I did it:

find dir1 dir2 dir3 dir4 -type d -exec cp header.html {} \;

Here’s what this command does:

  1. First, I use the Linux find command, and I tell it to look in four sub-directories (dir1, dir2, dir3 and dir4).
  2. I tell it to find only directories (-type d).
  3. I then issue the Linux copy (cp) command, and copy the file header.html to each directory that is found, one directory at a time.

The crazy syntax at the end of the command line is something that you have to do with the find command. Honestly, I don’t know too much about it, other than the fact that it is required when you do something like this. If you’re interested in it, you can find more details about the find command at this find command man page listing.