New on LowEndTalk? Please Register and read our Community Rules.
All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.
All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.
Find and replace with my file ( CENTOS 7 ) HELP
panthera666
Member
in Help
I'm using Centos and I would like to find the files and replace them with my file in /root/game.php.
I did it the following way but it's not good.
I want the other way around.
[root@ns1 home]# grep -lr --include=*.php "dasoekdas" | xargs -I{} cp -v {} "/root/game.php"
‘xxx/domains/xxx.com/public_html/test/game.php’ -> ‘/root/game.php’
‘xxx/domains/xxx.com/public_html/game.php’ -> ‘/root/game.php’
That's how I want it to be
‘/root/game.php’ -> ‘xxx/domains/xxx.com/public_html/game.php’
Comments
Something like this:
Thank you!
I need script for find the php file that contains the text "dasoekdas" and replace it with the file in /root/game.php
Sorry I thought you were just copying based on filenames. I would probably do something like "for file in (grep statement here resulting in a list of files) ; do cp $source $file ; done"
for your initial command obviously the order of source and destination in the cp part is wrong.
with -I{} you force xargs to insert the output at the given placeholder instead of just at the end as per default.
which then puts your grep-result right before your file, moving that to be the destination instead in the cp command.
so you probably just don't need that fancy stuff to reverse source and dest:
grep -lr --include=*.php "dasoekdas" | xargs cp -v "/root/game.php
cp: target ‘xxx/domains/xxx.com/public_html/game.php’ is not a directory
sorry, you always should check yourself, what the options/flags for each command are doing... my post was just to point out that the order of src and dest was wrong - I did not bother to check on the rest at all.
you probably need a -T in the cp as well, as it normally expect the destination to be a dir and you also might think of -f to force overwrite (though I am not sure if that's really needed, rarely use cp at all ;-))
not here to preach, but please start conducting your own research on each command that you are using instead of blindly copying random stuff from the internet ;-)
Congrats on your second comment
I am just curious to know whether:
you want to learn how to write a script?
or you want to pay someone to write it for you?
This will fail ("cp: dest is not a directory") if the grep matches more than one file. You need to use xargs -L1.
If anyone wants to help me
grep -lr --include=*.php "dasoekdas" | xargs cp -Tfv "/root/game.php"
cp: extra operand ‘xxx/domains/xxx.com/public_html/game.php’
Try 'cp --help' for more information.
@panthera666 - you need to understand what you are doing otherwise you are likely to screw up your system
Make a backup
Test your script in a different directory, not as root
@deank may have more to say about this.
I hope this helps.
You're welcome.
also true... see that happens if you just focus on one part of a statement...
I'd rather use something like find -exec grep + | xargs -0 cp than direct grep anyway - or maybe rsync? ;-)
I solved. Thank you all.
good! grats on solving your issue ;-)
now would be the perfect time to post your solution, so others can learn from it as well...
Having your webroot in /root is not a good idea.
Place, chown and chmod accordingly.