Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


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.

Find and replace with my file ( CENTOS 7 ) 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

  • raindog308raindog308 Administrator, Veteran

    Something like this:

    find /some/path -type f -name game.php -exec cp /root/game.php {} \;
    
    Thanked by 2pr0lz panthera666
  • @raindog308 said:
    Something like this:

    > find /some/path -type f -name game.php -exec cp /root/game.php {} \;
    > 

    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

  • raindog308raindog308 Administrator, Veteran

    panthera666 said: 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"
    Thanked by 1panthera666
  • 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 ;-))

    grep -lr --include=*.php "dasoekdas" | xargs cp -Tfv "/root/game.php"

    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 ;-)

    Thanked by 2uptime panthera666
  • angstromangstrom Moderator, Veteran

    @panthera666 said:

    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

    Congrats on your second comment

    Thanked by 1panthera666
  • uptimeuptime Member
    edited January 2020

    @panthera666 said:
    I need script for find the php file that contains the text "dasoekdas" and replace it with the file in /root/game.php

    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?

    Thanked by 1panthera666
  • rcxbrcxb Member
    edited January 2020

    @Falzo said:
    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"

    This will fail ("cp: dest is not a directory") if the grep matches more than one file. You need to use xargs -L1.

    Thanked by 2Falzo panthera666
  • 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.

    Thanked by 1panthera666
  • @rcxb said:

    @Falzo said:
    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"

    This will fail ("cp: dest is not a directory") if the grep matches more than one file. You need to use xargs -L1.

    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? ;-)

    Thanked by 1panthera666
  • 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...

  • JanevskiJanevski Member
    edited January 2020

    @panthera666 said:
    I solved. Thank you all.

    Having your webroot in /root is not a good idea.

    Place, chown and chmod accordingly.

Sign In or Register to comment.