Recursively add a file to all sub-directories
How about:
find . -type d -exec cp file {} \;
From man find
:
-type c
File is of type c:
d directory
-exec command ;
Execute command; All following arguments to find are taken
to be arguments to the command until an argument consisting
of `;' is encountered. The string `{}' is replaced by the
current file
So, the command above will find all directories and run cp file DIR_NAME/
on each of them.
If you just want to create an empty file, you can use touch
and a shell glob. In zsh:
touch **/*(/e:REPLY+=/file:)
In bash:
shopt -s globstar
for d in **/*/; do touch -- "$d/file"; done
Portably, you can use find
:
find . -type d -exec sh -c 'for d; do touch "$d/file"; done' _ {} +
Some find
implementations, but not all, let you write find . -type d -exec touch {}/file \;
If you want to copy some reference content, then you'll have to call find
in a loop. In zsh:
for d in **/*(/); do cp -p reference_file "$d/file"; done
In bash:
shopt -s globstar
for d in **/*/; do cp -p reference_file "$d/file"; done
Portably:
find . -type d -exec sh -c 'for d; do cp -p reference_file "$d/file"; done' _ {} +
When wanting to touch
files called $name in the current directory and in all subdirectories, this will work:
find . -type d -exec touch {}/"${name}" \;
Note that the comment by ChuckCottrill to the answer by terdon does NOT work, as it will only touch
the file called $name in the current directory and the directory itself.
It will not create files in subdirectories as requested by the OP, while this version here will.