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

Clash Royale CLAN TAG#URR8PPP
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"

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
add a comment |
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"

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
add a comment |
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"

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
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"

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
linux command-line grep terminal search
asked Jan 31 at 10:37
Anonymous UserAnonymous User
244
244
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
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 ofls;printf "file://$(pwd)/%sn" *
– tripleee
Feb 4 at 10:52
@tripleee I count myself educated on this use oflsthats a nice little nugget of knowledge.
– bu5hman
Feb 4 at 11:23
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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 ofls;printf "file://$(pwd)/%sn" *
– tripleee
Feb 4 at 10:52
@tripleee I count myself educated on this use oflsthats a nice little nugget of knowledge.
– bu5hman
Feb 4 at 11:23
add a comment |
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.
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 ofls;printf "file://$(pwd)/%sn" *
– tripleee
Feb 4 at 10:52
@tripleee I count myself educated on this use oflsthats a nice little nugget of knowledge.
– bu5hman
Feb 4 at 11:23
add a comment |
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.
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.
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 ofls;printf "file://$(pwd)/%sn" *
– tripleee
Feb 4 at 10:52
@tripleee I count myself educated on this use oflsthats a nice little nugget of knowledge.
– bu5hman
Feb 4 at 11:23
add a comment |
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 ofls;printf "file://$(pwd)/%sn" *
– tripleee
Feb 4 at 10:52
@tripleee I count myself educated on this use oflsthats 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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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