I am attempting to copy a file to a bunch of folders present in a directory. The folders have been saved in propagation.txt and are like:
sfproject/folder1
sfproject/folder2
The code I am attempting to run is :
for x in `cat propagation.txt`
do cp php.ini $x ; echo "Copied php.ini to $x"
done
echo "Finished";
However, it states that:
cp : 'target: 'propagation.txt' is not a text'
This is what propagation.txt consists of:
sfproject/apps/backend/modules/users/lib
sfproject/apps/backend/templates
sfproject/apps/frontend/config
sfproject/apps/frontend/lib
sfproject/apps/frontend/modules/EdboostSatGuide/actions
sfproject/apps/frontend/modules/EdboostSatGuide/templates
sfproject/apps/frontend/modules/dashboard/actions
sfproject/apps/frontend/modules/dashboard/templates
sfproject/apps/frontend/modules/quizzes/actions
sfproject/apps/frontend/modules/quizzes/templates
sfproject/apps/frontend/templates
sfproject/cache/frontend/prod/config
"Target is not a directory" is a cp error. It happens when you have three or more arguments, and the last argument is an existing filesystem object that isn't a directory!
Of course
cp a b
works if b is a regular file, and not a directory. b is simply replaced with a. But
cp a b c
means make a copy of a and b inside directory c.
It's not clear how this problem could happen in your cp php.ini $x, because the loop variable $x is iterating over the results of a cat ... process substitution. The process substitution undergoes word splitting and the split pieces are assigned to x one by one. For your cp to end up with three or more arguments, $x would have to somehow expand into two arguments.
Post the actual verbatim code that is failing and some sample data.
On a related note, if you do have spaces in some of the path names, it will be best to put them on separate lines in propagation.txt and then process it like this:
# read dir names one by one and copy php.ini into them ...
while read dir ; do
cp php.ini "$dir"
# ...
done < propagation.txt # ... getting the names from propagation.txt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With