cp- copy a file in many folders, but only if it already exists there
I would not parse the output of ls
, use the find
command instead:
find . -type f -name "info.txt" -exec cp -v ../y/info.txt {} \;
Note that the -v
option with cp
isn't necessary, I just like to see what's being copied where.
To address a comment, the find
command shown above searches the entire PWD
. If you want to limit the the search to just first level subdirectories of the PWD
then add -maxdepth 2
to the find
command, e.g.:
find . -maxdepth 2 -type f -name "info.txt" -exec cp -v ../y/info.txt {} \;
In this scenario:
.
├── a
│ ├── 1
│ │ └── info.txt
│ └── info.txt
├── b
│ └── info.txt
└── c
Only ./a/info.txt
and ./b/info.txt
are replaced, ./a/1/info.txt
is not.
Assuming somewhat sane directory names (no newlines etc.)
ls | while read dir; do
[[ -e "$dir"/info.txt ]] && cp ../y/info.txt "$dir"/
done