Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm on a dedicated server using CentOS, and on this server I have 5000+ directories in one main directory.

In the main directory I have an index.php. I would like to copy this index.php into all 5000+ directories, but the only way I know how is doing it manually.

Is there a way through the command line that I can enter something like cp and make it work from the directory? I'd copy it all the way down through all the directories and there sub directories within this main directory I am starting out in.

share|improve this question
up vote 3 down vote accepted
find . -mindepth 1 -type d -exec echo cp index.php {} \;

The above command would copy the index.php file from your current directory to all subdirectories, recursively – but not right away.

When you've made sure it prints all the correct copy commands, remove the echo and execute it. echo is just a safeguard to show you what the whole command would do before actually doing something you might not want.

Another interesting approach – thanks @RedGrittyBrick – would be to symlink the files instead of just plain copying them. This way, the index.php files in the subdirectories would all just be a shortcut, so to say, to the one in the root directory and not duplicated. To do this, in the above command, change cp to ln -s. This way, when you change the "master" index.php file, all the others in the subdirectories would still point to it and thus automatically change as well when viewed.

For completeness, the whole command, broken down, works like this:

  • find ., search in the current directory
  • -mindepth 1, going at least one directory deep
  • -type d, only find directories
  • -exec …, execute the command echo cp index.php {}, where {} is replaced with every directory found. This command is called multiple times, once for each subdirectory.
share|improve this answer
Before doing this, consider whether it might be better to use links (hard or soft) using ln rather than cp. – RedGrittyBrick Sep 23 '12 at 9:29
True, I added a paragraph on that. Thanks! – slhck Sep 23 '12 at 9:35

You must log in to answer this question.

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