UNIX cp command question

software development


I need to copy a single file into multiple (hundreds) of subdirectories. How can i do this?

Basically all subdirectories in a single folder need to have a single file copied into them.



I’d use a loop - something like (from a bash, zsh or ksh shell):
$ for a in 1 2 3 4 5 6 ; do cp yourfile $a ; done
(where 1 2 3 4 5 6 are the names of the directories you want to copy to). you can automate that process further depending on how the directories are setup; i.e., find an ls command that lists all the directories you want to copy to, and use backticks to put that in the loop).

If the file you’re copying isn’t a script, I’d use a symbolic link instead of copying the file, though.


hmmm, well that got me started.

but i’m getting lost with the backticks and ls command.

all i have in this folder are subfolders, and each and every one of them needs to have a file copied into it.

i tried

for a ls *; do cp myFile $a; done;

yea… that didn’t work to well! haha


errr durr…

just realized i had put a * wildcard in with my ls command…

never mind :slight_smile:


Just a side note - I’d recommend using “/bin/ls” for stuff like that, just in case you (or the administrator of the system you’re on) has aliased ls to “ls --color” or “ls -F” or anything funky like that.


well here’s what i ended up using and it seemed to work like a champ:

for a in ls -d *; do cp myFile $a; done;

thanks for all your help :slight_smile: