Howdy, Stranger!

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

Subscribe to our newsletter

Advertise on LowEndTalk.com

Latest LowEndBox Offers

    Find and replace with my file ( CENTOS 7 ) HELP
    New on LowEndTalk? Please read our 'Community Rules' by clicking on it in the right menu!

    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.
    [[email protected] 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:

      find /some/path -type f -name game.php -exec cp /root/game.php {} \;
      
      Thanked by 2pr0lz panthera666

      For LET support, please visit the interim support desk.

      Over the past few months we have been met with many challenges within the moderation business. Some that have not been overcome.

    • @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

    • 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 LET support, please visit the interim support desk.

      Over the past few months we have been met with many challenges within the moderation business. Some that have not been overcome.

    • FalzoFalzo Member

      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

      most recommended Provider: First-Root KVM Power-Edition /w SSD
      UltraVPS.eu KVM in US/UK/NL/DE: 15% off first 6 month | Netcup VPS/rootDS - 5€ off: 36nc15279180197 (ref)

    • 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

    • FalzoFalzo Member

      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

      most recommended Provider: First-Root KVM Power-Edition /w SSD
      UltraVPS.eu KVM in US/UK/NL/DE: 15% off first 6 month | Netcup VPS/rootDS - 5€ off: 36nc15279180197 (ref)

    • @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

      "Linux will run happily with only 4 MB of RAM, including all of the bells and whistles such as the X Window System, Emacs, and so on." (M. Welsh & L. Kaufman, Running Linux, 2e, 1996, p. 32)

    • uptimeuptime Member
      edited January 12

      @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

      the Amitz.party lives on!

    • rcxbrcxb Member
      edited January 12

      @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

      the Amitz.party lives on!

    • FalzoFalzo Member

      @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

      most recommended Provider: First-Root KVM Power-Edition /w SSD
      UltraVPS.eu KVM in US/UK/NL/DE: 15% off first 6 month | Netcup VPS/rootDS - 5€ off: 36nc15279180197 (ref)

    • I solved. Thank you all.

    • FalzoFalzo Member

      good! grats on solving your issue ;-)
      now would be the perfect time to post your solution, so others can learn from it as well...

      most recommended Provider: First-Root KVM Power-Edition /w SSD
      UltraVPS.eu KVM in US/UK/NL/DE: 15% off first 6 month | Netcup VPS/rootDS - 5€ off: 36nc15279180197 (ref)

    • JanevskiJanevski Member
      edited January 13

      @panthera666 said:
      I solved. Thank you all.

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

      Place, chown and chmod accordingly.

      You are dreaming. | And it's a nightmare. | THE SECRET THREAD | THE TRUTH | HAVES YOU SEEN THIS YURA?
      „Homo homini rattus.“ | It's not nightmare, it's reality, but it's still nightmare.

    Sign In or Register to comment.