Search for files using terminal and open with preferred program when clicked?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












0















I am not entirely sure if this is possible, but then again I am always surprised by the answers on here. With that said, Linux rules! And so do you guys!



Alright, to be a little more clear, simply put... I would like to somehow create links to files within the terminal from search results found when searching files. Same way you would click a hyperlink within the terminal, but in this case it would be a file and not an URL. Is this possible?



Below is the command in which I am working with.



grep -rnw "$2" -e "$1"


... and here is a screenshot when searching my logs for term "filezilla"



enter image description here



I would like to be able to click on the purple text which are exact file locations, the same way I would open a hyperlink within the terminal.



I tried this command below, but all this does is automatically open every search result found with the preferred program, in this case the program being mousepad.



grep -rnw "$2" -e "$1" | xargs mousepad


Thank you for any of your time.










share|improve this question


























    0















    I am not entirely sure if this is possible, but then again I am always surprised by the answers on here. With that said, Linux rules! And so do you guys!



    Alright, to be a little more clear, simply put... I would like to somehow create links to files within the terminal from search results found when searching files. Same way you would click a hyperlink within the terminal, but in this case it would be a file and not an URL. Is this possible?



    Below is the command in which I am working with.



    grep -rnw "$2" -e "$1"


    ... and here is a screenshot when searching my logs for term "filezilla"



    enter image description here



    I would like to be able to click on the purple text which are exact file locations, the same way I would open a hyperlink within the terminal.



    I tried this command below, but all this does is automatically open every search result found with the preferred program, in this case the program being mousepad.



    grep -rnw "$2" -e "$1" | xargs mousepad


    Thank you for any of your time.










    share|improve this question
























      0












      0








      0








      I am not entirely sure if this is possible, but then again I am always surprised by the answers on here. With that said, Linux rules! And so do you guys!



      Alright, to be a little more clear, simply put... I would like to somehow create links to files within the terminal from search results found when searching files. Same way you would click a hyperlink within the terminal, but in this case it would be a file and not an URL. Is this possible?



      Below is the command in which I am working with.



      grep -rnw "$2" -e "$1"


      ... and here is a screenshot when searching my logs for term "filezilla"



      enter image description here



      I would like to be able to click on the purple text which are exact file locations, the same way I would open a hyperlink within the terminal.



      I tried this command below, but all this does is automatically open every search result found with the preferred program, in this case the program being mousepad.



      grep -rnw "$2" -e "$1" | xargs mousepad


      Thank you for any of your time.










      share|improve this question














      I am not entirely sure if this is possible, but then again I am always surprised by the answers on here. With that said, Linux rules! And so do you guys!



      Alright, to be a little more clear, simply put... I would like to somehow create links to files within the terminal from search results found when searching files. Same way you would click a hyperlink within the terminal, but in this case it would be a file and not an URL. Is this possible?



      Below is the command in which I am working with.



      grep -rnw "$2" -e "$1"


      ... and here is a screenshot when searching my logs for term "filezilla"



      enter image description here



      I would like to be able to click on the purple text which are exact file locations, the same way I would open a hyperlink within the terminal.



      I tried this command below, but all this does is automatically open every search result found with the preferred program, in this case the program being mousepad.



      grep -rnw "$2" -e "$1" | xargs mousepad


      Thank you for any of your time.







      linux command-line grep terminal search






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 31 at 10:37









      Anonymous UserAnonymous User

      244




      244




















          1 Answer
          1






          active

          oldest

          votes


















          0














          Recent terminal emulators do handle hyperlinking to a limited extent. Provided that it 'knows' which programme to use to open a document then you just need to echo the file name to stdout in the terminal window with the correct protocol in front. Since we are dealing with local files then this means changing



          /path/to/file/this.doc


          to



          file:///path/to/file/this.doc


          Just like a hyperlink but without a domain name. Provided your shell knows how to open a file type then it will do so.



          for i in $(ls); do echo "file://$(pwd)/$i"; done


          ctrl+click



          EDIT



          To bring @tripleee excellent improvement out from the comments and into the answers...



          printf "file://$(pwd)/%sn" *


          END EDIT



          This will not work for files with spaces in the file names
          because the expansion of $(ls) produces a list of file names separated by spaces. A space within a file name will make the shell think it is actually two file names separated by a space.



          my file.pdf


          becomes



          my
          file.pdf


          If you have spaces in your filenames then try this until you find a more elegant solution since I just hacked it in anticipation of the next question...... ;-)



          for f in $(find . -type f -maxdepth 1 -iname "*" | sed 's/ /%20/g'); do echo "file://$(pwd)/$f#*/"; done


          Just use find in the normal way to return the files you want.






          share|improve this answer

























          • I am confused, where does the search term go?

            – Anonymous User
            Feb 3 at 18:51











          • Can you update your question so that it helps someone in which you are not assuming knows bash-scripting as well as you? Thanks.

            – Anonymous User
            Feb 3 at 18:52












          • You can easily avoid the useless use of ls; printf "file://$(pwd)/%sn" *

            – tripleee
            Feb 4 at 10:52












          • @tripleee I count myself educated on this use of ls thats a nice little nugget of knowledge.

            – bu5hman
            Feb 4 at 11:23










          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%2f497903%2fsearch-for-files-using-terminal-and-open-with-preferred-program-when-clicked%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














          Recent terminal emulators do handle hyperlinking to a limited extent. Provided that it 'knows' which programme to use to open a document then you just need to echo the file name to stdout in the terminal window with the correct protocol in front. Since we are dealing with local files then this means changing



          /path/to/file/this.doc


          to



          file:///path/to/file/this.doc


          Just like a hyperlink but without a domain name. Provided your shell knows how to open a file type then it will do so.



          for i in $(ls); do echo "file://$(pwd)/$i"; done


          ctrl+click



          EDIT



          To bring @tripleee excellent improvement out from the comments and into the answers...



          printf "file://$(pwd)/%sn" *


          END EDIT



          This will not work for files with spaces in the file names
          because the expansion of $(ls) produces a list of file names separated by spaces. A space within a file name will make the shell think it is actually two file names separated by a space.



          my file.pdf


          becomes



          my
          file.pdf


          If you have spaces in your filenames then try this until you find a more elegant solution since I just hacked it in anticipation of the next question...... ;-)



          for f in $(find . -type f -maxdepth 1 -iname "*" | sed 's/ /%20/g'); do echo "file://$(pwd)/$f#*/"; done


          Just use find in the normal way to return the files you want.






          share|improve this answer

























          • I am confused, where does the search term go?

            – Anonymous User
            Feb 3 at 18:51











          • Can you update your question so that it helps someone in which you are not assuming knows bash-scripting as well as you? Thanks.

            – Anonymous User
            Feb 3 at 18:52












          • You can easily avoid the useless use of ls; printf "file://$(pwd)/%sn" *

            – tripleee
            Feb 4 at 10:52












          • @tripleee I count myself educated on this use of ls thats a nice little nugget of knowledge.

            – bu5hman
            Feb 4 at 11:23















          0














          Recent terminal emulators do handle hyperlinking to a limited extent. Provided that it 'knows' which programme to use to open a document then you just need to echo the file name to stdout in the terminal window with the correct protocol in front. Since we are dealing with local files then this means changing



          /path/to/file/this.doc


          to



          file:///path/to/file/this.doc


          Just like a hyperlink but without a domain name. Provided your shell knows how to open a file type then it will do so.



          for i in $(ls); do echo "file://$(pwd)/$i"; done


          ctrl+click



          EDIT



          To bring @tripleee excellent improvement out from the comments and into the answers...



          printf "file://$(pwd)/%sn" *


          END EDIT



          This will not work for files with spaces in the file names
          because the expansion of $(ls) produces a list of file names separated by spaces. A space within a file name will make the shell think it is actually two file names separated by a space.



          my file.pdf


          becomes



          my
          file.pdf


          If you have spaces in your filenames then try this until you find a more elegant solution since I just hacked it in anticipation of the next question...... ;-)



          for f in $(find . -type f -maxdepth 1 -iname "*" | sed 's/ /%20/g'); do echo "file://$(pwd)/$f#*/"; done


          Just use find in the normal way to return the files you want.






          share|improve this answer

























          • I am confused, where does the search term go?

            – Anonymous User
            Feb 3 at 18:51











          • Can you update your question so that it helps someone in which you are not assuming knows bash-scripting as well as you? Thanks.

            – Anonymous User
            Feb 3 at 18:52












          • You can easily avoid the useless use of ls; printf "file://$(pwd)/%sn" *

            – tripleee
            Feb 4 at 10:52












          • @tripleee I count myself educated on this use of ls thats a nice little nugget of knowledge.

            – bu5hman
            Feb 4 at 11:23













          0












          0








          0







          Recent terminal emulators do handle hyperlinking to a limited extent. Provided that it 'knows' which programme to use to open a document then you just need to echo the file name to stdout in the terminal window with the correct protocol in front. Since we are dealing with local files then this means changing



          /path/to/file/this.doc


          to



          file:///path/to/file/this.doc


          Just like a hyperlink but without a domain name. Provided your shell knows how to open a file type then it will do so.



          for i in $(ls); do echo "file://$(pwd)/$i"; done


          ctrl+click



          EDIT



          To bring @tripleee excellent improvement out from the comments and into the answers...



          printf "file://$(pwd)/%sn" *


          END EDIT



          This will not work for files with spaces in the file names
          because the expansion of $(ls) produces a list of file names separated by spaces. A space within a file name will make the shell think it is actually two file names separated by a space.



          my file.pdf


          becomes



          my
          file.pdf


          If you have spaces in your filenames then try this until you find a more elegant solution since I just hacked it in anticipation of the next question...... ;-)



          for f in $(find . -type f -maxdepth 1 -iname "*" | sed 's/ /%20/g'); do echo "file://$(pwd)/$f#*/"; done


          Just use find in the normal way to return the files you want.






          share|improve this answer















          Recent terminal emulators do handle hyperlinking to a limited extent. Provided that it 'knows' which programme to use to open a document then you just need to echo the file name to stdout in the terminal window with the correct protocol in front. Since we are dealing with local files then this means changing



          /path/to/file/this.doc


          to



          file:///path/to/file/this.doc


          Just like a hyperlink but without a domain name. Provided your shell knows how to open a file type then it will do so.



          for i in $(ls); do echo "file://$(pwd)/$i"; done


          ctrl+click



          EDIT



          To bring @tripleee excellent improvement out from the comments and into the answers...



          printf "file://$(pwd)/%sn" *


          END EDIT



          This will not work for files with spaces in the file names
          because the expansion of $(ls) produces a list of file names separated by spaces. A space within a file name will make the shell think it is actually two file names separated by a space.



          my file.pdf


          becomes



          my
          file.pdf


          If you have spaces in your filenames then try this until you find a more elegant solution since I just hacked it in anticipation of the next question...... ;-)



          for f in $(find . -type f -maxdepth 1 -iname "*" | sed 's/ /%20/g'); do echo "file://$(pwd)/$f#*/"; done


          Just use find in the normal way to return the files you want.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Feb 4 at 12:29

























          answered Jan 31 at 12:11









          bu5hmanbu5hman

          1,282214




          1,282214












          • I am confused, where does the search term go?

            – Anonymous User
            Feb 3 at 18:51











          • Can you update your question so that it helps someone in which you are not assuming knows bash-scripting as well as you? Thanks.

            – Anonymous User
            Feb 3 at 18:52












          • You can easily avoid the useless use of ls; printf "file://$(pwd)/%sn" *

            – tripleee
            Feb 4 at 10:52












          • @tripleee I count myself educated on this use of ls thats a nice little nugget of knowledge.

            – bu5hman
            Feb 4 at 11:23

















          • I am confused, where does the search term go?

            – Anonymous User
            Feb 3 at 18:51











          • Can you update your question so that it helps someone in which you are not assuming knows bash-scripting as well as you? Thanks.

            – Anonymous User
            Feb 3 at 18:52












          • You can easily avoid the useless use of ls; printf "file://$(pwd)/%sn" *

            – tripleee
            Feb 4 at 10:52












          • @tripleee I count myself educated on this use of ls thats a nice little nugget of knowledge.

            – bu5hman
            Feb 4 at 11:23
















          I am confused, where does the search term go?

          – Anonymous User
          Feb 3 at 18:51





          I am confused, where does the search term go?

          – Anonymous User
          Feb 3 at 18:51













          Can you update your question so that it helps someone in which you are not assuming knows bash-scripting as well as you? Thanks.

          – Anonymous User
          Feb 3 at 18:52






          Can you update your question so that it helps someone in which you are not assuming knows bash-scripting as well as you? Thanks.

          – Anonymous User
          Feb 3 at 18:52














          You can easily avoid the useless use of ls; printf "file://$(pwd)/%sn" *

          – tripleee
          Feb 4 at 10:52






          You can easily avoid the useless use of ls; printf "file://$(pwd)/%sn" *

          – tripleee
          Feb 4 at 10:52














          @tripleee I count myself educated on this use of ls thats a nice little nugget of knowledge.

          – bu5hman
          Feb 4 at 11:23





          @tripleee I count myself educated on this use of ls thats a nice little nugget of knowledge.

          – bu5hman
          Feb 4 at 11:23

















          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%2f497903%2fsearch-for-files-using-terminal-and-open-with-preferred-program-when-clicked%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

          Peggy Mitchell

          Palaiologos

          The Forum (Inglewood, California)