Find file while other file exist in same directory Announcing the arrival of Valued Associate...

How to write this math term? with cases it isn't working

Is a ledger board required if the side of my house is wood?

Did Deadpool rescue all of the X-Force?

A term for a woman complaining about things/begging in a cute/childish way

Denied boarding although I have proper visa and documentation. To whom should I make a complaint?

Dating a Former Employee

What is the difference between globalisation and imperialism?

Can anything be seen from the center of the Boötes void? How dark would it be?

Why does it sometimes sound good to play a grace note as a lead in to a note in a melody?

Why wasn't DOSKEY integrated with COMMAND.COM?

Can a new player join a group only when a new campaign starts?

Has negative voting ever been officially implemented in elections, or seriously proposed, or even studied?

Crossing US/Canada Border for less than 24 hours

Converted a Scalar function to a TVF function for parallel execution-Still running in Serial mode

Time to Settle Down!

How were pictures turned from film to a big picture in a picture frame before digital scanning?

How does light 'choose' between wave and particle behaviour?

How come Sam didn't become Lord of Horn Hill?

Using audio cues to encourage good posture

Do wooden building fires get hotter than 600°C?

How to react to hostile behavior from a senior developer?

What is the appropriate index architecture when forced to implement IsDeleted (soft deletes)?

How to play a character with a disability or mental disorder without being offensive?

Should I use a zero-interest credit card for a large one-time purchase?



Find file while other file exist in same directory



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)
2019 Community Moderator Election Results
Why I closed the “Why is Kali so hard” questionReading lines from a file with bash: for vs. whileHow to name a file in the deepest level of a directory treefind -print always prints the starting directoryFind Command: File Path vs -name ArgumentCheck exist file on another pcFind extracted directory name from tar fileWhile read from file with sequence numberLinux: Find the second biggest file/directory in size using commandsHow to rename a file to have the same name and extension as another file in same directoryFind command - file path with white space





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







0















I'm trying to do "one line script" or really small bash script.



It have to find file (for example ./xxx/one.php) and if that file exist edit (with printf or echo) other file IN SAME directory (for example ./xxx/test.php).



Right now I made second part - editing existing file, but I don't have idea how to, or where to enter "if".



Actually I have something like that



for file in `find . -name "test*.php"` ; do

(printf "It's me, on first line n and me on second linen" && cat $file) > "$file".bak && mv "$file".bak "$file"

done


It's should be something like this?



if [[ `find . -name "one*.php` ]]; do

for file in `find . -name "test*.php"` ; do

(printf "It's me, on first line n and me on second linen" && cat $file) > "$file".bak && mv "$file".bak "$file"

done
done









share|improve this question





























    0















    I'm trying to do "one line script" or really small bash script.



    It have to find file (for example ./xxx/one.php) and if that file exist edit (with printf or echo) other file IN SAME directory (for example ./xxx/test.php).



    Right now I made second part - editing existing file, but I don't have idea how to, or where to enter "if".



    Actually I have something like that



    for file in `find . -name "test*.php"` ; do

    (printf "It's me, on first line n and me on second linen" && cat $file) > "$file".bak && mv "$file".bak "$file"

    done


    It's should be something like this?



    if [[ `find . -name "one*.php` ]]; do

    for file in `find . -name "test*.php"` ; do

    (printf "It's me, on first line n and me on second linen" && cat $file) > "$file".bak && mv "$file".bak "$file"

    done
    done









    share|improve this question

























      0












      0








      0








      I'm trying to do "one line script" or really small bash script.



      It have to find file (for example ./xxx/one.php) and if that file exist edit (with printf or echo) other file IN SAME directory (for example ./xxx/test.php).



      Right now I made second part - editing existing file, but I don't have idea how to, or where to enter "if".



      Actually I have something like that



      for file in `find . -name "test*.php"` ; do

      (printf "It's me, on first line n and me on second linen" && cat $file) > "$file".bak && mv "$file".bak "$file"

      done


      It's should be something like this?



      if [[ `find . -name "one*.php` ]]; do

      for file in `find . -name "test*.php"` ; do

      (printf "It's me, on first line n and me on second linen" && cat $file) > "$file".bak && mv "$file".bak "$file"

      done
      done









      share|improve this question














      I'm trying to do "one line script" or really small bash script.



      It have to find file (for example ./xxx/one.php) and if that file exist edit (with printf or echo) other file IN SAME directory (for example ./xxx/test.php).



      Right now I made second part - editing existing file, but I don't have idea how to, or where to enter "if".



      Actually I have something like that



      for file in `find . -name "test*.php"` ; do

      (printf "It's me, on first line n and me on second linen" && cat $file) > "$file".bak && mv "$file".bak "$file"

      done


      It's should be something like this?



      if [[ `find . -name "one*.php` ]]; do

      for file in `find . -name "test*.php"` ; do

      (printf "It's me, on first line n and me on second linen" && cat $file) > "$file".bak && mv "$file".bak "$file"

      done
      done






      linux bash find for






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 6 hours ago









      TheTanaduTheTanadu

      104




      104






















          1 Answer
          1






          active

          oldest

          votes


















          0














          The -execdir option to find is handy here:



          find -name 'one*.php' -execdir bash -c '
          if [ -e test.php ] ; then
          printf ... && mv test.bak test.php
          fi' {} ;


          -execdir is




          Like -exec, but the specified command is run from the subdirectory containing the matched file, which is not normally the directory in which you started find.




          -execdir will run the given command in the directory the found file was in, so the test.php file "next to" it will be right there to manipulate. Here we launch bash in that directory and run a small script there to make the manipulations you want. There's no need to complex path manipulations or re-finding the file. Above I check that the file exists, but if it will always be there (or you want to create it unconditionally) you can take the if out.



          -execdir is a GNU find extension, not in POSIX, but you very likely have that given your tags. One caveat is that your PATH environment variable can't contain . or any other relative paths (including an empty element), for security reasons, so if your ambient one does you'll need to reset it first: PATH=... find ....






          share|improve this answer
























          • So because my PATH can't contain dot... it will by default search in current directory?

            – TheTanadu
            5 hours ago











          • If you have a . or empty element in PATH, executables will be searched for in the current directory, but the default is that they are not. That part is just a caveat about when find -execdir will refuse to run, which is confusing to encounter otherwise.

            – Michael Homer
            5 hours ago











          • Oh ok, thank you for explaining.

            – TheTanadu
            5 hours ago











          • I found that when I pasted that into .sh executable file and run it I get bash skrypt.sh: line 4: unexpected EOF while looking for matching `"' skrypt.sh: line 6: syntax error: unexpected end of file

            – TheTanadu
            5 hours ago











          • You're missing a closing ", somewhere. I don't know where, because that's something you've added, but the opening quote should be on line 4.

            – Michael Homer
            5 hours ago












          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "106"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f513311%2ffind-file-while-other-file-exist-in-same-directory%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          The -execdir option to find is handy here:



          find -name 'one*.php' -execdir bash -c '
          if [ -e test.php ] ; then
          printf ... && mv test.bak test.php
          fi' {} ;


          -execdir is




          Like -exec, but the specified command is run from the subdirectory containing the matched file, which is not normally the directory in which you started find.




          -execdir will run the given command in the directory the found file was in, so the test.php file "next to" it will be right there to manipulate. Here we launch bash in that directory and run a small script there to make the manipulations you want. There's no need to complex path manipulations or re-finding the file. Above I check that the file exists, but if it will always be there (or you want to create it unconditionally) you can take the if out.



          -execdir is a GNU find extension, not in POSIX, but you very likely have that given your tags. One caveat is that your PATH environment variable can't contain . or any other relative paths (including an empty element), for security reasons, so if your ambient one does you'll need to reset it first: PATH=... find ....






          share|improve this answer
























          • So because my PATH can't contain dot... it will by default search in current directory?

            – TheTanadu
            5 hours ago











          • If you have a . or empty element in PATH, executables will be searched for in the current directory, but the default is that they are not. That part is just a caveat about when find -execdir will refuse to run, which is confusing to encounter otherwise.

            – Michael Homer
            5 hours ago











          • Oh ok, thank you for explaining.

            – TheTanadu
            5 hours ago











          • I found that when I pasted that into .sh executable file and run it I get bash skrypt.sh: line 4: unexpected EOF while looking for matching `"' skrypt.sh: line 6: syntax error: unexpected end of file

            – TheTanadu
            5 hours ago











          • You're missing a closing ", somewhere. I don't know where, because that's something you've added, but the opening quote should be on line 4.

            – Michael Homer
            5 hours ago
















          0














          The -execdir option to find is handy here:



          find -name 'one*.php' -execdir bash -c '
          if [ -e test.php ] ; then
          printf ... && mv test.bak test.php
          fi' {} ;


          -execdir is




          Like -exec, but the specified command is run from the subdirectory containing the matched file, which is not normally the directory in which you started find.




          -execdir will run the given command in the directory the found file was in, so the test.php file "next to" it will be right there to manipulate. Here we launch bash in that directory and run a small script there to make the manipulations you want. There's no need to complex path manipulations or re-finding the file. Above I check that the file exists, but if it will always be there (or you want to create it unconditionally) you can take the if out.



          -execdir is a GNU find extension, not in POSIX, but you very likely have that given your tags. One caveat is that your PATH environment variable can't contain . or any other relative paths (including an empty element), for security reasons, so if your ambient one does you'll need to reset it first: PATH=... find ....






          share|improve this answer
























          • So because my PATH can't contain dot... it will by default search in current directory?

            – TheTanadu
            5 hours ago











          • If you have a . or empty element in PATH, executables will be searched for in the current directory, but the default is that they are not. That part is just a caveat about when find -execdir will refuse to run, which is confusing to encounter otherwise.

            – Michael Homer
            5 hours ago











          • Oh ok, thank you for explaining.

            – TheTanadu
            5 hours ago











          • I found that when I pasted that into .sh executable file and run it I get bash skrypt.sh: line 4: unexpected EOF while looking for matching `"' skrypt.sh: line 6: syntax error: unexpected end of file

            – TheTanadu
            5 hours ago











          • You're missing a closing ", somewhere. I don't know where, because that's something you've added, but the opening quote should be on line 4.

            – Michael Homer
            5 hours ago














          0












          0








          0







          The -execdir option to find is handy here:



          find -name 'one*.php' -execdir bash -c '
          if [ -e test.php ] ; then
          printf ... && mv test.bak test.php
          fi' {} ;


          -execdir is




          Like -exec, but the specified command is run from the subdirectory containing the matched file, which is not normally the directory in which you started find.




          -execdir will run the given command in the directory the found file was in, so the test.php file "next to" it will be right there to manipulate. Here we launch bash in that directory and run a small script there to make the manipulations you want. There's no need to complex path manipulations or re-finding the file. Above I check that the file exists, but if it will always be there (or you want to create it unconditionally) you can take the if out.



          -execdir is a GNU find extension, not in POSIX, but you very likely have that given your tags. One caveat is that your PATH environment variable can't contain . or any other relative paths (including an empty element), for security reasons, so if your ambient one does you'll need to reset it first: PATH=... find ....






          share|improve this answer













          The -execdir option to find is handy here:



          find -name 'one*.php' -execdir bash -c '
          if [ -e test.php ] ; then
          printf ... && mv test.bak test.php
          fi' {} ;


          -execdir is




          Like -exec, but the specified command is run from the subdirectory containing the matched file, which is not normally the directory in which you started find.




          -execdir will run the given command in the directory the found file was in, so the test.php file "next to" it will be right there to manipulate. Here we launch bash in that directory and run a small script there to make the manipulations you want. There's no need to complex path manipulations or re-finding the file. Above I check that the file exists, but if it will always be there (or you want to create it unconditionally) you can take the if out.



          -execdir is a GNU find extension, not in POSIX, but you very likely have that given your tags. One caveat is that your PATH environment variable can't contain . or any other relative paths (including an empty element), for security reasons, so if your ambient one does you'll need to reset it first: PATH=... find ....







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 5 hours ago









          Michael HomerMichael Homer

          51.4k8142179




          51.4k8142179













          • So because my PATH can't contain dot... it will by default search in current directory?

            – TheTanadu
            5 hours ago











          • If you have a . or empty element in PATH, executables will be searched for in the current directory, but the default is that they are not. That part is just a caveat about when find -execdir will refuse to run, which is confusing to encounter otherwise.

            – Michael Homer
            5 hours ago











          • Oh ok, thank you for explaining.

            – TheTanadu
            5 hours ago











          • I found that when I pasted that into .sh executable file and run it I get bash skrypt.sh: line 4: unexpected EOF while looking for matching `"' skrypt.sh: line 6: syntax error: unexpected end of file

            – TheTanadu
            5 hours ago











          • You're missing a closing ", somewhere. I don't know where, because that's something you've added, but the opening quote should be on line 4.

            – Michael Homer
            5 hours ago



















          • So because my PATH can't contain dot... it will by default search in current directory?

            – TheTanadu
            5 hours ago











          • If you have a . or empty element in PATH, executables will be searched for in the current directory, but the default is that they are not. That part is just a caveat about when find -execdir will refuse to run, which is confusing to encounter otherwise.

            – Michael Homer
            5 hours ago











          • Oh ok, thank you for explaining.

            – TheTanadu
            5 hours ago











          • I found that when I pasted that into .sh executable file and run it I get bash skrypt.sh: line 4: unexpected EOF while looking for matching `"' skrypt.sh: line 6: syntax error: unexpected end of file

            – TheTanadu
            5 hours ago











          • You're missing a closing ", somewhere. I don't know where, because that's something you've added, but the opening quote should be on line 4.

            – Michael Homer
            5 hours ago

















          So because my PATH can't contain dot... it will by default search in current directory?

          – TheTanadu
          5 hours ago





          So because my PATH can't contain dot... it will by default search in current directory?

          – TheTanadu
          5 hours ago













          If you have a . or empty element in PATH, executables will be searched for in the current directory, but the default is that they are not. That part is just a caveat about when find -execdir will refuse to run, which is confusing to encounter otherwise.

          – Michael Homer
          5 hours ago





          If you have a . or empty element in PATH, executables will be searched for in the current directory, but the default is that they are not. That part is just a caveat about when find -execdir will refuse to run, which is confusing to encounter otherwise.

          – Michael Homer
          5 hours ago













          Oh ok, thank you for explaining.

          – TheTanadu
          5 hours ago





          Oh ok, thank you for explaining.

          – TheTanadu
          5 hours ago













          I found that when I pasted that into .sh executable file and run it I get bash skrypt.sh: line 4: unexpected EOF while looking for matching `"' skrypt.sh: line 6: syntax error: unexpected end of file

          – TheTanadu
          5 hours ago





          I found that when I pasted that into .sh executable file and run it I get bash skrypt.sh: line 4: unexpected EOF while looking for matching `"' skrypt.sh: line 6: syntax error: unexpected end of file

          – TheTanadu
          5 hours ago













          You're missing a closing ", somewhere. I don't know where, because that's something you've added, but the opening quote should be on line 4.

          – Michael Homer
          5 hours ago





          You're missing a closing ", somewhere. I don't know where, because that's something you've added, but the opening quote should be on line 4.

          – Michael Homer
          5 hours ago


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Unix & Linux Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f513311%2ffind-file-while-other-file-exist-in-same-directory%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

          Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

          Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...